diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..d6fa1f7cb --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(someone) + "Hello, #{someone}!" +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..79d693cf8 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,10 @@ +#farenheigh to celsius +def ftoc(temperature) + celsius = (temperature.to_f - 32.0) * (5.0/9.0) +end + + +#celsius to fahrenheit +def ctof(temperature) + fahrenheight = (temperature.to_f * (9.0/5.0)) + 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..08b0c74b2 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,35 @@ +# add method +def add(first, second) + first + second +end + +#subtract method +def subtract(first, second) + first - second +end + +#sum method +def sum(array) + sum = 0 + array.each { |x| sum += x } + sum +end + +#multiply method +def multiply(*numbers) + product = 1 + numbers.each { |x| product *= x} + product +end + +#power method +def power(a,b) + a ** b +end + +#factorial method +def factorial(n) + product = 1 + (n).downto(1).each { |x| product *= x } + product +end diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..88273468b 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -78,22 +78,40 @@ # write tests and code for the following: describe "#multiply" do + it "multiplies two numbers" do + expect(multiply(5,7)).to eq(35) + end - it "multiplies two numbers" - - it "multiplies several numbers" - + it "multiplies several numbers" do + expect(multiply(5,84,32,6)). to eq(80640) + 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(4,3)). to eq(64) + 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/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..b876cb7d0 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,34 @@ +def translate(input) + +alphabet = ("a".."z").to_a +vowels = ["a","e","i","o","u"] +consonants = alphabet - vowels +words = input.split(" ") + + + + words.map do |word| + + #word begins with vowel + if vowels.include?(word[0]) + word << "ay" + #word begins with 2 consonants + elsif (consonants.include?(word[0]) && consonants.include?(word[1]) && consonants.include?(word[2])) || word[1..2].include?("qu") + add = word[0..2] + "ay" + word.delete!(word[0..2]) + word << add + elsif (consonants.include?(word[0]) && consonants.include?(word[1])) || word[0..1].include?("qu") + add = word[0..1] + "ay" + word.delete!(word[0..1]) + word << add + #word begins with 1 consonant + elsif consonants.include?(word[0]) + add = word[0] + "ay" + word.delete!(word[0]) + word << add + end + end + +words = words.join" ".to_s +end + diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index cc659edfd..8c5f05ad5 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -65,6 +65,16 @@ expect(s).to eq("ethay ickquay ownbray oxfay") end + it "retains capitalized words" do + s = translate("The Quick Brown Fox") + expect(s).to eq("eThay ickQuay ownBray oxFay") + end + + it "retains punctuation" do + s = translate("something 'here' should still translate...!") + expect(s).to eq("omethingsay 'erehay' ouldshay illstay anslatetray...!") + end + # 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 diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..c7aaf198d --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,13 @@ +def reverser + words = yield.split" " + words.map {|word| word.reverse }.join(" ") +end + +def adder(n=1) + yield(n) + 1 + (n-1) +end + +def repeater(number=1) + number.times {|x| yield } +end + diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..f6a2ccbec --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,5 @@ +def measure(n=1) + start_time = Time.now + n.times {|n| yield } + (Time.now - start_time) / 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..9796bdc5e --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,11 @@ +class Friend + +def greeting(someone=nil) + if someone == nil + "Hello!" + else + "Hello, #{someone}!" + 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..df8d68c52 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,18 @@ +class Book + +attr_accessor :title + +def title + @title = @title.split(" ") + articles = ["the","a","and","in","of","an"] + @title.map do |word| + word[0] = word[0].capitalize unless articles.include?(word) + end + @title[0] = @title[0].capitalize + @title.join(" ") +end + + +end + + diff --git a/09_timer/timer.rb b/09_timer/timer.rb new file mode 100644 index 000000000..8a3df7a58 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,15 @@ +class Timer + +attr_accessor :seconds + +def seconds + 0 +end + +def time_string + default = Time.at(0)+18000 + @seconds + (default + @seconds).strftime("%H:%M:%S") +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..1a5561b32 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,50 @@ +class Temperature + +def initialize(values) + @f = values[:f] + @c = values[:c] + @c.nil? ? @c = Temperature.ftoc(@f) : @f = Temperature.ctof(@c) +end + + +def in_fahrenheit + @f +end + +def in_celsius + @c +end + +#factory methods +def self.from_celsius(values) + Temperature.new(:c => values) +end + +def self.from_fahrenheit(values) + Temperature.new(:f => values) +end + +#test-driving bonus: +def self.ftoc(values) + (values - 32) * (5.0/9.0) +end + +def self.ctof(values) + (values * (9.0/5.0)) + 32.0 +end +end + + +#subclasses + +class Celsius < Temperature + def initialize(values) + super(:c => values) + end +end + +class Fahrenheit < Temperature + def initialize(values) + super(:f => values) + 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..60c3ebc40 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,48 @@ +class Dictionary + +def initialize + @entries = {} +end + +def entries + @entries +end + +def add(entry, value=nil) + if entry.is_a?Hash + entry.each do |word, definition| + @entries[word] = definition + end + elsif entry.is_a?String + @entries[entry] = nil + end +end + +def keywords + @entries.keys.sort +end + +def include?(word) + keywords.include?(word) +end + +def find(word) + result = {} + @entries.each do |key, value| + if key.start_with?(word) + result[key] = value + end + end + result +end + +def printable + print = @entries.sort.map do |key, value| + "[#{key}] \"#{value}\"" + end + print.join"\n" +end + + +#class ends here +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..782e10b16 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,55 @@ +class RPNCalculator + +def initialize + @numbers = [] +end + +def push(value) + @numbers << value.to_f +end + +def calc(operation) + raise "calculator is empty" if @numbers.size < 2 + calculation = @numbers[-2].send(operation, @numbers[-1]) + @numbers.pop(2) + @numbers << calculation +end + +def plus + calc(:+) +end + +def minus + calc(:-) +end + +def divide + calc(:/) +end + +def times + calc(:*) +end + +def tokens(string) + string.split(" ").map do |item| + item =~ /[\+\-\*\/]/ ? item.to_sym : item.to_f + end +end + +def evaluate(string) + tokens(string).each do |item| + item.is_a?(Float) ? @numbers<" + xml << "\n" if @indent + @indent_level += 1 + xml << yield + @indent_level -= 1 + xml << (" " * @indent_level) if @indent + xml << "" + xml << "\n" if @indent + else + xml << "/>" + xml << "\n" if @indent + end + xml + 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..250239301 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,18 @@ +class Array + +def sum + self.reduce(0, :+) +end + +def square + return [] if self.empty? + self.map {|x| x * x} +end + +def square! + return [] if self.empty? + self.map! {|x| x * x} +end + + +end \ No newline at end of file