diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..efe573ea2 Binary files /dev/null and b/.DS_Store differ diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..97176ed02 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,6 @@ +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..a083e4356 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,15 @@ +def ftoc(temp) + temp = temp.to_f + temp -= 32 + temp *= 5 + temp /= 9 + temp +end + +def ctof (temp) + temp = temp.to_f + temp *= 9 + temp /= 5 + temp += 32 + temp +end diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..d5957ab4f --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,31 @@ +def add(x, y) + x + y +end + +def subtract(x, y) + x - y +end + +def sum(array) + if array.length == 0 + 0 + else + array.inject{|result, n| result + n} + end +end + +def multiply(array) + array.inject{|result, n| result * n} +end + +def power(x, y) + x ** y +end + +def factorial(number) + if number == 0 + return 1 + else + (1..number).inject{|result, n| result * n} + end +end \ No newline at end of file diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..a2dd1a8e2 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -79,21 +79,38 @@ describe "#multiply" do - it "multiplies two numbers" - - it "multiplies several numbers" - + it "multiplies two numbers" do + expect(multiply([3,7])).to eq(21) + end + it "multiplies several numbers" do + expect(multiply([3,7,2])).to eq(42) + 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 + it "returns 1 if exponent is 0" do + expect(power(1,0)).to eq(1) + 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..83e25ab0f --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,38 @@ +def echo(string) + string +end + +def shout(string) + string.upcase +end + +def repeat(string, n = 2) + new_string = (string + " ") * n + new_string.strip! +end + +def start_of_word(string, n) + letters = string.split '' + letters[0..(n-1)].join '' +end + +def first_word(string) + words = string.split ' ' + words[0] +end + +def titleize(string) + words = string.split ' ' + little_words = ["and", "the", "over"] + if little_words.include? words[0] + words[0][0] = words[0][0].upcase + end + words.each do |word| + if little_words.include? word + next + else + word[0] = word[0].upcase + end + end + words.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..81024ebbd --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,21 @@ +def translate(string) + translation = '' + vowels = ["a", "e", "i", "o", "u"] + words = string.split ' ' + words.each do |word| + letters = word.split '' + until vowels.include? letters[0] + letter = letters.shift + letters.push letter + end + #if next letter is q, checks end of word for u and shifts it if necessary + if letters[0] == "u" && letters[-1] == "q" + letter = letters.shift + letters.push letter + end + letters.push "ay" + translation += (letters.join '') + ' ' + end + translation.strip! +end +translate("quiet") \ 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..1b9997337 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,16 @@ +def reverser + words = yield.split ' ' + words.each do |word| + word.reverse! + end + words.join ' ' +end + +def adder(add=1) + yield + add +end + +def repeater(repeat=1) + repeat.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..c0d753eca --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,7 @@ +def measure(n=1) + start = Time.now + n.times do + yield + end + (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..4a9b76961 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(name = nil) + if name == nil + "Hello!" + else + "Hello, #{name}!" + end + end +end \ No newline at end of file diff --git a/08_book_titles/.DS_Store b/08_book_titles/.DS_Store new file mode 100644 index 000000000..070ec7597 Binary files /dev/null and b/08_book_titles/.DS_Store differ diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb new file mode 100644 index 000000000..ff6bcd6e8 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,19 @@ +class Book + attr_accessor :title + + def title + words = @title.split ' ' + articles = ["the", "a", "an", "and", "in", "of"] + if articles.include? words[0] + words[0][0] = words[0][0].upcase + end + words.each do |word| + if articles.include? word + next + else + word[0] = word[0].upcase + end + 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..bd1d054ec --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,35 @@ +class Timer + attr_accessor :seconds + + def initialize + @seconds = 0 + end + + def time_string + "#{padded(hours)}:#{padded(minutes)}:#{padded(seconds)}" + end + + def hours + hours = @seconds / 3600 + end + + def minutes + minutes = (@seconds - hours * 3600) / 60 + end + + def seconds + seconds = @seconds % 60 + end + + def padded(number) + number = number.to_s + digits = number.length + if digits == 0 + "00" + elsif digits == 1 + "0" + number + else + number + end + end +end \ No newline at end of file diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index 40b33f23f..39bcca573 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -45,16 +45,17 @@ # 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/.DS_Store b/10_temperature_object/.DS_Store new file mode 100644 index 000000000..1aa1a5e99 Binary files /dev/null and b/10_temperature_object/.DS_Store differ diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..6ae229cdb --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,42 @@ +class Temperature + #converts options hash into C° and F° values + def initialize(options = {}) + @degreesf, @degreesc = options[:f], options[:c] + + if @degreesf != nil && @degreesc == nil + @degreesc = (@degreesf - 32) * 5.0 / 9.0 + elsif @degreesc != nil && @degreesf == nil + @degreesf = (@degreesc * 9.0 / 5.0) + 32 + end + end + #returns converted input as F° + def in_fahrenheit + @degreesf + end + #returns converted input as C° + def in_celsius + @degreesc + end + #Factory method for inputs from C° + def self.from_celsius(temperature) + Temperature.new(:c => temperature) + end + #Factory method for inputs from F° + def self.from_fahrenheit(temperature) + Temperature.new(:f => temperature) + end +end +#Temperature subclass to convert input to C° +class Celsius < Temperature + def initialize(temperature) + @degreesc = temperature + @degreesf = (temperature * 9.0 / 5.0) + 32 + end +end +#Temperature subclass to convert input to F° +class Fahrenheit < Temperature + def initialize(temperature) + @degreesf = temperature + @degreesc = (temperature - 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..0da3d60a7 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,51 @@ +class Dictionary + attr_accessor :entries + + #creates empty hash for entries + def initialize + @entries = {} + end + + #adds entries with key and value(if given) + def add(input) + if input.class == String + @entries[input] = nil + else + @entries.merge!(input) + end + end + + #returns keys alphabetically + def keywords + @entries.keys.sort + end + + #checks whether or not a given key is in @entries + def include?(word) + if @entries.keys.include?(word) + true + else + false + end + end + + #finds keys and parts of keys and returns all relevant entries + def find(search) + results = {} + @entries.each do |key, value| + if key.start_with? search + results[key] = value + end + end + results + end + + #sorts and prints @entries in [key] "value" format + def printable + printed = @entries.sort.map do |key,value| + "[#{key}] \"#{value}\"" + end + printed.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..e79db13ed --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,88 @@ +class RPNCalculator + attr_accessor :calculator + #sets array for operands awaiting operations + def initialize + @operands = [] + end + #adds operands to array + def push(n) + @operands.push n + end + #returns total of operations + def value + @operands[-1] + end + #adds operands when possible, otherwise raises error + def plus + if @operands.size>= 2 + @operands.push (@operands.pop + @operands.pop) + else + raise "calculator is empty" + end + end + #subtracts operands when possible, otherwise raises error + def minus + if @operands.size>= 2 + new_n = (@operands[-2] - @operands[-1]) + @operands.pop(2) + @operands.push new_n + else + raise "calculator is empty" + end + end + #divides operands when possible, otherwise raises error + def divide + if @operands.size>= 2 + new_n = (@operands[-2].to_f / @operands[-1].to_f) + @operands.pop(2) + @operands.push new_n + else + raise "calculator is empty" + end + end + #multiplies operands when possible, otherwise raises error + def times + if @operands.size>= 2 + new_n = (@operands[-2] * @operands[-1]) + @operands.pop(2) + @operands.push new_n + else + raise "calculator is empty" + end + end + #evaluates a string and turns components into operators or operands + 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 + #evaluats tokenized string + 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 + + + + diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..f0b1711d2 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,13 @@ +class Array + def sum + self.inject(0){|sum,x| sum + x} + end + + def square + self.map {|x| x * x} + end + + def square! + self.map! {|x| x * x} + 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..ef5560e2f --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,81 @@ +class Fixnum + def in_words(number = 0) + number = self + if number == 0 + return 'zero' + end + + number_phrase = '' + ones = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] + tens = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] + teens = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + left_over = number + + calculating = left_over / 1000000000000 + left_over = left_over - calculating * 1000000000000 + if calculating > 0 + trillions = calculating.in_words + number_phrase = number_phrase + trillions + ' trillion' + if left_over > 0 + number_phrase = number_phrase + ' ' + end + end + calculating = left_over / 1000000000 + left_over = left_over - calculating * 1000000000 + if calculating > 0 + billions = calculating.in_words + number_phrase = number_phrase + billions + ' billion' + if left_over > 0 + number_phrase = number_phrase + ' ' + end + end + calculating = left_over / 1000000 + left_over = left_over - calculating * 1000000 + if calculating > 0 + millions = calculating.in_words + number_phrase = number_phrase + millions + ' million' + if left_over > 0 + number_phrase = number_phrase + ' ' + end + end + calculating = left_over / 1000 + left_over = left_over - calculating * 1000 + if calculating > 0 + thousands = calculating.in_words + number_phrase = number_phrase + thousands + ' thousand' + if left_over > 0 + number_phrase = number_phrase + ' ' + end + end + calculating = left_over / 100 + left_over = left_over - calculating * 100 + if calculating > 0 + hundreds = calculating.in_words + number_phrase = number_phrase + hundreds + ' hundred' + if left_over > 0 + number_phrase = number_phrase + ' ' + end + end + calculating = left_over / 10 + left_over = left_over - calculating * 10 + if calculating > 0 + if ((calculating == 1) and (left_over > 0)) + number_phrase = number_phrase + teens[left_over-1] + left_over = 0 + else + number_phrase = number_phrase + tens[calculating-1] + end + + if left_over > 0 + number_phrase = number_phrase + ' ' + end + end + calculating = left_over + left_over = 0 + + if calculating > 0 + number_phrase = number_phrase + ones[calculating-1] + end + number_phrase + end +end \ No newline at end of file