diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..c285c09f5 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..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..ff3f0055e --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,9 @@ +def ftoc(fahrenheit) + c = (fahrenheit - 32).to_f * 5/9 + return c +end + +def ctof(celcius) + f = (celcius.to_f * 9/5) + 32 + return f +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..aa8ec5479 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,37 @@ +def add(a,b) + a + b +end + +def subtract(a,b) + a - b +end + +def sum(array) + sum = 0 + array.each do |i| + sum += i + end + sum +end + +def multiply(array) + product = 1 + array.each do |i| + product *= i + end + product +end + +def power(a,b) + a**b +end + +def factorial(n) + if n == 1 + return 1 + elsif n == 0 + return 0 + else + n * factorial(n - 1) + end +end \ No newline at end of file diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..4e9f731ba 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([2,8])).to eq(16) + end - it "multiplies several numbers" + it "multiplies several numbers" do + expect(multiply([2,5,3,4])).to eq(120) + 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(5,3)).to eq(125) + 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/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..79fd0126a --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,40 @@ +def echo(str) + str +end + +def shout(str) + str.upcase +end + +def repeat(str, num = 2) + phrase = "" + until num == 0 + phrase += str + phrase += " " + num -= 1 + end + phrase.strip +end + +def start_of_word(word,num_letters) + word.slice(0, num_letters) +end + +def first_word(str) + str.split(' ').first +end + +def titleize(str) + small_word = ['and','over','the'] + str_array = str.split(' ') + str_array.each_with_index do |i, index| + if index == 0 + i.capitalize! + elsif small_word.include?(i) + i + else + i.capitalize! + end + end + str_array.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..df79c1348 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,61 @@ +require 'pry' +def translate(str) + + str.downcase! + # split string into words + words = split_str(str) + + # split word into letters + letters = split_words(words) + + # until reach a vowel (unless qu), unshift consonants + # and push those consonants to an end_of_word array + # append the end_of_word array to the array + # append the 'ay' array to the array + pig_latinned_arrays = move_consonants(letters) + + # join the letters array + # join the word array + # return the array + pig_latinned_string = join_arrays(pig_latinned_arrays) + return pig_latinned_string + +end + +def split_str(str) + str.split(' ') +end + +def split_words(words) + letters = words.map do |word| + word.split('') + end + letters +end + +def move_consonants(letters) + vowels = ['a','e','i','o','u'] + ay = ['a','y'] + letters.each do |word| + consonants_array = [] + until vowels.include?(word[0]) + if word[0] == 'q' && word[1] == 'u' + 2.times do |i| + consonants_array << word.shift + end + else + consonants_array << word.shift + end + end + word.concat(consonants_array).concat(ay) + end + letters +end + +def join_arrays(pig_latinned_arrays) + joined_words = [] + pig_latinned_arrays.each do |word| + joined_words << word.join('') + end + joined_words.join(' ') +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..716edea94 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,19 @@ +def reverser + split_string = yield.split(' ') + split_string.map do |word| + word.reverse! + end + result = split_string.join(' ') + result +end + +def adder(num = 1) + result = yield + num + result +end + +def repeater(num = 1) + num.times do |i| + 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..4e8edb5b1 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,8 @@ +def measure(num_loops = 1) + start_time = Time.now + num_loops.times do + yield + end + end_time = Time.now + (end_time - start_time) / num_loops +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..68c4506ca --- /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..8465db206 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,21 @@ +class Book + + def title=(title) + lowercase_words = ['the','a','an','and','in','of'] + word_array = title.split(' ') + word_array.map do |word| + if lowercase_words.include?(word) + word + else + word.capitalize! + end + end + word_array[0].capitalize! + @title = word_array.join(' ') + end + + def title + @title + 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..55a0f2a53 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,46 @@ + +# what we're trying to do here is create an object "Timer" with a seconds method that takes the number of seconds elapsed, and a time_string method that returns in hour: minute: second format + +require 'pry' + +class Timer + + def initialize(s = 0, m = 0, h = 0) + @seconds = s + @minutes = m + @hours = h + end + + def seconds + @seconds + end + + def seconds=(number) + @seconds = number + end + + def time_string + calculate_clock_time + make_time_read_friendly + end + +end + +def calculate_clock_time + if @seconds > 60 + @minutes = @seconds / 60 + @seconds -= @minutes * 60 + end + if @minutes > 60 + @hours = @minutes / 60 + @minutes -= @hours * 60 + end +end + +def make_time_read_friendly + time_array = [@hours.to_s,@minutes.to_s,@seconds.to_s] + time_array.map do |time| + time.prepend('0') until time.length == 2 + end + time_array.join(':') +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..b5c3ede1f --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,45 @@ +require 'pry' +class Temperature + + def initialize(temperatures = {:f => nil, :c => nil }) + @fahrenheit = temperatures[:f] + @celsius = temperatures[:c] + end + + def in_fahrenheit + if @fahrenheit + @fahrenheit + else + (@celsius.to_f * 9/5).to_f + 32.to_f + end + end + + def in_celsius + if @celsius + @celsius + else + (@fahrenheit.to_f - 32).to_f * 5/9.to_f + end + end + + def self.from_celsius(c) + self.new(:c => c) + end + + def self.from_fahrenheit(f) + self.new(:f => f) + end + +end + +class Celsius < Temperature + def initialize(c) + @celsius = c + end +end + +class Fahrenheit < Temperature + def initialize(f) + @fahrenheit = f + end +end diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..b8196efe2 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,59 @@ +require 'pry' + +class Dictionary + + def initialize + @entries = {} + end + + def entries + @entries + end + + def add(new_entries) + if new_entries.class == String + @entries[new_entries] = nil + else + new_entries.each do |word, definition| + @entries[word] = definition + end + end + end + + def keywords + @entries.keys.sort + end + + def include?(keyword) + return true if @entries.keys.include?(keyword) + false + end + + def find(string) + if @entries.keys.grep(/#{string}/).empty? + {} + else + key_array = [] + @entries.keys.each do |key| + if key =~ /#{string}/ + key_array << key + end + end + result = { } + key_array.each do |key| + result.store(key,@entries[key]) + end + result + end + end + + def printable + print_version = "" + @entries.sort.each do |key, value| + print_version += "[#{key}] \"#{value}\"\n" + end + print_version.chomp + end + +end + diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..b42a8b24b --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,93 @@ +require 'pry' + +class RPNCalculator + + def initialize + @int_stack = [] + end + + def push(n) + @int_stack << n + end + + def plus + if @int_stack.size >= 2 + num_1 = @int_stack.pop + num_2 = @int_stack.pop + num_3 = num_1 + num_2 + @int_stack.push(num_3) + else + raise "calculator is empty" + end + end + + def minus + if @int_stack.size >= 2 + num_1 = @int_stack.pop + num_2 = @int_stack.pop + num_3 = num_2 - num_1 + @int_stack.push(num_3) + else + raise "calculator is empty" + end + end + + def divide + if @int_stack.size >= 2 + num_1 = @int_stack.pop.to_f + num_2 = @int_stack.pop.to_f + num_3 = num_2 / num_1 + @int_stack.push(num_3) + else + raise "calculator is empty" + end + end + + def times + if @int_stack.size >= 2 + num_1 = @int_stack.pop.to_f + num_2 = @int_stack.pop.to_f + num_3 = num_1 * num_2 + @int_stack.push(num_3) + else + raise "calculator is empty" + end + end + + def tokens(str) + math_operators = ['+','*','-','/'] + split_str = str.split(' ') + split_str.map! do |i| + if math_operators.include?(i) + i.to_sym + else + i.to_i + end + end + split_str + end + + def evaluate(str) + token_array = tokens(str) + token_array.each do |i| + case i + when :* + times + when :+ + plus + when :- + minus + when :/ + divide + else + push(i) + end + end + value + end + + def value + @int_stack[-1] + 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..aaac07cc3 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,24 @@ +class Array + + def sum + result = 0 + self.each do |i| + result += i + end + result + end + + def square + result = [] + self.map do |i| + result << i * i + end + result + end + + def square! + self.map! do |i| + i * i + 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..6e99487a9 --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,143 @@ +require 'pry' + +class Fixnum + + def in_words + @@num_string = "" + num = self + @@num_array = num.to_s.delete('_').split('') + size_checker + end + + private + + def size_checker + case @@num_array.length + when 1 + ones + when 2 + tens + when 3 + hundreds + when 4..6 + thousands + when 7..9 + + millions + when 10..12 + billions + when 13..15 + trillions + end + size_checker until @@num_array.empty? + @@num_string.strip + end + + def ones + ones_hash = { 0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine' } + @@num_string += " " + ones_hash[@@num_array.shift.to_i] + end + + def tens + if @@num_array.slice(0,2).join('').to_i >= 20 + above_twenty + else + teens + end + end + + def teens + teens_hash = { 10 => 'ten', 11 => 'eleven', 12 => 'twelve', 13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen', 16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen', 19 => 'nineteen'} + @@num_string += " " + teens_hash[@@num_array.slice(0,2).join('').to_i] + @@num_array.shift(2) + end + + def above_twenty + tens_hash = { 20 => 'twenty', 30 => 'thirty', 40 => 'forty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety'} + if @@num_array.slice(0,2).join('').to_i % 10 != 0 + n = @@num_array.shift + "0" + @@num_string += " " + tens_hash[n.to_i] + else + @@num_string += " " + tens_hash[@@num_array.slice(0,2).join('').to_i] + @@num_array.shift(2) + end + end + + def hundreds + hundreds_hash = { 100 => 'one hundred', 200 => 'two hundred', 300 => 'three hundred', 400 => 'four hundred', 500 => 'five hundred', 600 => 'six hundred', 700 => 'seven hundred', 800 => 'eight hundred', 900 => 'nine hundred'} + if @@num_array.slice(0,3).join('').to_i % 100 != 0 + n = @@num_array.shift + "00" + @@num_string += " " + hundreds_hash[n.to_i] + else + @@num_string += hundreds_hash[@@num_array.join('').to_i] + @@num_array.shift(3) + end + end + + def thousands + while @@num_array.length >= 4 + if @@num_array.length == 4 + ones + elsif @@num_array.length == 5 + tens + else + hundreds + end + end + @@num_string += " " + 'thousand' + while @@num_array[0] == "0" + + @@num_array.delete_at(0) + end + end + + def millions + while @@num_array.length >= 7 + if @@num_array.length == 7 + ones + elsif @@num_array.length == 8 + tens + else + hundreds + end + end + @@num_string += " " + 'million' + while @@num_array[0] == "0" + @@num_array.delete_at(0) + end + end + + def billions + while @@num_array.length >= 10 + if @@num_array.length == 10 + ones + elsif @@num_array.length == 11 + tens + else + hundreds + end + end + @@num_string += " " + 'billion' + while @@num_array[0] == "0" + @@num_array.delete_at(0) + end + end + + def trillions + while @@num_array.length >= 13 + if @@num_array.length == 13 + ones + elsif @@num_array.length == 14 + tens + else + hundreds + end + end + @@num_string += " " + 'trillion' + while @@num_array[0] == "0" + @@num_array.delete_at(0) + end + end + +end +