diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..503aede1e --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(name) + "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..324d5c39a --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(fahrenheit) + celcius = (fahrenheit - 32) * (5.0/9.0) +end + +def ctof(celcius) + fahrenheit = celcius * (9.0/5.0) + 32 +end diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..4224d2a87 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,46 @@ +def add(a, b) + total = a + b +end + +def subtract(a, b) + total = a -b +end + +def sum(array) + total = 0 + + array.each do |item| + total += item + end + + total +end + +def multiply(array) + total = 1 + + array.each do |item| + total *= item + end + + total +end + +def power(a, b) + total = 1 + + # total = a ** b + b.times do + total *= a + end + + total +end + +def factorial(a) + if a == 0 || a == 1 + return 1 + else + a * factorial(a - 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..559a71cc2 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,2])).to eq(4) + end + + it "multiplies several numbers" do + expect(multiply([2,2,3])).to eq(12) + end - it "multiplies several numbers" - 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..2462dc829 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,29 @@ +def echo(input) + "#{input}" +end + +def shout(input) + "#{input.upcase}" +end + +def repeat(input, number=2) + (input + " ") * (number-1) + input +end + +def start_of_word(string, position) + string[0..position-1] +end + +def first_word(string) + all_words = string.split(" ") + all_words[0] +end + +def titleize(string) + no_capitalization = ["and", "over", "the"] + + all_words = string.split(" ") + all_words.each { |word| word.capitalize! unless no_capitalization.include?(word) } + all_words[0].capitalize! + all_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..947dea4de --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,45 @@ +def is_vowel?(character) + vowels = ["a", "e", "i", "o", "u"] + + if vowels.include?(character) + true + else + false + end +end + + +def consonants(string) + result = "" + first = string[0] + second = string[1] + third = string[2] + + if !is_vowel?(second) && !is_vowel?(third) || (second =="q" && third == "u") + word_begin = string[0..2] + result += string[3..-1] + word_begin + "ay" + elsif !is_vowel?(second) || (first =="q" && second == "u") + word_begin = string[0..1] + result += string[2..-1] + word_begin + "ay" + else + result += string[1..-1] + first + "ay" + end +end + + +def translate(string) + split_words = string.split(" ") + result = "" + + if split_words.length > 1 + translated = split_words.collect { |word| translate(word)} + return translated.join(" ") + end + + if is_vowel?(string[0]) + result += "#{string}ay" + else + consonants(string) + end + +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..0386d4af8 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,18 @@ +def reverser + result = [] + + yield.split.each do |word| + result << word.reverse + end + + result.join(" ") +end + +def adder(added_num=1) + result = yield + result += added_num +end + +def repeater(num=1) + num.times { |item| 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..dd2a2081d --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,9 @@ +def measure(num=1) + first_time = Time.now + + num.times { |item| yield } + + second_time = Time.now + + elapsed_time = (second_time - first_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..5faf65cb9 --- /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..cec71b827 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,18 @@ +class Book + def title + @title + end + + def title=(string) + @title = titlelize(string) + end + + def titlelize(string) + no_capitalization = ["a", "an", "and", "in", "over", "of", "the"] + + all_words = string.split(" ") + all_words.each { |word| word.capitalize! unless no_capitalization.include?(word) } + all_words[0].capitalize! + all_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..893e36d70 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,22 @@ +class Timer + attr_accessor :seconds + + def initialize + @seconds = 0 + end + + def time_string + the_time = "" + + secs = @seconds % 60 + mins = (@seconds/60) % 60 + hrs = @seconds/3600 + + the_time = "#{format(hrs)}:#{format(mins)}:#{format(secs)}" + end + + def format(input) + input.to_s.length > 1 ? input.to_s : "0#{input.to_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..021b62675 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,37 @@ +class Temperature + def initialize(options={}) + @fahrenheit = options[:f] + @celsius = options[:c] + + @fahrenheit = in_fahrenheit if options[:c] + @celsius = in_celsius if options[:f] + end + + def in_fahrenheit + @fahrenheit = @celsius * (9.0/5.0) + 32 + end + + def in_celsius + @celsius = (@fahrenheit - 32) * (5.0/9.0) + end + + def self.from_celsius(celsius) + Temperature.new(c: celsius) + end + + def self.from_fahrenheit(fahrenheit) + Temperature.new(f: fahrenheit) + end +end + +class Celsius < Temperature + def initialize(celsius) + super(:c => celsius) + end +end + +class Fahrenheit < Temperature + def initialize(fahrenheit) + super(:f => fahrenheit) + 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..bc37e8d8a --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,47 @@ +class Dictionary + attr_accessor :entries + + def initialize + @entries = {} + end + + def add(listing) + if listing.is_a?(String) + @entries[listing] = nil + else + listing.each do |key, value| + @entries[key] = value + end + end + end + + def keywords + @entries.keys.sort + end + + def include?(listing) + @entries.include?(listing) + end + + def find(input) + matches = {} + @entries.each do |key, value| + if key[0..input.length-1] == input + matches[key] = value + end + end + + matches + end + + def printable + print = [] + + @entries.sort.map do |key, value| + print << "[#{key}] \"#{value}\"" + end + + print.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..ade57d36a --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,76 @@ +class RPNCalculator + attr_accessor :calculator, :value + + def initialize + @stack = [] + + end + + def value + @stack.last + end + + def push(a) + @stack << a + end + + def plus + raise "calculator is empty" if @stack.length < 2 + + a = @stack.pop + b = @stack.pop + total = a + b + + @stack << total + end + + def minus + raise "calculator is empty" if @stack.length < 2 + + a = @stack.pop + b = @stack.pop + total = b - a + + @stack << total + end + + def times + raise "calculator is empty" if @stack.length < 2 + + a = @stack.pop.to_f + b = @stack.pop.to_f + total = b * a + + @stack << total + end + + def divide + raise "calculator is empty" if @stack.length < 2 + + a = @stack.pop.to_f + b = @stack.pop.to_f + total = b / a + + @stack << total + end + + def tokens(string) + string.split(" ").map do |token| + (token.match(/\d+/)) ? token.to_f : token.to_sym + end + end + + def evaluate(string) + tokens(string).each do |token| + case token + when :+ then plus + when :- then minus + when :/ then divide + when :* then times + else push(token) + end # case + end # tokens.each + + value + end # evaluate +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..278400d5a --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,15 @@ +class Array + + def sum + self.reduce(0, :+) + end + + def square + self.collect { |num| num ** 2 } + end + + def square! + self.collect! { |num| num ** 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..da47f21ac --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,52 @@ +class Fixnum + + ONES = %w(one two three four five six seven eight nine) + TEENS = %w(eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen ten) + TENS = %w(ten twenty thirty forty fifty sixty seventy eighty ninety) + BIG_NUMS = [ + [100, "hundred"], + [1000, "thousand"], + [1_000_000, "million"], + [1_000_000_000, "billion"], + [1_000_000_000_000, "trillion"], + ] + + def in_words + num = self + output = '' + + return "zero" if num == 0 + + BIG_NUMS.reverse.each do |limit, word| + if num > (limit - 1) + th_place = num / limit + if th_place > 0 + output << "#{th_place.in_words} #{word}" + num -= (th_place * limit) + output << " " if num > 0 + end + end + end + + # conversions + tens_place = num / 10 + ones_place = num - tens_place * 10 + + if tens_place == 1 + output += TEENS[ones_place - 1] + return output + end + + if tens_place > 1 + output << TENS[tens_place - 1] + output << " " if ones_place > 0 + end + + if ones_place > 0 + output << ONES[ones_place - 1] + end + + output + end + +end