diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..5a5ad056e --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(who) + "Hello, #{who}!" +end diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..9943bab77 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f) + (f-32.0) * (5.0/9) +end + +def ctof(c) + (c * (9.0/5)) + 32 +end diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..8e0f4d58c --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,26 @@ +def add (num1, num2) + num1 + num2 +end + +def subtract (num1, num2) + num1 - num2 +end + +def sum (numbers) + #numbers.reduce(0) { |current_num, next_num| current_num + next_num } + numbers.reduce(0, :+) +end + +def multiply (*args) + args.reduce(1, :*) +end + +def power (base, exponent) + base ** exponent +end + +def factorial number + accumulator = 1 + number.downto(1) { |i| accumulator *= i } + accumulator +end \ No newline at end of file diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..4bb529c19 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 several numbers" - + it "multiplies two numbers" do + expect(multiply(3,4)).to eq(12) + end + it "multiplies several numbers" do + expect(multiply(3,4,2)).to eq(24) + 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(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/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..f9f82fc0c --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,29 @@ +def echo(word) + return word +end + +def shout(word) + return word.upcase +end + +def repeat(word, iterations=2) + repeats = [] + iterations.times { repeats << word } + return repeats.join(" ") +end + +def start_of_word(word, amount) + return word.slice(0, amount) +end + +def first_word(words) + return words.split(" ")[0] +end + +def titleize(string) + array = string.split(" ") + + return array.map.with_index do |word, index| + (index == 0) || (word == array[-1]) || (word.length > 4) ? word.capitalize : word + end.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..f07bee78a --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,43 @@ +VOWELS = ['a', 'e', 'i', 'o', 'u'] + +def translate(string) + words = string.split(" ") + + return words.map do |word| + translate_word word + end.join(" ") + +end + + +def translate_word(word) + + working_index = nil + group1, group2, groups = '', '', '' + + if word[0..1] == 'qu' || word[1..2] == 'qu' + regex = word.scan(/\As?q[a-z]/) + working_index = regex[0].length + group1 = word[0...working_index] + group2 = word[working_index..-1] + + elsif !(VOWELS.include? word[0]) + regex = word.scan(/\A[^aeiou]+/) + working_index = regex[0].length + group1 = word[0...working_index] + group2 = word[working_index..-1] + + else + group2 = word + end + + groups = group2 + group1 + if groups.end_with? 'a' + return groups + 'y' + else + return groups + 'ay' + end + +end + + diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..fb5648266 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,11 @@ +def reverser + return yield.split(" ").map { |word| word.reverse }.join(" ") +end + +def adder(int = 1) + return yield + int +end + +def repeater(i=3) + return i.times { yield } +end diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..c3190014c --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,20 @@ +def measure(i=1) + i = i.size if i.is_a?(Array) + + total = 0 + + i.times do + if block_given? + start_time = Time.now + yield + total += Time.now - start_time + else + start_time = Time.now + total += Time.now - start_time + end + end + + return total / i + +end + diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb new file mode 100644 index 000000000..68f5ab292 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,6 @@ +class Friend + def greeting(name = nil) + name ? "Hello, #{name}!" : "Hello!" + end +end + diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb new file mode 100644 index 000000000..153b2567f --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,26 @@ +class Book + #be better to seperate by exceptions, conjuctions, and prepositions? + #most effecient way to do an_array.include?) + + #exceptions + CONJUNCTIONS = %w[and] + PREPOSITIONS = %w[in of] + OTHER_WORDS = %w[the a an] + + ALL_EXCEPTIONS = CONJUNCTIONS + PREPOSITIONS + OTHER_WORDS + + EXCEPTIONS = %w[the a an] + + def title=(name) + @name = name + end + + def title + #@name.split.map(&:capitalize).join(" ") + + @name.split.map.with_index do |word, index| + !ALL_EXCEPTIONS.include?(word) || index == 0 ? word.capitalize : word + end.join(" ") #just keeping it playful with some spaghetti code :-) + 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..de559d292 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,50 @@ +class Timer + + attr_accessor :seconds + + def initialize(secs = 0) + @seconds = secs + @minutes = 0 + @hours = 0 + end + + # def seconds + # @seconds + # end + + # def seconds=(seconds) + # @seconds = seconds + # end + + def seconds_to_minutes_hours + while @seconds >= 60 + if (@seconds - 3600 > 0) + @hours += 1 + @seconds -= 3600 + + elsif (@seconds - 60 > 0) + @minutes += 1 + @seconds -= 60 + end + end + + end + + def time_string + + seconds_to_minutes_hours + + #INT TO STRING CONVERSIONS + @sec_str = @seconds.to_s + @min_str = @minutes.to_s + @hour_str = @hours.to_s + + @sec_str = '0' + @sec_str if @seconds < 10 + @min_str = '0' + @min_str if @minutes < 10 + @hour_str = '0' + @hour_str if @hours < 10 + + + "#{@hour_str}:#{@min_str}:#{@sec_str}" + + end +end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..6422565d0 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,36 @@ +class Temperature + + def initialize hash + @fahrenheit = hash[:f] + @celsius = hash[:c] + end + + def in_fahrenheit + @fahrenheit ||= @celsius * (9.0/5) + 32 + end + + def in_celsius + @celsius ||= (@fahrenheit - 32.0) * (5.0 / 9) + end + + def self.from_celsius(degrees) + self.new(:c => degrees) + end + + def self.from_fahrenheit(degrees) + self.new(:f => degrees) + end + +end + +class Celsius < Temperature + def initialize degrees + @celsius = degrees + end +end + +class Fahrenheit < Temperature + def initialize degrees + @fahrenheit = degrees + 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..1cc5ecf95 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,43 @@ +class Dictionary + + attr_reader :entries, :keywords + def initialize + @entries = Hash.new() + @keywords = [] + end + + def add(entry) + if String === entry + @keywords << entry + @entries.store(entry, nil) + else + @entries.merge!(entry) + @keywords << entry.keys[0] + end + @keywords.sort! + end + + def include?(keyword) + @keywords.include?(keyword) + end + + def find(word) + # include?(word) ? match_it(word) : {} + matches = {} + @keywords.each do |keyword| + if keyword.start_with?(word) + matches[keyword] = @entries[keyword] + end + end + return matches + end + + def printable + output = [] + keywords.each do |keyword| + output << "[#{keyword}] \"#{entries[keyword]}\"" + end + output * "\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..f37e7f729 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,83 @@ +#be able to sequence go back to back with operations w/o creating new obj +class RPNCalculator + + attr_reader :value + + # def value + # @value + # end + + def initialize + @stack = [] + @value = 0 + end + + def push number + @stack << number + end + + def plus + raise "calculator is empty" if @stack.length < 2 + addition = @stack.pop + @stack.pop + + @stack.push(addition) + @value = addition + end + + def minus + raise "calculator is empty" if @stack.length < 2 + subtraction = (@stack.pop - @stack.pop) * -1 + + @stack.push(subtraction) + @value = subtraction + end + + def divide + raise "calculator is empty" if @stack.length < 2 + last = @stack.pop.to_f + second_last = @stack.pop.to_f + action = second_last / last + + @stack.push(action) + @value = action + end + + def times + raise "calculator is empty" if @stack.length < 2 + last = @stack.pop.to_f + second_last = @stack.pop.to_f + action = second_last * last + + @stack.push(action) + @value = action + end + + def tokens string + string.split(" ").map do |token| + (token.match(/\d+/)) ? token.to_i : token.to_sym + end + + end + + def evaluate string + + string.split(" ").each do |item| + if item.match(/\d+/) + @stack.push(item.to_f) + elsif item == '+' + plus + elsif item == '-' + minus + elsif item == '*' + times + elsif item == '/' + divide + end + end + value + + end + + + +end diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..6175885f5 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,14 @@ +class Array + def sum + self.inject(0, &:+) + end + + def square + self.map { |n| n ** 2 } + end + + def square! + self.map! { |n| n ** 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..4f4d9703c --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,79 @@ +class Fixnum + + SUB_TEN = {1=>"one", 2=>"two", 3=>"three", 4=>"four", 5=>"five", 6=>"six", 7=>"seven", 8=>"eight", 9=>"nine"} + SUB_TWENTY = {11=>"eleven", 12=>"twelve", 13=>"thirteen", 14=>"fourteen", 15=>"fifteen", 16=>"sixteen", 17=>"seventeen", 18=>"eighteen", 19=>"nineteen"} + TENS = {10=>"ten", 20=>"twenty", 30=>"thirty", 40=>"forty", 50=>"fifty", 60=>"sixty", 70=>"seventy", 80=>"eighty", 90=>"ninety"} + + DIGITS = SUB_TEN.merge(SUB_TWENTY).merge(TENS) + + VALUES = ['thousand', 'million', 'billion', 'trillion'] + + def in_words + if self.zero? + return "zero" + elsif DIGITS[self] + return DIGITS[self] + else + return convert(self) + end + end + + + def convert(number) + first = three_digit_splitter(number) + wanted_values = values_selection(first.length) + + output = first.map do |i| + current = wanted_values.shift + three_places(i,current) + end + + output.reject { |i| i.empty? }.join(" ") + end + + + def three_places(n, current) + output = [] + + a, b = n.divmod(100) + + unless a.zero? + output << DIGITS[a] + output << 'hundred' + end + + if DIGITS[b] + output << DIGITS[b] + else + tens, ones = b.divmod(10) + tens *= 10 + + output << DIGITS[tens] + output << DIGITS[ones] + end + + output << current unless b.zero? + + output.compact! + output.join(" ") + end + + + def three_digit_splitter(number) + reversed_string = number.to_s.reverse + + split_array = reversed_string.scan(/\d{3}|\d{2}|\d{1}/).reverse + split_array.map! { |x| x.reverse.to_i } + end + + + def values_selection(size) + if size >= 2 + VALUES[0..(size-2)].reverse + else + [] + end + end + + +end diff --git a/15_in_words/in_words_spec.rb b/15_in_words/in_words_spec.rb index 1f5768575..fdaee9293 100644 --- a/15_in_words/in_words_spec.rb +++ b/15_in_words/in_words_spec.rb @@ -89,7 +89,8 @@ expect(10_000_001.in_words).to eq('ten million one') end - it "reads billions" do + it "read + 's billions" do expect(1_234_567_890.in_words).to eq('one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety') end