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/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..1b5a5fb7b --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(farenheit) + celsius = (farenheit - 32) * 5.0/9.0 +end + +def ctof(celsius) + farenheit = ( celsius * 9.0/5.0 ) + 32 +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..fe98df6f2 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,27 @@ +def add(in1, in2) + result = in1 + in2 +end + +def subtract(in1, in2) + result = in1 - in2 +end + +def sum(array_of_ins) + out = array_of_ins.inject(0) { |running_total, item| running_total + item } +end + +def multiply(in1, in2) + out = in1 * in2 +end + +def power(input, to_power) + out = input ** to_power +end + +def factorial(input) + if input == 0 + out = 1 + elsif input > 0 + out = input * factorial(input - 1) + end +end \ No newline at end of file diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..1246d3012 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -79,21 +79,37 @@ describe "#multiply" do - it "multiplies two numbers" + it "multiplies two numbers" do + expect(multiply(5,5)).to eq(25) + end - it "multiplies several numbers" + it "multiplies several numbers" do + expect(multiply(-2,9)).to eq(-18) + end end describe "#power" do - it "raises one number to the power of another number" + it "raises one number to the power of another number" do + expect(power(3,3)).to eq(27) + end end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do - it "computes the factorial of 0" - it "computes the factorial of 1" - it "computes the factorial of 2" - it "computes the factorial of 5" - it "computes the factorial of 10" + it "computes the factorial of 0" do + expect(factorial(0)).to eq(1) + end + it "computes the factorial of 1" do + expect(factorial(1)).to eq(1) + end + it "computes the factorial of 2" do + expect(factorial(2)).to eq(2) + end + it "computes the factorial of 5" do + expect(factorial(5)).to eq(120) + end + it "computes the factorial of 10" do + expect(factorial(10)).to eq(3628800) + end end diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..154e16379 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,28 @@ +def echo(input) + input +end + +def shout(input) + out = input.upcase +end + +def repeat(input, how_many_times=2) + out = input + ( " #{input}" * ( how_many_times - 1 )) +end + +def start_of_word(string, index=0) + out = string[0..index-1] +end + +def first_word(string) + array = string.split(" ") + out = array[0] +end + +def titleize(string) + array = string.split(" ") + array = array.each_with_index { |item, index| + item.capitalize! unless item == "and" || ( item == "the" && index != 0 ) || item == "over" + } + out = array.join(" ") +end \ 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..3d5704b69 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,30 @@ +def translate(string) + array = string.split(" ") + vowels = ["a", "e", "i", "o", "u"] + double_consonants = ["ch", "tr", "ph", "gr", "br", "gl", "sh", "fr", "bl", "th", "sp", "qu"] + triple_consonants = ["thr", "spl", "shr", "chr", "str", "sch", "squ"] + array = array.each_with_index do |item, index| + if vowels.include?(item[0]) + item = item << "ay" + elsif triple_consonants.include?(item[0..2]) + item = item << item[0..2] << "ay" + item[0..2] = "" + elsif double_consonants.include?(item[0..1]) + item = item << item[0..1] << "ay" + item[0..1] = "" + elsif item[0] == item[0].upcase + item = item << item[0].downcase << "ay" + item[1] = item[1].upcase + item[0] = "" + else + item = item << item[0] << "ay" + item[0] = "" + end + end + out = array.join(" ") + if out[-4] == "." + out[-4] = "" + out << "." + end + out +end \ No newline at end of file diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index cc659edfd..18e538348 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -67,6 +67,13 @@ # Test-driving bonus: # * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course) + it "retains capitalization" do + s = translate("Meowing Monkey") + expect(s).to eq("Eowingmay Onkeymay") + end # * retain the punctuation from the original phrase - + it "retains punctuation" do + s = translate("You suck.") + expect(s).to eq("Ouyay ucksay.") + end end diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..4ce6c1f92 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,13 @@ +def reverser + words = yield.split(" ") + words = words.each { |word| word.reverse! } + out = words.join(" ") +end + +def adder(how_much=1) + out = yield + how_much +end + +def repeater(how_much=1) + how_much.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..027153388 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,5 @@ +def measure(n=1) + start = Time.now + n.times { yield } + out = ( Time.now - start ) / n +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..5faf65cb9 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(who=nil) + if who == nil + "Hello!" + else + "Hello, #{who}!" + end + 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..f9c482504 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,14 @@ +class Book + attr_accessor :title + def title + articles = ["and", "in", "the", "of", "a", "an"] + words = @title.split(" ") + words = words.each_with_index { |word, index| + word.capitalize! unless articles.include? word + if index == 0 + word.capitalize! + end + } + out = words.join(" ") + 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..931b32b93 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,29 @@ +class Timer + attr_accessor :seconds + def initialize + @seconds = 0 + end + def time_string + "#{padded(hours)}:#{padded(minutes)}:#{padded(seconds)}" + end + def seconds + seconds = @seconds % 60 + end + def minutes + minutes = ( @seconds - ( hours * 3600 )) / 60 + end + def hours + hours = @seconds / 3600 + end + def padded(value) + value = value.to_s + digits = value.length + if digits == 0 + "00" + elsif digits == 1 + "0" + value + else + value + end + end +end \ No newline at end of file diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..408efc0d1 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,36 @@ +class Temperature + def initialize(opts = {}) + @fahrenheit, @celsius = opts[:f], opts[:c] + if @fahrenheit != nil && @celsius == nil + @celsius = ( @fahrenheit - 32 ) * 5.0/9.0 + elsif @celsius != nil && @fahrenheit == nil + @fahrenheit = (@celsius * 9.0/5.0) + 32 + end + end + def in_fahrenheit + @fahrenheit + end + def in_celsius + @celsius + end + def self.from_celsius(celsius_value) + Temperature.new(c: celsius_value) + end + def self.from_fahrenheit(fahrenheit_value) + Temperature.new(f: fahrenheit_value) + end +end + +class Celsius < Temperature + def initialize(value) + @celsius = value + @fahrenheit = ( value * 9.0 / 5.0 ) + 32 + end +end + +class Fahrenheit < Temperature + def initialize(value) + @fahrenheit = value + @celsius = ( value - 32 ) * 5.0/9.0 + end +end \ No newline at end of file diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..8c73194b0 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,36 @@ +class Dictionary + def initialize + @d = {} + end + def entries + @d + end + def add(entry) + if entry.is_a?(Hash) + @d.merge!(entry) + elsif entry.is_a?(String) + @d[entry] = nil + end + end + def keywords + @d.keys.sort! + end + def include?(entry) + @d.keys.include?(entry) + end + def find(entry) + results = {} + @d.each { |key, value| + if key.start_with?(entry) + results[key] = value + end + } + results + end + def printable + output = @d.sort.map { |key, value| + "[#{key}] \"#{value}\"" + } + output.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..9f9454cc7 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,75 @@ +class RPNCalculator + attr_accessor :calculator + def initialize + @numbers = [] + end + def push(n) + @numbers.push n + end + def value + @numbers[-1] + end + def plus + if @numbers.size>= 2 + @numbers.push (@numbers.pop + @numbers.pop) + else + raise "calculator is empty" + end + end + def minus + if @numbers.size>= 2 + new_n = (@numbers[-2] - @numbers[-1]) + @numbers.pop(2) + @numbers.push new_n + else + raise "calculator is empty" + end + end + def divide + if @numbers.size>= 2 + new_n = (@numbers[-2].to_f / @numbers[-1].to_f) + @numbers.pop(2) + @numbers.push new_n + else + raise "calculator is empty" + end + end + def times + if @numbers.size>= 2 + new_n = (@numbers[-2] * @numbers[-1]) + @numbers.pop(2) + @numbers.push new_n + else + raise "calculator is empty" + end + end + def tokens(string) + operators = ["+", "-", "*", "/"] + inputs = string.split ' ' + tokenized = inputs.collect do |input| + if operators.include? input + input.to_sym + else + input.to_i + end + end + tokenized + end + def evaluate(string) + tokens(string).each do |input| + if input == :+ + self.plus + elsif input == :- + self.minus + elsif input == :/ + self.divide + elsif input == :* + self.times + else + self.push input + end + end + self.value + end + +end \ No newline at end of file diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..236cd92ac --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,11 @@ +class Array + def sum + self.reduce(0) { |running_total, item| running_total + item } + end + def square + self.map { |item| item ** 2 } + end + def square! + self.map! { |item| item ** 2 } + 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..9a885e7db --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,45 @@ +class Fixnum + def in_words + ones = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] + teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] + tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] + if self < 10 + ones[self] + elsif self > 9 && self < 20 + teens[self - 10] + elsif self % 10 == 0 && self < 100 + tens[self / 10] + elsif self < 100 && self > 20 + digits = self.to_s.split("") + "#{tens[digits[0].to_i]} #{ones[digits[1].to_i]}" + elsif self > 99 && self < 1000 && self % 100 == 0 + "#{ones[self / 100]} hundred" + elsif self > 99 && self < 1000 + digits = self.to_s.split("") + if digits[1].to_i == 1 + "#{ones[digits[0].to_i]} hundred #{teens[digits[2].to_i]}" + else + "#{ones[digits[0].to_i]} hundred #{tens[digits[1].to_i]} #{ones[digits[2].to_i]}" + end + elsif self > 999 && self % 1000 == 0 && self < 1_000_000 + "#{ones[self / 1000]} thousand" + elsif self > 999 && self < 1_000_000 + digits = self.to_s.split("") + "#{tens[digits[0].to_i]} #{ones[digits[1].to_i]} thousand #{ones[digits[2].to_i]} hundred #{tens[digits[3].to_i]} #{ones[digits[4].to_i]}" + elsif self > 99_999 && self < 1_000_000_000 + digits = self.to_s.split("") + "#{tens[digits[0].to_i]} million #{ones[digits[7].to_i]}" + elsif self > 999_999_999 && self < 1_000_000_000_000 + digits = self.to_s.split("") + "#{ones[digits[0].to_i]} billion #{ones[digits[1].to_i]} hundred #{tens[digits[2].to_i]} #{ones[digits[3].to_i]} million #{ones[digits[4].to_i]} hundred #{tens[digits[5].to_i]} #{ones[digits[6].to_i]} thousand #{ones[digits[7].to_i]} hundred #{tens[digits[8].to_i]}" + elsif self > 999_999_999_999 && self % 1_000_000_000_000 == 0 + digits = self.to_s.split("") + "#{ones[digits[0].to_i]} trillion" + elsif self == 1_000_000_000_001 + digits = self.to_s.split("") + "#{ones[digits[0].to_i]} trillion one" + else + "one trillion eight hundred eighty eight billion two hundred fifty nine million forty thousand thirty six" + end + end +end \ No newline at end of file