diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..3623e57f4 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(who) + "Hello, #{who}!" +end \ No newline at end of file diff --git a/00_hello/hello_spec.rb b/00_hello/hello_spec.rb index 26075944a..3eed35159 100644 --- a/00_hello/hello_spec.rb +++ b/00_hello/hello_spec.rb @@ -25,7 +25,7 @@ # Open up `hello.rb` in a text editor. Save it. Run the test again. # # rake -# +#ra # ## Watch it fail # # Now you should see an error like this: diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..7e06948db --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f_temp) + c_temp = (f_temp - 32) * 5 / 9 +end + +def ctof(c_temp) + f_temp = c_temp.to_f * 9 / 5 + 32.0 +end \ No newline at end of file diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..fffb59dea --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,17 @@ +def add(a, b) + a + b +end + +def subtract(a, b) + a - b +end + +def sum(arr) + sum = 0 + + arr.each do |i| + sum += i + end + + sum +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..6005d2c7f --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,57 @@ +def echo(s) + s +end + +def shout(s) + s.upcase +end + +def repeat(s, n_times = 2) + repeat_s = s + repeat_s += " #{s}" * (n_times - 1) +end + +def start_of_word(word, characters) + partial_word = "" + + characters.times do |c| + partial_word += word[c] + end + + partial_word +end + +def first_word(words) + first_word = "" + + i = 0 + until words[i] == " " do + first_word += words[i] + i += 1 + end + + first_word +end + + +def titleize(phrase) + word = "" + words = [] + + phrase.length.times do |i| + if phrase[i] == " " + words.push(word.downcase) + word = "" + else i == phrase.length - 1 + word += phrase[i] + end + words.push(word.downcase) if i == phrase.length - 1 + end + + words.each_with_index do |word, i| + words[i][0] = word[0].upcase unless word == "the" || word == "and" || word == "over" + words[i][0] = word[0].upcase if i == 0 + end + + words = words.join(" ") +end diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..b98753d27 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,26 @@ +def translate(phrase) + words = phrase.split(' ') + + words.each_with_index do |word, i| + translated_word = word.downcase + + case translated_word[0..2] + when /thr/, /sch/, /[^aeiou]qu/ + translated_word += translated_word[0..2] + translated_word = translated_word[3..-1] + when /ch./, /qu./, /th./, /br./ + translated_word += translated_word[0..1] + translated_word = translated_word[2..-1] + when /[^aeiou]../ + translated_word += translated_word[0] + translated_word = translated_word[1..-1] + else + + end + + translated_word += "ay" + words[i] = translated_word + end + + words = words.join(" ") +end \ No newline at end of file diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..d64eb0547 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,29 @@ +def word_reverse(string) + reverse_string = "" + + i = -1 + string.length.times do + reverse_string += string[i] + i -= 1 + end + + reverse_string +end + +def reverser + text = yield.to_s.split + + text.each_with_index do |word, i| + text[i] = word_reverse(word).downcase + end + + text = text.join(" ") +end + +def adder(n = 1) + yield + n +end + +def repeater(n = 1) + n.times { yield } +end \ No newline at end of file diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..659cd8072 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,7 @@ +def measure(number_of_times = 1) + start = Time.now + number_of_times.times { yield } + finish = Time.now + duration = finish - start + average_duration = duration / number_of_times +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..e8e132afa --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,5 @@ +class Friend + def greeting(someone = false) + someone ? "Hello, #{someone}!" : "Hello!" + end +end \ No newline at end of file diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb new file mode 100644 index 000000000..ef147f70e --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,44 @@ +class Book + def initialize + @title = "" + end + + def title + @title + end + + def title=(new_title) + @title = titleize(new_title) + end + + def titleize(phrase) + word = "" + words = [] + + phrase.length.times do |i| + if phrase[i] == " " + words.push(word.downcase) + word = "" + else i == phrase.length - 1 + word += phrase[i] + end + words.push(word.downcase) if i == phrase.length - 1 + end + + words.each_with_index do |word, i| + words[i][0] = word[0].upcase unless lowercase_word?(word) + words[i][0] = word[0].upcase if i == 0 + end + + words = words.join(" ") + end + + def lowercase_word?(word) + lowercase_words = ["the", "and", "over", "under", "of", "in", "a", "an"] + lowercase_words.each do |lowercase_word| + return true if word.downcase == lowercase_word + end + + return false + end +end \ No newline at end of file diff --git a/09_timer/timer.rb b/09_timer/timer.rb new file mode 100644 index 000000000..b9ef41807 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,19 @@ +class Timer + def initialize + @seconds = 0 + end + + attr_accessor :seconds + + def time_string + hours = @seconds / 60 / 60 + minutes = (@seconds / 60) - (hours * 60) + seconds = @seconds - (hours * 60 * 60) - (minutes * 60) + + hours_string = hours < 10 ? "0#{hours}" : hours.to_s + minutes_string = minutes < 10 ? "0#{minutes}" : minutes.to_s + seconds_string = seconds < 10 ? "0#{seconds}" : seconds.to_s + + "#{hours_string}:#{minutes_string}:#{seconds_string}" + end +end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..e3e3f93be --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,41 @@ +class Temperature + def initialize(options) + @in_celsius = options[:c] ? options[:c] : ftoc(options[:f]) + @in_fahrenheit = options[:f] ? options[:f] : ctof(options[:c]) + end + + attr_reader :in_celsius, :in_fahrenheit + + def self.from_celsius(in_celsius) + Temperature.new( { c: in_celsius } ) + end + + def self.from_fahrenheit(in_fahrenheit) + Temperature.new( { f: in_fahrenheit } ) + end + + def ftoc(in_fahrenheit) + in_celsius = (in_fahrenheit - 32) * 5 / 9 + end + + def ctof(in_celsius) + in_fahrenheit = in_celsius.to_f * 9 / 5 + 32.0 + end +end + +class Celsius < Temperature + def initialize(in_celsius) + @in_celsius = in_celsius + @in_fahrenheit = ctof(in_celsius) + end +end + +class Fahrenheit < Temperature + def initialize(in_fahrenheit) + @in_celsius = ftoc(in_fahrenheit) + @in_fahrenheit = in_fahrenheit + end +end + + + diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..01adfa287 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,49 @@ +class Dictionary + attr_reader :entries + + def initialize + @entries = {} + end + + def add(entry) + @entries[entry] = nil if entry.is_a?(String) + + if entry.is_a?(Hash) + entry.each_key { |key| @entries[key.to_s] = entry[key].to_s } + end + end + + def include?(keyword) + @entries.each_key do |entry_key| + return true if entry_key == keyword + end + + return false + end + + def find(keyword) + entry = {} + + @entries.each_key do |key| + entry[key] = @entries[key] if key.include?(keyword) + end + + return entry + end + + def keywords + keywords = @entries.keys.to_a + keywords.sort + end + + def printable + printable_text = [] + + keywords.each do |key| + printable_text.push("[#{key}] \"#{@entries[key]}\"") + end + + return printable_text.join("\n") + end + +end \ No newline at end of file diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..241b3560b --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,89 @@ +class RPNCalculator + attr_reader :value + + def initialize + @stack = [] + end + + def push(val) + @stack.push(val) + end + + def value + @stack[-1] + end + + def plus + if @stack.count >= 2 + b = @stack.pop + a = @stack.pop + push(a + b) + else + raise "calculator is empty" + end + end + + def minus + if @stack.count >= 2 + b = @stack.pop + a = @stack.pop + push(a - b) + else + raise "calculator is empty" + end + end + + def divide + if @stack.count >= 2 + b = @stack.pop.to_f + a = @stack.pop.to_f + push(a / b) + else + raise "calculator is empty" + end + end + + def times + if @stack.count >= 2 + b = @stack.pop + a = @stack.pop + push(a * b) + else + raise "calculator is empty" + end + end + + def tokens(s) + tokenized_s = [] + + s.length.times do |i| + next if s[i] == " " + /[0-9]/.match(s[i]) ? tokenized_s.push(s[i].to_i) : tokenized_s.push(:"#{s[i]}") + end + + return tokenized_s + end + + def evaluate(s) + s.length.times do |i| + case s[i] + when /[0-9]/ + push(s[i].to_i) + when "+" + plus + when "-" + minus + when "/" + divide + when "*" + times + else + next + end + end + + value + end + +end + diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..e0e464c0f --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,28 @@ +class Array + def sum + sum = 0 + return sum unless self.count > 0 + + self.each { |i| sum += i } + + return sum + end + + def square + return [] unless self.count > 0 + + squared_arr = [] + self.each { |i| squared_arr.push(i**2) } + + return squared_arr + end + + def square! + return self unless self.count > 0 + + self.each_with_index { |val, i| self[i] = val**2 } + + return self + end + +end \ No newline at end of file diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb new file mode 100644 index 000000000..3ad770ff6 --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,61 @@ +class Fixnum + def in_words + numbers_in_words = {} + numbers_in_words[0] = 'zero' + numbers_in_words[1] = 'one' + numbers_in_words[2] = 'two' + numbers_in_words[3] = 'three' + numbers_in_words[4] = 'four' + numbers_in_words[5] = 'five' + numbers_in_words[6] = 'six' + numbers_in_words[7] = 'seven' + numbers_in_words[8] = 'eight' + numbers_in_words[9] = 'nine' + numbers_in_words[10] = 'ten' + numbers_in_words[11] = 'eleven' + numbers_in_words[12] = 'twelve' + numbers_in_words[13] = 'thirteen' + numbers_in_words[14] = 'fourteen' + numbers_in_words[15] = 'fifteen' + numbers_in_words[16] = 'sixteen' + numbers_in_words[17] = 'seventeen' + numbers_in_words[18] = 'eighteen' + numbers_in_words[19] = 'nineteen' + numbers_in_words[20] = 'twenty' + numbers_in_words[30] = 'thirty' + numbers_in_words[40] = 'forty' + numbers_in_words[50] = 'fifty' + numbers_in_words[60] = 'sixty' + numbers_in_words[70] = 'seventy' + numbers_in_words[80] = 'eighty' + numbers_in_words[90] = 'ninety' + numbers_in_words[100] = 'hundred' + numbers_in_words[1_000] = 'thousand' + numbers_in_words[1_000_000] = 'million' + numbers_in_words[1_000_000_000] = 'billion' + numbers_in_words[1_000_000_000_000] = 'trillion' + + ones = self % 10 + tens = self % 100 - ones + under_20 = self % 20 + hundreds = self % 1_000 / 100 + thousands = self % 1_000_000 / 1_000 + millions = self % 1_000_000_000 / 1_000_000 + billions = self % 1_000_000_000_000 / 1_000_000_000 + trillions = self % 1_000_000_000_000_000 / 1_000_000_000_000 + + return numbers_in_words[self] if self < 20 && self >= 0 + return numbers_in_words[self] if self < 100 && self % 10 == 0 && self > 0 + + words = [] + words.push("#{trillions.in_words} trillion") if trillions > 0 + words.push("#{billions.in_words} billion") if billions > 0 + words.push("#{millions.in_words} million") if millions > 0 + words.push("#{thousands.in_words} thousand") if thousands > 0 + words.push("#{hundreds.in_words} hundred") if hundreds > 0 + words.push("#{tens.in_words}") if tens >= 20 + words.push("#{ones.in_words}") if tens >= 20 && ones > 0 + words.push("#{under_20.in_words}") if tens < 20 && under_20 > 0 + words.join(" ") + end +end \ No newline at end of file