diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..e01ac3047 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + return "Hello!" +end + +def greet(name) + return "Hello, #{name}!" +end \ No newline at end of file diff --git a/00_hello/hello_spec.rb b/00_hello/hello_spec.rb index 26075944a..0a7c6e94a 100644 --- a/00_hello/hello_spec.rb +++ b/00_hello/hello_spec.rb @@ -130,3 +130,4 @@ expect(greet("Bob")).to eq("Hello, Bob!") end end + diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..bf94fd882 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(temp) + (temp - 32) * (5.0/9.0) +end + +def ctof(temp) + (temp * 9.0/5.0) + 32 +end diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..460dbbd03 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,38 @@ +def add(x,y) + + (x + y) + +end + +def subtract(x,y) + + (x - y) + +end + +def sum(arr) + + arr.inject(0) {|sum, x| sum + x} + +end + +def multiply(numbers) + + numbers.inject(1) {|sum, number| sum + number} + +end + +def power (x, y) + + x ** y + +end + +def factiorial(x) + + 1 if x == 0 || x == 1 + result = 1 + number.downto(1) { |n| result *= n} + result + +end \ No newline at end of file diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..2ba5f7ed8 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,38 @@ +def echo(str) + str +end + +def shout(str) + str.upcase +end + +def repeat(str, n = 2) + ([str] * n).join(' ') +end + +def start_of_word(str, n) + str.slice(0..(n-1)) +end + +def first_word(str) + word_array = [] + word_array = str.split(" ") + return word_array[0] +end + +def titleize(str) + word_array = [] + new_array = [] + little_word = ["the", "and"] + word_array = str.split(" ") + word_array.each do |word| + if little_word.include?(word) + new_array << word + else + new_array << word[0].upcase + word[1..-1] + end +end + puts new_array.join(" ") +end + +titleize("Hello and world how are you") \ No newline at end of file diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..c4a6b8d42 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,13 @@ +def translate(str) + vowels = %w(a e i o u) + words = str.split(" ") + translated_array = [] + words.each do |word| + if vowels.include?(word[0]) + translated_array << word + "ay" + translated_array.join("") + end + end + +end + diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..d77041a0e --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,15 @@ +def reverser + yield.split.map {|word| word.reverse}.join(" ") + +end + +def adder(num=1) + yield + num +end + +def repeater(loop=1) + loop.times do + yield + end +end + diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..ac2772888 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,7 @@ +def measure(num=1) + start = Time.now + num.times {yield} + finish = Time.now + result = (finish-start) / num + return result +end \ No newline at end of file diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb new file mode 100644 index 000000000..c7d14a067 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,5 @@ +class Friend + def greeting(name = nil) + name.nil? ? "Hello!": "Hello, #{name}!" + end +end \ No newline at end of file diff --git a/07_hello_friend/hello_friend.rb b/07_hello_friend/hello_friend.rb new file mode 100644 index 000000000..e69de29bb diff --git a/09_timer/timer.rb b/09_timer/timer.rb new file mode 100644 index 000000000..7abea0502 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,18 @@ +class Timer + attr_accessor :seconds + def initialize + @seconds = 0 + end + + def time_string + hrs = @seconds/ 3600 + mins = @seconds % 3600 / 60 + secs = @seconds % 3600 % 60 + seconds = secs.to_s.rjust(2, "0") + minutes = mins.to_s.rjust(2, "0") + hours = hrs.to_s.rjust(2, "0") + "#{hours}:#{minutes}:#{seconds}" + end + + +end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..acf2f7337 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,15 @@ +class Temperature + def initialize(temp) + @f = temp[:f] + @c = temp[:c] + end + + def in_fahrenheit + @f + end + + def in_celsius + (@f - 32) * (5.0/9) + end +end + diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..e57b924af --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,22 @@ +class Dictionary + attr_reader :entries + def initialize + @hash = {} + end + + def add(defs) + defs.each do |word, definition| + @hash[word] = definition + end + + end + + def entries + @hash + end + + def keywords + @hash.keys + end +end + diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..b48be5a66 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,34 @@ +class RPNCalculator + def initialize + @stack = [] + + end + def push(num) + @stack.push(num) + + end + def plus + @stack[-2] = @stack[-2] + @stack[-1] + @stack.pop + end + + def minus + @stack[-2] = @stack[-2] - @stack[-1] + @stack.pop + end + + def times + @stack[-2] = @stack[-2] * @stack[-1] + @stack.pop + end + + def divide + @stack[-2] = @stack[-2].to_f / @stack[-1].to_f + @stack.pop + end + def value + @stack[-1] + end + + +end diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..212f58406 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,19 @@ +class Array + def initialize(array) + @array = array + end + def sum + self.inject(0) { |sum, x| sum + x} + end + + def square + self.map { |n| n ** 2} + end + + def square! + self.map! { |n| n ** 2} + end + + +end + diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb new file mode 100644 index 000000000..599e69489 --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,5 @@ +class Fixnum + def in_words + return "zero" + end +end \ No newline at end of file