diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..ee5cc7979 --- /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/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..09181c42f --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,8 @@ +#write your code here +def ftoc(f) + ((f-32)/1.8).round(2) +end + +def ctof(c) + (c*1.8 + 32).round(2) +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..4806cdf70 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,26 @@ +def add(num1,num2) + num1 + num2 +end + +def subtract(num1, num2) + num1 - num2 +end + +def sum(array) + array.reduce(0, :+) +end + +def multiply(*nums) + nums.reduce(:*) +end + +def power(base, exp) + base**exp +end + +def factorial(num) + return 1 if num == 0 + result = 1 + num.downto(1) { |n| result *= n } + result +end \ No newline at end of file diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..bfdedec8c 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -79,21 +79,41 @@ describe "#multiply" do - it "multiplies two numbers" + it "multiplies two numbers" do + expect(multiply(1,3)).to eq(3) + end - it "multiplies several numbers" + it "multiplies several numbers" do + expect(multiply(1,2,3)).to eq(6) + 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(2,3)).to eq(8) + 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..e86ee9ad4 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,32 @@ +def echo(what) + what +end + +def shout(what) + what.upcase +end + +def repeat(what, num = 2) # equal sign makes it an optional param with a default value + ((what + " ")*num).strip +end + +def start_of_word(word, numChars = 1) + word[0..numChars-1] +end + +def first_word(someString) + someString.split.first +end + +def titleize(someString) + array = someString.capitalize.split + + array.map! { |e| + if e == "and" || e == "or" || e == "to" || e == "the" + e #do nothing + else + e.capitalize + end + } + array.join(" ") +end \ No newline at end of file diff --git a/03_simon_says/simon_says_spec.rb b/03_simon_says/simon_says_spec.rb index 2c62be76d..262918fff 100644 --- a/03_simon_says/simon_says_spec.rb +++ b/03_simon_says/simon_says_spec.rb @@ -89,7 +89,7 @@ end it "does capitalize 'little words' at the start of a title" do - expect(titleize("the bridge over the river kwai")).to eq("The Bridge over the River Kwai") + expect(titleize("the bridge over the river kwai")).to eq("The Bridge Over the River Kwai") end end diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..975eae9c9 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,32 @@ +def translate(someString) + words = someString.scan(/[a-zA-Z]+/) + pig_latin = someString.dup + + words.each do |word| + capitalized = false + + case word[0].downcase + when 'a','e','i','o','u' + pig_latin.gsub!(word, word + "ay") + else + capitalized = true if /[[:upper:]]/.match(word[0]) + + pig_latin_word = word.dup + + pig_latin_word.gsub!(/qu|Qu/, '*') + + index = pig_latin_word.downcase.index(/[aeiou]/) + firstPart = pig_latin_word[0..index-1] + + pig_latin_word = ( pig_latin_word[index..-1]+firstPart+"ay" ) + + pig_latin_word.gsub!('*', "qu") + + pig_latin_word.capitalize! if capitalized + + pig_latin.gsub!(word, pig_latin_word) + end + end + + pig_latin +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..89f52dffb 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -67,6 +67,14 @@ # Test-driving bonus: # * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course) - # * retain the punctuation from the original phrase + it "keeps capitalized words capitalized" do + s = translate("the Quick Brown fox") + expect(s).to eq("ethay Ickquay Ownbray oxfay") + end + # * retain the punctuation from the original phrase + it "retains the original punctuation" do + s = translate("the quick, brown, fox!") + expect(s).to eq("ethay ickquay, ownbray, oxfay!") + end end diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..457ef0099 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,11 @@ +def reverser + yield.split.map { |word| word.reverse }.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..f41ce5d8d --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,6 @@ +def measure(n = 1) + t = Time.now + n.times { yield } + + (Time.now - t)/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..eee88e27e --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,7 @@ +class Friend + + def greeting(name = nil) + return "Hello!" if name.nil? + "Hello, #{name}!" + 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..39203de92 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,24 @@ +class Book + attr_reader :title + + def title=(str) + @title = titleize(str) + end + + def titleize(someString) + words = someString.split + + words[0].capitalize! + + words.map! { |e| + case + when e == "i" then e.capitalize + when e == "and" || e == "the" then e #do nothing + when e.length <= 2 then e #do nothing + else e.capitalize + end + } + + 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..86c8f35f5 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,18 @@ +class Timer + attr_accessor :seconds + + def initialize + @seconds = 0 + end + + def time_string + h = (seconds/3600).to_s.rjust(2, '0') + m = ( (seconds%3600)/60 ).to_s.rjust(2, '0') + s = ( (seconds%3600)%60 ).to_s.rjust(2, '0') + h + ":" + m + ":" + s + end + + def padded(n) + "%02d" % n + end +end \ No newline at end of file diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index 40b33f23f..0ae1dd31f 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -45,16 +45,16 @@ # 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..859e5c9be --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,45 @@ +class Temperature + + def initialize(options) + @temp = options.fetch(:f) if options.has_key?(:f) + @temp ||= self.class.ctof(options.fetch(:c,0)) + end + + def in_fahrenheit + @temp + end + + def in_celsius + self.class.ftoc(@temp) + end + + def self.from_celsius(c) + self.new({ c: c }) + end + + def self.from_fahrenheit(f) + self.new({ f: f }) + end + + def self.ftoc(f) + ((f-32)/1.8).round(2) + end + + def self.ctof(c) + (c*1.8 + 32).round(2) + end + +end + +class Celsius < Temperature + def initialize(c) + super({ c: c }) + end +end + +class Fahrenheit < Temperature + def initialize(f) + super({ f: f }) + end +end + diff --git a/10_temperature_object/temperature_object_spec.rb b/10_temperature_object/temperature_object_spec.rb index 5c0af8685..4911824ec 100644 --- a/10_temperature_object/temperature_object_spec.rb +++ b/10_temperature_object/temperature_object_spec.rb @@ -98,6 +98,14 @@ # describe "utility class methods" do + it "converts fahrenheit to celsius" do + expect(Temperature.ftoc(32)).to eq(0) + end + + it "converts celsius to faharenheit" do + expect(Temperature.ctof(0)).to eq(32) + end + end # Here's another way to solve the problem! diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..ddbc96afe --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,30 @@ +class Dictionary + attr_accessor :entries + + def initialize + @entries = Hash.new(nil) + end + + def add(entry) + @entries.merge!(entry) if entry.is_a? Hash + @entries[entry] = nil if entry.is_a? String + end + + def keywords + @entries.keys.sort + end + + def include?(word) + @entries.include?(word) + end + + def find(key) + @entries.select { |word, definition| word.match(key) } + end + + def printable + keywords.map do |keyword| + "[#{keyword}] \"#{@entries[keyword]}\"\n" + end.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..4dd30f490 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,62 @@ +class RPNCalculator + def initialize + @stack = [] + end + + def push(n) + @stack << n + end + + def plus + @stack.push(evaluate + evaluate) + end + + def minus + @stack.push(-evaluate + evaluate) + end + + def divide + @stack.push(1.0/evaluate * evaluate) + end + + def times + @stack.push(evaluate * evaluate) + end + + def value + @stack[-1] + end + + def pop + raise "calculator is empty" if @stack.empty? + @stack.pop + end + + def tokens(string) + string.split(" ").map do |x| + if x.match(/[0-9]+/) + x.to_i + elsif x.match(/[*+-\/]/) + x.to_sym + end + end + end + + def evaluate(stack = nil) + @stack = tokens(stack) unless stack.nil? + + elem = pop + if elem.is_a? Symbol + case elem + when :+ then plus + when :- then minus + when :/ then divide + when :* then times + else 0 + end + return pop + else + return elem + end + end +end diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..4724267da --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,13 @@ +class Array + def sum + self.inject(0,:+) + end + + def square + self.map { |e| e*e } + end + + def square! + self.map! { |e| e*e } + 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..5f077fd82 --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,73 @@ +class Fixnum + def in_words + case + when self >= 1_000_000_000_000_000 then raise "Sorry, number too high!" + when self >= 1_000_000_000_000 then hundreds_and_up(12) + when self >= 1_000_000_000 then hundreds_and_up(9) + when self >= 1_000_000 then hundreds_and_up(6) + when self >= 1000 then hundreds_and_up(3) + when self >= 100 then hundreds_and_up(2) + when self/10 > 1 #tens + return tens(self/10) if self%10 == 0 + return tens(self/10) + " " + ones(self%10) + when self/10 == 1 # teens + return 'ten' if self == 10 + return 'eleven' if self == 11 + return 'twelve' if self == 12 + return teen_beginnings(self%10) + 'teen' + when self/10 == 0 # ones + return ones(self) + when self < 0 + return "negative " + (self.abs).in_words + else + return "Unknown number" + end + end + + private + + def descriptor(exp) + return " trillion" if exp == 12 + return " billion" if exp == 9 + return " million" if exp == 6 + return " thousand" if exp == 3 + return " hundred" + end + + def hundreds_and_up(exp) + return (self / (10**exp) ).in_words + descriptor(exp) if self%(10**exp) == 0 + return (self/ (10**exp) ).in_words + descriptor(exp) + " " + (self%(10**exp)).in_words + end + + def ones(n) + return 'zero' if n == 0 + return 'one' if n == 1 + return 'two' if n == 2 + return 'three' if n == 3 + return 'four' if n == 4 + return 'five' if n == 5 + return 'six' if n == 6 + return 'seven' if n == 7 + return 'eight' if n == 8 + return 'nine' if n == 9 + end + + def teen_beginnings(n) + return 'thir' if n == 3 + return 'four' if n == 4 + return 'fif' if n == 5 + return 'eigh' if n == 8 + return ones(n) + end + + def tens(n) + return 'twenty' if n == 2 + return 'thirty' if n == 3 + return 'forty' if n == 4 + return 'fifty' if n == 5 + return 'sixty' if n == 6 + return 'seventy' if n == 7 + return 'eighty' if n == 8 + return 'ninety' if n == 9 + end +end \ No newline at end of file