diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..407219bd6 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(name="Stephen") + "Hello, #{name}!" +end diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..1095a6223 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f_temp) + ((f_temp - 32.0) * 5.0)/9.0 +end + +def ctof(c_temp) + ((c_temp * 9.0)/5.0)+32.0 +end diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..f152af6ab --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,38 @@ +def add(a, b) + a + b +end + +def subtract(a, b) + a - b +end + +def sum(nums_to_sum) + total = 0 + nums_to_sum.each do |num| + total += num + end + + return total +end + +def multiply(nums_to_multiply=[]) + total = 1 + nums_to_multiply.each do |num| + total *= num + end + + return total +end + +def power(a, b) + a**b +end + +def factorial(num) + total = 1 + 2.upto(num) do |product| + total *= product + end + + return total +end diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..f8ce2f497 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,3])).to eq(6) + end + + it "multiplies several numbers" do + expect(multiply([2,6,3,28])).to eq(1008) + 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(5,8)).to eq(390625) + 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..96d9c220f --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,33 @@ +def echo(statement) + statement +end + +def shout(statement) + statement.upcase +end + +def repeat(input, n=2) + ([input] * n).join(' ') +end + +def start_of_word(word, num) + word.slice(0..(num - 1)) +end + +def first_word(input) + input.split(' ')[0] +end + +def titleize(input) + captilized = [] + little_words = ['and', 'the', 'over'] + input.split(' ').each_with_index do |word, ind| + if ind != 0 && little_words.include?(word) + captilized.push(word) + else + captilized.push(word.capitalize) + end + end + + captilized.join(' ').to_s +end diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..aed21b123 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,19 @@ +def translate(input) + must_capitalize = true if input.capitalize == input + vowels = %w(a e i o u) + words_to_translate = input.split(' ') + translated_word = [] + words_to_translate.each do |word| + if vowels.include?(word[0]) + translated_word << word + 'ay' + else + ind = 0 + ind += 1 until vowels.include?(word[ind]) && (word[ind-1] + word[ind] != 'qu') + + translated_word << word.slice(ind...word.size) + word.slice(0...ind) + 'ay' + end + end + + return translated_word.join(' ').capitalize if must_capitalize + return translated_word.join(' ') +end diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index cc659edfd..4ea087a98 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -67,6 +67,11 @@ # Test-driving bonus: # * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course) + it "must retain captilization after translation" do + s = translate("The quick brown fox") + expect(s).to eq("Ethay ickquay ownbray oxfay") + end + # * retain the punctuation from the original phrase end diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..dce329aae --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,13 @@ +def reverser + yield.split.map { |word| word.reverse}.join(" ") +end + +def adder(num=1) + yield + num +end + +def repeater(loops=1) + loops.times do + yield + end +end diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..c3f545dc0 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,10 @@ +def measure(num_times = 1) + avg_time = [] + num_times.times do + beginning_time = Time.now + yield + avg_time << (Time.now - beginning_time) + end + + return avg_time.inject{ |total_time, bench_time| total_time + bench_time }.to_f / avg_time.size +end diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb new file mode 100644 index 000000000..858650aac --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,6 @@ +class Friend + def greeting(name=nil) + return "Hello, #{name}!" if name + "Hello!" + end +end diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb new file mode 100644 index 000000000..790b84f35 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,25 @@ +class Book + def initialize(title = nil) + @title = title + @exclusion_words = %w(the a an and in of) + end + + def title + capitalize_each(@title) + end + + def title=(title) + capitalize_each(@title = title) + end + + def capitalize_each(title) + capitalized_title = title.split.each_with_index.map do |v, i| + if i == 0 || !@exclusion_words.include?(v) + v.capitalize + else + v + end + end + capitalized_title.join(' ') + end +end diff --git a/09_timer/timer.rb b/09_timer/timer.rb new file mode 100644 index 000000000..2fc6bf910 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,11 @@ +class Timer + attr_accessor :seconds + + def initialize + self.seconds = 0 + end + + def time_string + Time.at(self.seconds).utc.strftime("%H:%M:%S") + end +end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..3b391929f --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,63 @@ +class Temperature + attr_accessor :temp + + def initialize(options={}) + @temp = options + end + + def in_fahrenheit + if temp[:f] + return temp[:f] + else + return ((temp[:c] * 9.0)/5.0)+32.0 + end + end + + def in_celsius + if temp[:c] + return temp[:c] + else + return ((temp[:f] - 32.0) * 5.0)/9.0 + end + end + + def self.from_celsius(temp) + return Temperature.new(:c => temp) + end + + def self.from_fahrenheit(temp) + return Temperature.new(:f => temp) + end +end + +class Celsius < Temperature + attr_accessor :temp + + def initilize(temp) + @temp = temp + end + + def in_celsius + return temp + end + + def in_fahrenheit + return ((temp * 9.0)/5.0)+32.0 + end +end + +class Fahrenheit < Temperature + attr_accessor :temp + + def initilize(temp) + @temp = temp + end + + def in_celsius + return ((temp - 32.0) * 5.0)/9.0 + end + + def in_fahrenheit + return temp + end +end diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..d89b15e65 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,38 @@ +class Dictionary + attr_accessor :entries + + def initialize (entries = {}) + @entries = entries + end + + def add(words={}) + if words.is_a? String + entries[words] = nil + else + words.each_pair do |word, definition| + entries[word] = definition + end + end + end + + def keywords + entries.keys.sort + end + + def include?(word) + entries.has_key?(word) + end + + def find(word) + return entries.select{|k,v| k.start_with? word} || {} + end + + def printable + printable_list = "" + entries.sort.each do |value| + printable_list << "[#{value[0]}] \"#{value[1]}\"\n" + end + + printable_list.strip + end +end diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..aaa4a7bce --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,87 @@ +class RPNCalculator + attr_accessor :stack + + def initialize + @stack = [] + end + + def push(num) + @stack.push(num) + end + + def plus + empty_array? + b, a = populate_numbers + result = a + b + @stack.push(result) + + nil + end + + def minus + empty_array? + b, a = populate_numbers + result = a - b + @stack.push(result) + + nil + end + + def times + empty_array? + b, a = populate_numbers + result = a * b + @stack.push(result) + + nil + end + + def divide + empty_array? + b, a = populate_numbers + result = a.to_f / b.to_f + @stack.push(result) + + nil + end + + def empty_array? + raise 'calculator is empty' if @stack == [] + end + + def populate_numbers + b = stack.pop + a = stack.pop + [b, a] + end + + def tokens(str) + str.split.map! { |t| t[/\d/] ? t.to_i : t.to_sym } + end + + def evaluate(str) + temp_array = str.split + temp_array.each do |t| + if t[/\d/] + @stack.push(t.to_i) + else + case t + when '*' + self.times + when '/' + self.divide + when '+' + self.plus + when '-' + self.minus + end + end + end + + value + end + + def value + @stack[-1] + end +end diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb new file mode 100644 index 000000000..9440b629b --- /dev/null +++ b/13_xml_document/xml_document.rb @@ -0,0 +1,62 @@ +class XmlDocument + attr_accessor :indent, :indent_count + + def initialize(indent = false) + @indent = indent + @indent_count = 0 + end + + def hello(options = {}, &block) + builder = " name='#{options[:name]}'" if options[:name] + + return xml_builder('hello', &block) if block_given? + + "" + end + + def goodbye(_options = {}, &block) + return xml_builder('goodbye', &block) if block_given? + + '' + end + + def come_back(_options = {}, &block) + return xml_builder('come_back', &block) if block_given? + + '' + end + + def ok_fine(options = {}, &block) + builder = " be='#{options[:be]}'" if options[:be] + return xml_builder('ok_fine', &block) if block_given? + + if @indent + indent_space = increase_indent + return "#{indent_space}\n" + else + return "" + end + end + + def xml_builder(caller) + built_xml = '' + if @indent + indent_space = increase_indent + built_xml = "#{indent_space}<#{caller}>\n#{yield}#{indent_space}\n" + else + built_xml = "<#{caller}>#{yield}" + end + + built_xml + end + + def increase_indent + indent_space = ' ' * @indent_count + @indent_count = 1 + @indent_count + indent_space + end + + def send(tag_name) + "<#{tag_name}/>" + end +end diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..d66363439 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,13 @@ +class Array + def sum + inject(0) { |sum, n| sum + n } + end + + def square + map { |x| x**2 } + end + + def square! + map! { |x| x**2 } + end +end diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb new file mode 100644 index 000000000..db2c8be2c --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,61 @@ +class Integer + ONES = %w(#{nil} one two three four five six seven eight nine).freeze + TEENS = %w(ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen).freeze + TENS = %w(ten twenty thirty forty fifty sixty seventy eighty ninety).freeze + + def translate(number, str = '', last = false) + return str if number == 0 + if number / 100 > 0 + remainder = number % 100 + number /= 100 + str += ' ' + ONES[number] + ' hundred' + elsif number.between?(10, 19) + str += if last + ' ' + TEENS[number - 10] + else + ' ' + TEENS[number - 10] + end + return str + elsif number / 10 > 0 + remainder = number % 10 + number /= 10 + str += if last + ' ' + TENS[number - 1] + else + ' ' + TENS[number - 1] + end + else + str += ' ' + ONES[number] + remainder = 0 + end + translate(remainder, str, true) + end + + def in_words(str = '') + now_writing = self + return 'zero' if now_writing == 0 + case + when now_writing / 1_000_000_000_000 > 0 + to_write = now_writing % 1_000_000_000_000 + now_writing /= 1_000_000_000_000 + str += translate(now_writing) + ' trillion' + when now_writing / 1_000_000_000 > 0 + to_write = now_writing % 1_000_000_000 + now_writing /= 1_000_000_000 + str += translate(now_writing) + ' billion' + when now_writing / 1_000_000 > 0 + to_write = now_writing % 1_000_000 + now_writing /= 1_000_000 + str += translate(now_writing) + ' million' + when now_writing / 1000 > 0 + to_write = now_writing % 1000 + now_writing /= 1000 + str += translate(now_writing) + ' thousand' + else + str += translate(now_writing) + return str[1..-1] + end + return str[1..-1] if to_write == 0 + to_write.in_words(str) + end +end