diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..b7be55eb7 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..52008fc13 --- /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..80cbbdb24 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(temp) + (temp - 32) * 5.0 / 9.0 +end + +def ctof(temp) + temp * 9 / 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..eff4af732 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,27 @@ +def add(num1, num2) + num1 + num2 +end + +def subtract(num1, num2) + num1 - num2 +end + +def sum(arr) + return 0 if arr.empty? + arr.inject(:+) +end + +def multiply(arr) + return 0 if arr.empty? + arr.inject(:*) +end + +def power(num1, pow) + num1**pow +end + +def factorial(num) + return 0 if num == 0 + fac = [*1..num] + fac.inject(:*) +end \ No newline at end of file diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..1096621ff 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([10,4])).to eq(40) + end - it "multiplies several numbers" - + it "multiplies several numbers" do + expect(multiply([10,4,2])).to eq(80) + 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(10,2)).to eq(100) + 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(0) + 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/.DS_Store b/03_simon_says/.DS_Store new file mode 100644 index 000000000..7c71df58d Binary files /dev/null and b/03_simon_says/.DS_Store differ diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..82c821a23 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,39 @@ +def echo(word) + word +end + +def shout(word) + word.upcase +end + +def repeat(word, t=2) + result = word + (t-1).times do + result = result + " " + word + end + result +end + +def start_of_word(word, num) + word.slice(0, num) +end + +def first_word(phrase) + phrase.split(" ")[0] +end + +def titleize(phrase) + result = "" + phrase.split(" ").each_with_index do |word, index| + if index == 0 + result = result + word.capitalize + " " + else + if word == "and" || word == "the" || word == "over" + result = result + word + " " + else + result = result + word.capitalize + " " + end + end + end + result.strip +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..008135194 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,19 @@ +def translate(words) + vowels = ["a", "e", "i", "o", "u"] + result = "" + words.split(" ").each do |word| + if vowels.include?(word[0]) + result += word + "ay" + " " + else + while !vowels.include?(word[0]) + if word.slice(0, 2) == "qu" + word = word.slice(2, word.length) + word.slice(0, 2) + else + word = word.slice(1, word.length) + word[0] + end + end + result += word + "ay" + " " + end + end + result.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..66316c8f3 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,14 @@ +def reverser + result = yield.split(" ").map { |word| word.reverse } + result.join(" ") +end + +def adder(num = 1) + yield + num +end + +def repeater(num = 1) + num.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..d933df63a --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,6 @@ +def measure(num = 1) + beg_time = Time.now + num.times { yield } + finish_time = Time.now + (finish_time - beg_time) / num +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..1cd3936de --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(who = nil) + if who + "Hello, #{who}!" + else + "Hello!" + 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..f26ba563a --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,12 @@ +class Book + def initialize + @title = '' + @untitlized_words = ['a', 'an', 'of', 'in', 'and', 'the', 'over'] + end + def title=(value) + @title = value.split(' ') + end + def title + @title.map.with_index { |word, index| @untitlized_words.include?(word) && index > 0 ? word : word.capitalize }.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..55694112c --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,14 @@ +class Timer + def initialize + @seconds = 0 + end + def seconds + @seconds + end + def seconds=(value) + @seconds = value + end + def time_string + [@seconds / 3600, @seconds / 60 % 60, @seconds % 60].map { |t| t.to_s.rjust(2, '0') }.join(':') + 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..19dbeddf8 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,31 @@ +class Temperature + def initialize(options = {}) + @f = options[:f] + @c = options[:c] + end + def in_fahrenheit + return @f if @f + @c * 9 / 5.0 + 32 + end + def in_celsius + return @c if @c + (@f - 32) * 5 / 9.0 + end + def self.from_celsius(c) + Temperature.new(:c => c) + end + def self.from_fahrenheit(f) + Temperature.new(:f => f) + end +end + +class Celsius < Temperature + def initialize(t) + super(:c => t) + end +end +class Fahrenheit < Temperature + def initialize(t) + super(:f => t) + 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..9ad2a5012 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,33 @@ +class Dictionary + def initialize + @entries = {} + end + def entries + @entries + end + def add(entry) + return @entries.merge!(entry) if entry.is_a?(Hash) + @entries[entry] = nil + end + def keywords + @entries.keys.sort + end + def include?(value) + keywords.include?(value) + end + def find(word) + results = {} + @entries.each do |key, value| + results[key] = value if key.match(/#{word}/) + end + results + end + def printable + s = '' + keywords.each do |key| + s += "[#{key}] \"#{@entries[key]}\"" + s += "\n" if keywords.index(key) != keywords.length - 1 + end + s + 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..a9cb1941a --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,66 @@ +class RPNCalculator + attr_accessor :stack + attr_accessor :value + def initialize + @stack = [] + @value = 0 + end + def push(num) + @stack.push(num) + end + def operate + error_empty if @stack.length < 2 + second = @stack.pop + first = @stack.pop + new_value = yield(second, first) + @stack.push(new_value) + @value = new_value + end + def plus + operate { |second, first| first + second } + @value + end + def minus + operate { |second, first| first - second } + @value + end + def divide + operate { |second, first| first.to_f / second.to_f } + @value + end + def times + operate { |second, first| first * second } + @value + end + def error_empty + raise "calculator is empty" + end + def tokens(string) + string.split(" ").map do |token| + if token.match(/\d/) + token.to_i + elsif ['+', '-', '*', '/'].include?(token) + token.to_sym + end + end + end + def evaluate(string) + tokens(string).each do |token| + if token.is_a?(Integer) + push(token) + else + case token + when :+ + plus + when :- + minus + when :* + times + when :/ + divide + end + end + end + @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..dcabcc5de --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,12 @@ +class Array + def sum + return 0 if self.empty? + self.inject(:+) + end + def square + self.map { |e| e**2 } + end + def square! + self.map! { |e| e**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..94e7e45cc --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,42 @@ +class Fixnum + def in_words + words_map = {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', 14 => 'fourteen', 15 => 'fifteen', 16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen', 19 => 'nineteen', 20 => 'twenty', 30 => 'thirty', 40 => 'forty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety'} + if self == 0 + return "zero" + end + words = "" + number = self + if number / 1_000_000_000_000 > 0 + words += (number / 1_000_000_000_000).in_words + " trillion " + number %= 1_000_000_000_000 + end + if number / 1_000_000_000 > 0 + words += (number / 1_000_000_000).in_words + " billion " + number %= 1_000_000_000 + end + if number / 1_000_000 > 0 + words += (number / 1_000_000).in_words + " million " + number %= 1_000_000 + end + if number / 1000 > 0 + words += (number / 1000).in_words + " thousand " + number %= 1000 + end + if number / 100 > 0 + words += (number / 100).in_words + " hundred " + number %= 100 + end + if number > 0 + if number < 20 + words += words_map[number] + else + words += words_map[(number.to_s[0].to_i) * 10] + if number % 10 > 0 + words += " " + words_map[number%10] + end + end + end + words.strip + end +end +