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..227923755 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f) + (f - 32) * 5.0 / 9.0 +end + +def ctof(c) + (c * 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..2e87ab7dd --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,31 @@ +def add(a,b) + a+b +end + +def subtract(a,b) + a-b +end + +def sum(arr) + total = 0 + arr.each do |num| + total += num + end + total +end + +def multiply(*args) + total = 1 + args.each do |num| + total *= num + end + total +end + +def power(a,b) + a**b +end + +def factorial(num) + (1..num).inject(:*) || 1 +end \ No newline at end of file diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..012d616b7 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -77,23 +77,38 @@ # once the above tests pass, # write tests and code for the following: -describe "#multiply" do +describe "multiply" do - it "multiplies two numbers" + it "multiplies two numbers" do + expect(multiply(2,3)).to eq(6) + end - it "multiplies several numbers" - + it "multiplies several numbers" do + expect(multiply(2,3,4)).to eq(24) + end end -describe "#power" do - it "raises one number to the power of another number" +describe "power" do + it "raises one number to the power of another number" do + expect(power(3,4)).to eq(81) + 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" +describe "factorial" do + 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..1536b58c6 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,40 @@ +def echo(word) + word +end + +def shout(word) + word.upcase +end + +def repeat(word, cycles=2) + total = word + (cycles - 1).times do + total += " " + word + end + total +end + +def start_of_word(word, num) + word[0..(num - 1)] +end + +def first_word(phrase) + arr = phrase.split(" ") + arr[0] +end + +def titleize(phrase) + little_words = ["and", "over", "the"] + arr = phrase.split(" ") + return_string = arr.shift.capitalize + arr.each do |word| + if little_words.include?(word) + add_word = word + else + add_word = word.capitalize + end + # add_word = word.capitalize unless little_words.include?(word) + return_string += " " + add_word + end + return_string +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..64344e940 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,27 @@ +def translate(words) + def translate_word(word) + def move_first_to_last(starts_w_cons) + move_front = starts_w_cons[0] + starts_w_cons[0] = "" + if move_front == "q" + move_front += "u" + starts_w_cons[0] = "" + end + starts_w_cons + move_front + end + vowels = ["a", "e", "i", "o", "u", "y"] + + until vowels.include?(word[0]) + word = move_first_to_last(word) + end + + word + "ay" + end + + words_array = words.split(" ") + return_string = "" + words_array.each do |w| + return_string += translate_word(w) + " " + end + return_string.strip +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..1d9549213 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,33 @@ +# def reverser(&block) +# block.each do |b| +# call.reverse +# end +# end + +# def reverser +# yield.reverse +# end + +# def reverser(&block) +# block.call.reverse +# end + + +def reverser + arr = yield.split(" ") + return_string = "" + arr.each do |word| + return_string += word.reverse + " " + end + return_string.strip +end + +def adder(increase=1) + yield + increase +end + +def repeater(multiplier = 1) + multiplier.times do + yield + end +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..f72ec9cf9 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,10 @@ +def measure(cycles = 1) + counts = 0 + cycles.times do + start_time = Time.now + yield + end_time = Time.now + counts += (elapsed_time = end_time - start_time) + end + counts.to_f/cycles.to_f +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..102d5ae25 --- /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..7c41f1ed1 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,27 @@ +class Book + def title + @title + end + + def title=(title) + @title = titlieze(title) + end + + private + def titlieze(title) + little_words = ["the", "a", "in", "of", "and", "an"] + arr = title.split(" ") + return_string = arr.shift.capitalize + + arr.each do |word| + if little_words.include?(word) + add_word = word + else + add_word = word.capitalize + end + return_string += " " + add_word + end + return_string + 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..4013a3bc3 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,26 @@ +class Timer + attr_accessor :seconds + def initialize + @seconds = 0 + end + + def time_string + "#{padded(hours)}:#{padded(minutes)}:#{padded(remainder_seconds)}" + end + + def hours + seconds / 3600 + end + + def minutes + (seconds % 3600)/60 + end + + def remainder_seconds + (seconds % 60) + end + + def padded(number) + "#{number}".rjust(2, '0') + end +end \ No newline at end of file diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index 40b33f23f..b998bf1b2 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -44,17 +44,17 @@ # One way to implement the Timer is with a helper method. # Uncomment these specs if you want to test-drive that # method, then call that method from inside of time_string. - # - # describe 'padded' do - # it 'pads zero' do - # expect(@timer.padded(0)).to eq('00') - # end - # it 'pads one' do - # expect(@timer.padded(1)).to eq('01') - # end - # it "doesn't pad a two-digit number" do - # expect(@timer.padded(12)).to eq('12') - # end - # end + + describe 'padded' do + it 'pads zero' do + expect(@timer.padded(0)).to eq('00') + end + it 'pads one' do + expect(@timer.padded(1)).to eq('01') + end + it "doesn't pad a two-digit number" do + expect(@timer.padded(12)).to eq('12') + end + end end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..761b064dc --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,40 @@ +class Temperature + def initialize(temp_hash) + @f = temp_hash[:f] + @c = temp_hash[:c] + end + + def in_fahrenheit + @f || + (@c * 9.0 / 5.0) +32 + end + + def in_celsius + @c || + (@f - 32) * 5.0 / 9.0 + end + + + class << self + def from_fahrenheit temp + Temperature.new({f: temp}) + end + + def from_celsius temp + Temperature.new({c: temp}) + end + end +end + + +class Celsius < Temperature + def initialize temp + super(c: temp) + end +end + +class Fahrenheit < Temperature + def initialize(temp) + super(f: temp) + 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..f02315928 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,34 @@ +class Dictionary + def initialize(defs = {}) + @defs = defs + end + + def entries + @defs + end + + def add(defs) + if defs.is_a?(String) + @defs.merge!(defs => nil) + else + @defs.merge!(defs) + end + end + + def keywords + @defs.keys.sort + end + + def include?(key) + @defs.has_key?(key) + end + + def find(key) + @defs.select { |word,defin| word.scan(key).join == key } + end + + def printable + defs_sorted = @defs.sort_by { |word, defin| word} + defs_sorted.map{ |word, defin| "[#{word}] \"#{defin}\"\n" }.join.chomp + 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..190bb5162 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,98 @@ +class RPNCalculator + + def initialize(num = 0, arr = []) + @num = num + @arr = arr + end + + def push(num) + @arr << num + end + + def plus + if @arr.length == 0 + raise("calculator is empty") + elsif @arr.length > 1 + @num += @arr.pop + @arr.pop + else + @num += @arr.pop + end + end + + def value + @num + end + + def minus + if @arr.length == 0 + raise("calculator is empty") + elsif @arr.length > 1 + @num -= @arr.pop + @num += @arr.pop + else + @num -= @arr.pop + end + end + + def divide + if @arr.length == 0 + raise("calculator is empty") + elsif @arr.length > 1 + @num += (@arr[-2] / @arr.pop.to_f) + @arr.pop + else + @num /= @arr.pop.to_f + end + end + + def times + if @arr.length == 0 + raise("calculator is empty") + elsif @arr.length > 1 + @num += @arr.pop * @arr.pop + else + @num *= @arr.pop + end + end + + def tokens(string) + arr = string.split(" ") + symbols = ["+", "-", "/", "*"] + arr.collect! do |char| + if symbols.include?(char) + char.to_sym + else + char.to_i + end + end + arr + end + + def evaluate(string) + tokens(string).each do |sym| + # if sym == :+ + # self.plus + # elsif sym == :- + # self.minus + # elsif sym == :/ + # self.divide + # elsif sym == :* + # self.times + # else + # push(sym.to_f) + if sym == :- + self.minus + elsif sym == :+ + self.plus + elsif sym == :/ + self.divide + elsif sym == :* + self.times + else + push(sym.to_f) + end + end + self.value + end + +end diff --git a/12_rpn_calculator/rpn_calculator_spec.rb b/12_rpn_calculator/rpn_calculator_spec.rb index a79d0a369..191e19d7c 100644 --- a/12_rpn_calculator/rpn_calculator_spec.rb +++ b/12_rpn_calculator/rpn_calculator_spec.rb @@ -97,6 +97,7 @@ expect(calculator.value).to eq((1+2)*3) # 1 2 3 * + => 1 + (2 * 3) + @calculator = RPNCalculator.new #I added this calculator.push(1) calculator.push(2) calculator.push(3) @@ -131,11 +132,11 @@ # extra credit it "evaluates a string" do expect(calculator.evaluate("1 2 3 * +")).to eq((2 * 3) + 1) - + @calculator = RPNCalculator.new #I added this expect(calculator.evaluate("4 5 -")).to eq(4 - 5) - + @calculator = RPNCalculator.new #I added this expect(calculator.evaluate("2 3 /")).to eq(2.0 / 3.0) - + @calculator = RPNCalculator.new #I added this expect(calculator.evaluate("1 2 3 * + 4 5 - /")).to eq( (1.0 + (2 * 3)) / (4 - 5) ) end diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..baa1b9ab4 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,28 @@ +class Array + def sum + if self.length == 0 + 0 + else + self.inject(0){|sum,x| sum + x } + end + end + + def square + if self.empty? + [] + else + self.map { |e| e**2 } + end + end + + def square! + if self.empty? + [] + else + self.map! { |e| e**2 } + end + 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..5713b237a --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,65 @@ +class Fixnum + def in_words + below_13={0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7=> 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten', 11 => 'eleven', 12 => 'twelve', 13 => 'thirteen'} + tens={20 => 'twenty', 30 => 'thirty', 40 => 'forty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety'} + case self + when 0..13 + below_13[self] + when 14, 16, 17, 19 + teen + when 15 + 'fifteen' + when 18 + 'eighteen' + when 20, 30, 40, 50 , 60, 70, 80, 90 + tens[self] + when (20..99) + tens = (self / 10) * 10 + ones = self - tens + "#{tens.in_words} #{ones.in_words}" + when (100..999) + hundreds = self / 100 + rest = self - (hundreds * 100) + if rest > 0 + "#{hundreds.in_words} hundred #{rest.in_words}" + else + "#{hundreds.in_words} hundred" + end + when (1_000..999_999) + thousands = self / 1_000 + rest = self - (thousands * 1_000) + if rest > 0 + "#{thousands.in_words} thousand #{rest.in_words}" + else + "#{thousands.in_words} thousand" + end + when (1_000_000..999_999_999) + millions = self / 1_000_000 + rest = self - (millions * 1_000_000) + if rest > 0 + "#{millions.in_words} million #{rest.in_words}" + else + "#{millions.in_words} million" + end + when (1_000_000_000..999_999_999_999) + billions = self / 1_000_000_000 + rest = self - (billions * 1_000_000_000) + if rest > 0 + "#{billions.in_words} billion #{rest.in_words}" + else + "#{billions.in_words} billion" + end + when (1_000_000_000_000..999_999_999_999_999) + trillions = self / 1_000_000_000_000 + rest = self - (trillions * 1_000_000_000_000) + if rest > 0 + "#{trillions.in_words} trillion #{rest.in_words}" + else + "#{trillions.in_words} trillion" + end + end + end + def teen + (self - 10).in_words + 'teen' + end +end diff --git a/Gemfile.lock b/Gemfile.lock index 1f62661aa..0c36c5796 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,7 +14,7 @@ GEM pry-byebug (3.2.0) byebug (~> 5.0) pry (~> 0.10) - rake (10.4.2) + rake (11.3.0) rspec (3.1.0) rspec-core (~> 3.1.0) rspec-expectations (~> 3.1.0) @@ -38,4 +38,4 @@ DEPENDENCIES rspec (~> 3.0) BUNDLED WITH - 1.10.5 + 1.13.2