From 1c386a985077a33f41fcd176c3e504404e4bb57c Mon Sep 17 00:00:00 2001 From: test Date: Wed, 10 Aug 2016 22:04:17 +0000 Subject: [PATCH 1/5] finish problems 1-2 --- 00_hello/hello.rb | 7 ++++++ 01_temperature/temperature.rb | 7 ++++++ 02_calculator/calculator.rb | 26 +++++++++++++++++++++ 02_calculator/calculator_spec.rb | 39 ++++++++++++++++++++++---------- 03_simon_says/simon_says.rb | 0 5 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 00_hello/hello.rb create mode 100644 01_temperature/temperature.rb create mode 100644 02_calculator/calculator.rb create mode 100644 03_simon_says/simon_says.rb 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..e69de29bb From a960254d315c42e005f68a5a25e4477709034436 Mon Sep 17 00:00:00 2001 From: test Date: Wed, 17 Aug 2016 09:23:12 +0000 Subject: [PATCH 2/5] finish 4-14 --- 03_simon_says/simon_says.rb | 31 +++++++ 04_pig_latin/pig_latin.rb | 43 +++++++++ 05_silly_blocks/silly_blocks.rb | 13 +++ 06_performance_monitor/performance_monitor.rb | 19 ++++ 07_hello_friend/friend.rb | 9 ++ 08_book_titles/book.rb | 19 ++++ 09_timer/timer.rb | 50 ++++++++++ 10_temperature_object/temperature.rb | 50 ++++++++++ 11_dictionary/dictionary.rb | 51 ++++++++++ 12_rpn_calculator/rpn_calculator.rb | 80 ++++++++++++++++ 14_array_extensions/array_extensions.rb | 16 ++++ 15_in_words/in_words.rb | 93 +++++++++++++++++++ 12 files changed, 474 insertions(+) create mode 100644 04_pig_latin/pig_latin.rb create mode 100644 05_silly_blocks/silly_blocks.rb create mode 100644 06_performance_monitor/performance_monitor.rb create mode 100644 07_hello_friend/friend.rb create mode 100644 08_book_titles/book.rb create mode 100644 09_timer/timer.rb create mode 100644 10_temperature_object/temperature.rb create mode 100644 11_dictionary/dictionary.rb create mode 100644 12_rpn_calculator/rpn_calculator.rb create mode 100644 14_array_extensions/array_extensions.rb create mode 100644 15_in_words/in_words.rb diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb index e69de29bb..418d62e83 100644 --- a/03_simon_says/simon_says.rb +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,31 @@ + + +def echo word + word +end + +def shout word + word.upcase +end + +def repeat word, iterations=2 + repeats = [] + iterations.times { repeats << word } + repeats.join(" ") +end + +def start_of_word word, amount + word.slice(0, amount) +end + +def first_word words + words.split(" ")[0] +end + +def titleize string + array = string.split(" ") + + 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..091c35d1f --- /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..1b8534d78 --- /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 int = 1 + yield + int +end + +def repeater iterations = 1 + iterations.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..32c86e59d --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,19 @@ +def measure iterations = 1 + total = 0 + + if iterations.class == Array + iterations = iterations.length + end + + iterations.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 + total / iterations +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..d70cf764f --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting name=nil + if name + "Hello, #{name}!" + 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..556c564d7 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,19 @@ +class Book + + EXCEPTIONS = %w|the a an and in the of| + + def title + @title + end + + def title=(new_title) + @title = new_title.split(" ").map.with_index(0) do |word, index| + if !(EXCEPTIONS.include?(word.downcase)) || (index == 0) + word.capitalize + else + word + end + end.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..249ad9e4b --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,50 @@ +class Timer + + def initialize + @seconds = 0 + @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 #CLASS 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..f712bd9d4 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,50 @@ +class Temperature + def initialize (options = {}) + @option = options + end + + def in_fahrenheit + if @option[:c] + (@option[:c] * (9.0/5)) + 32 + elsif @option[:f] + @option[:f] + end + end + + def in_celsius + if @option[:f] + (@option[:f]-32.0) * (5.0/9) + elsif @option[:c] + @option[:c] + end + end + + def self.from_celsius (temperature) + self.new(:c => temperature) + end + + def self.from_fahrenheit (temperature) + self.new(:f => temperature) + end + +end + +class Celsius < Temperature + + def initialize temp + @temp = temp + @option = {} + @option[:c] = @temp + end + +end + +class Fahrenheit < Temperature + + def initialize temp + @temp = temp + @option = {} + @option[:f] = @temp + 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..ad019b208 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,51 @@ +class Dictionary + + def initialize + @entries = {} + end + + def entries + @entries + end + + def add key_value + if key_value.is_a? String + @entries[key_value] = nil + else + @key = key_value.keys[0] + @value = key_value.values[0] + + @entries[@key] = @value + end + end + + def keywords + @entries.keys.sort + end + + def include? word + keywords.include?(word) + end + + def find prefix + matches = {} + + keywords.each do |word| + if word.scan(/^#{prefix}/).length > 0 + matches[word] = entries[word] + # matches.merge(@) + end + end + matches + end + + def printable + output = [] + keywords.each do |keyword| + output << "[#{keyword}] \"#{entries[keyword]}\"" + end + output.join("\n") + end + +end + diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..c9290c745 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,80 @@ +#be able to sequence go back to back with operations w/o creating new obj +class RPNCalculator + 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 + + def value + @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..1154a57de --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,16 @@ +class Array + + def sum + self.reduce(0) { |current, after| current + after } + end + + def square + self.map { |number| number ** 2 } + end + + def square! + self.map! { |number| number ** 2 } + end + + +end#class 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..415eafbcc --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,93 @@ +# Regex to create digits key +# string = %q| + +# expect(0.in_words).to eq('zero') +# expect(1.in_words).to eq('one') +# expect(2.in_words).to eq('two') +# expect(3.in_words).to eq('three') +# expect(4.in_words).to eq('four') +# expect(5.in_words).to eq('five') +# expect(6.in_words).to eq('six') +# expect(7.in_words).to eq('seven') +# expect(8.in_words).to eq('eight') +# expect(9.in_words).to eq('nine') +# end + + +# expect(10.in_words).to eq('ten') +# expect(11.in_words).to eq('eleven') +# expect(12.in_words).to eq ('twelve') +# end + + +# expect(13.in_words).to eq('thirteen') +# expect(14.in_words).to eq('fourteen') +# expect(15.in_words).to eq('fifteen') +# expect(16.in_words).to eq('sixteen') +# expect(17.in_words).to eq('seventeen') +# expect(18.in_words).to eq('eighteen') +# expect(19.in_words).to eq('nineteen') +# end + + +# expect(20.in_words).to eq('twenty') +# expect(30.in_words).to eq('thirty') +# expect(40.in_words).to eq('forty') +# expect(50.in_words).to eq('fifty') +# expect(60.in_words).to eq('sixty') +# expect(70.in_words).to eq('seventy') +# expect(80.in_words).to eq('eighty') +# expect(90.in_words).to eq('ninety') +# end| + +# x = string.scan(/(\d+)/) +# puts x + +# y = string.scan(/'(\w+)'/) +# puts y + +# x.length == y.length + +# digits = {} + +# x.length.times do |i| +# num = x[i][0].to_i +# str = y[i][0] +# digits[num] = str +# end + +# digits + + + + +class Fixnum + + + def in_words + digits ={0=>"zero", 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"} + # digits[self] ? digits[self] : 'oops' + + if digits[self] + digits[self] + else + # + + money = (self / 10.0).to_s + tens = money[0] + ones = money[2] + ans = digits[tens.to_i * 10] + " " + digits[ones.to_i] + ans + + + end + + + + end + + + + + +end#classend From 6c993793e714679cb11172247cbe3f6d7ec2d9fe Mon Sep 17 00:00:00 2001 From: test Date: Tue, 30 Aug 2016 09:18:59 +0000 Subject: [PATCH 3/5] problem 15 complete --- 15_in_words/in_words.rb | 173 +++++++++++++++++++---------------- 15_in_words/in_words_spec.rb | 3 +- 2 files changed, 98 insertions(+), 78 deletions(-) diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb index 415eafbcc..61580503b 100644 --- a/15_in_words/in_words.rb +++ b/15_in_words/in_words.rb @@ -1,93 +1,112 @@ -# Regex to create digits key -# string = %q| - -# expect(0.in_words).to eq('zero') -# expect(1.in_words).to eq('one') -# expect(2.in_words).to eq('two') -# expect(3.in_words).to eq('three') -# expect(4.in_words).to eq('four') -# expect(5.in_words).to eq('five') -# expect(6.in_words).to eq('six') -# expect(7.in_words).to eq('seven') -# expect(8.in_words).to eq('eight') -# expect(9.in_words).to eq('nine') -# end - +class Fixnum -# expect(10.in_words).to eq('ten') -# expect(11.in_words).to eq('eleven') -# expect(12.in_words).to eq ('twelve') -# end + DIGITS = {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"} + ONES = {1=>"one", 2=>"two", 3=>"three", 4=>"four", 5=>"five", 6=>"six", 7=>"seven", 8=>"eight", 9=>"nine"} + TEENS = {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"} - -# expect(13.in_words).to eq('thirteen') -# expect(14.in_words).to eq('fourteen') -# expect(15.in_words).to eq('fifteen') -# expect(16.in_words).to eq('sixteen') -# expect(17.in_words).to eq('seventeen') -# expect(18.in_words).to eq('eighteen') -# expect(19.in_words).to eq('nineteen') -# end + def in_words + #as_string + as = self.to_s -# expect(20.in_words).to eq('twenty') -# expect(30.in_words).to eq('thirty') -# expect(40.in_words).to eq('forty') -# expect(50.in_words).to eq('fifty') -# expect(60.in_words).to eq('sixty') -# expect(70.in_words).to eq('seventy') -# expect(80.in_words).to eq('eighty') -# expect(90.in_words).to eq('ninety') -# end| + #divvys up number into threes + y = as.reverse + z = y.scan(/\d{3}|\d{2}|\d{1}/).reverse + z = z.map { |x| x.reverse } + -# x = string.scan(/(\d+)/) -# puts x - -# y = string.scan(/'(\w+)'/) -# puts y - -# x.length == y.length - -# digits = {} - -# x.length.times do |i| -# num = x[i][0].to_i -# str = y[i][0] -# digits[num] = str -# end - -# digits + ans = [] + + if self == 0 + ans << 'zero' + + elsif self < 1000 + ans << triple_gem(self) + + elsif self < 1000000 + z.each_with_index { |x,i| + ans << triple_gem(x.to_i) unless x.to_i == 0 ; puts triple_gem(x.to_i) + ans << 'thousand' if i == 0 && x.to_i != 0 + } + puts z.inspect + + elsif self < 100000000 + z.each_with_index { |x,i| + ans << triple_gem(x.to_i) unless x.to_i == 0 + ans << 'million' if i == 0 + ans << 'thousand' if i == 1 if i == 0 && x.to_i != 0 + } + + elsif self < 10000000000 + z.each_with_index { |x,i| + ans << triple_gem(x.to_i) unless x.to_i == 0 + ans << 'billion' if i == 0 + ans << 'million' if i == 1 && x.to_i != 0 + ans << 'thousand' if i == 2 && x.to_i != 0 + } + + elsif self < 10000000000000 + z.each_with_index { |x,i| + ans << triple_gem(x.to_i) unless x.to_i == 0 + ans << 'trillion' if i == 0 + ans << 'billion' if i == 1 && x.to_i != 0 + ans << 'million' if i == 2 && x.to_i != 0 + ans << 'thousand' if i == 3 && x.to_i != 0 + } + + else + "Too high of a number!" + + end + + return ans.join(" ").strip + end + def sub_hundred num + output = [] + + if DIGITS[num] + output << DIGITS[num] + else + ten_greater = (num / 10.0).to_s + tens = ten_greater[0] + ones = ten_greater[2] + output << DIGITS[tens.to_i * 10] + output << DIGITS[ones.to_i] + + end -class Fixnum + return output.join(" ") + + end - def in_words - digits ={0=>"zero", 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"} - # digits[self] ? digits[self] : 'oops' - if digits[self] - digits[self] - else - # - - money = (self / 10.0).to_s - tens = money[0] - ones = money[2] - ans = digits[tens.to_i * 10] + " " + digits[ones.to_i] - ans - + def super_hundred num + output = [] + temp = num.to_s[0].to_i + output << ONES[temp] + output << 'hundred' + + return output + end #hundo end + - end + def triple_gem number + output = [] - end - - - - - -end#classend + if number < 100 + output << sub_hundred(number) unless sub_hundred(number) == 0 + else + output << super_hundred(number) + output << sub_hundred(number.to_s[1..-1].to_i) unless number.to_s[1..-1].to_i == 0 + end + + output.join(" ") + 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 From a503f8b804aeb2aed7498e7ad6b1ec58c82552ff Mon Sep 17 00:00:00 2001 From: test Date: Wed, 12 Oct 2016 03:11:31 +0000 Subject: [PATCH 4/5] Rewrote for fun --- 03_simon_says/simon_says.rb | 26 ++- 04_pig_latin/pig_latin.rb | 6 +- 05_silly_blocks/silly_blocks.rb | 14 +- 06_performance_monitor/performance_monitor.rb | 21 +-- 07_hello_friend/friend.rb | 11 +- 08_book_titles/book.rb | 35 ++-- 09_timer/timer.rb | 22 +-- 10_temperature_object/temperature.rb | 46 ++---- 11_dictionary/dictionary.rb | 80 +++++---- 12_rpn_calculator/rpn_calculator.rb | 13 +- 14_array_extensions/array_extensions.rb | 28 ++-- 15_in_words/Untitled1 | 75 +++++++++ 15_in_words/in_words.rb | 153 +++++++----------- 13 files changed, 276 insertions(+), 254 deletions(-) create mode 100644 15_in_words/Untitled1 diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb index 418d62e83..f9f82fc0c 100644 --- a/03_simon_says/simon_says.rb +++ b/03_simon_says/simon_says.rb @@ -1,31 +1,29 @@ - - -def echo word - word +def echo(word) + return word end -def shout word - word.upcase +def shout(word) + return word.upcase end -def repeat word, iterations=2 +def repeat(word, iterations=2) repeats = [] iterations.times { repeats << word } - repeats.join(" ") + return repeats.join(" ") end -def start_of_word word, amount - word.slice(0, amount) +def start_of_word(word, amount) + return word.slice(0, amount) end -def first_word words - words.split(" ")[0] +def first_word(words) + return words.split(" ")[0] end -def titleize string +def titleize(string) array = string.split(" ") - array.map.with_index do |word, index| + 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 index 091c35d1f..f07bee78a 100644 --- a/04_pig_latin/pig_latin.rb +++ b/04_pig_latin/pig_latin.rb @@ -1,6 +1,6 @@ VOWELS = ['a', 'e', 'i', 'o', 'u'] -def translate string +def translate(string) words = string.split(" ") return words.map do |word| @@ -10,12 +10,12 @@ def translate string end -def translate_word word +def translate_word(word) working_index = nil group1, group2, groups = '', '', '' - if word[0..1] == 'qu' || word[1..2] == 'qu' + 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] diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb index 1b8534d78..fb5648266 100644 --- a/05_silly_blocks/silly_blocks.rb +++ b/05_silly_blocks/silly_blocks.rb @@ -1,13 +1,11 @@ def reverser - yield.split(" ").map { |word| word.reverse }.join(" ") + return yield.split(" ").map { |word| word.reverse }.join(" ") end -def adder int = 1 - yield + int +def adder(int = 1) + return yield + int end -def repeater iterations = 1 - iterations.times do - yield - end -end \ No newline at end of file +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 index 32c86e59d..c3190014c 100644 --- a/06_performance_monitor/performance_monitor.rb +++ b/06_performance_monitor/performance_monitor.rb @@ -1,19 +1,20 @@ -def measure iterations = 1 - total = 0 +def measure(i=1) + i = i.size if i.is_a?(Array) - if iterations.class == Array - iterations = iterations.length - end + total = 0 - iterations.times do - if block_given? + i.times do + if block_given? start_time = Time.now yield - total += Time.now - start_time + total += Time.now - start_time else start_time = Time.now total += Time.now - start_time end end - total / iterations -end \ No newline at end of file + + return total / i + +end + diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb index d70cf764f..68f5ab292 100644 --- a/07_hello_friend/friend.rb +++ b/07_hello_friend/friend.rb @@ -1,9 +1,6 @@ class Friend - def greeting name=nil - if name - "Hello, #{name}!" - else - "Hello!" - end + def greeting(name = nil) + name ? "Hello, #{name}!" : "Hello!" end -end \ No newline at end of file +end + diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb index 556c564d7..153b2567f 100644 --- a/08_book_titles/book.rb +++ b/08_book_titles/book.rb @@ -1,19 +1,26 @@ class Book + #be better to seperate by exceptions, conjuctions, and prepositions? + #most effecient way to do an_array.include?) - EXCEPTIONS = %w|the a an and in the of| + #exceptions + CONJUNCTIONS = %w[and] + PREPOSITIONS = %w[in of] + OTHER_WORDS = %w[the a an] - def title - @title - end + 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 - def title=(new_title) - @title = new_title.split(" ").map.with_index(0) do |word, index| - if !(EXCEPTIONS.include?(word.downcase)) || (index == 0) - word.capitalize - else - word - end - end.join(" ") - - end end \ No newline at end of file diff --git a/09_timer/timer.rb b/09_timer/timer.rb index 249ad9e4b..de559d292 100644 --- a/09_timer/timer.rb +++ b/09_timer/timer.rb @@ -1,21 +1,22 @@ class Timer + + attr_accessor :seconds - def initialize - @seconds = 0 + def initialize(secs = 0) + @seconds = secs @minutes = 0 @hours = 0 end - def seconds - @seconds - end + # def seconds + # @seconds + # end - def seconds=(seconds) - @seconds = seconds - end + # def seconds=(seconds) + # @seconds = seconds + # end def seconds_to_minutes_hours - while @seconds >= 60 if (@seconds - 3600 > 0) @hours += 1 @@ -46,5 +47,4 @@ def time_string "#{@hour_str}:#{@min_str}:#{@sec_str}" end - -end #CLASS END \ No newline at end of file +end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb index f712bd9d4..6422565d0 100644 --- a/10_temperature_object/temperature.rb +++ b/10_temperature_object/temperature.rb @@ -1,50 +1,36 @@ class Temperature - def initialize (options = {}) - @option = options + + def initialize hash + @fahrenheit = hash[:f] + @celsius = hash[:c] end def in_fahrenheit - if @option[:c] - (@option[:c] * (9.0/5)) + 32 - elsif @option[:f] - @option[:f] - end - end + @fahrenheit ||= @celsius * (9.0/5) + 32 + end def in_celsius - if @option[:f] - (@option[:f]-32.0) * (5.0/9) - elsif @option[:c] - @option[:c] - end + @celsius ||= (@fahrenheit - 32.0) * (5.0 / 9) end - def self.from_celsius (temperature) - self.new(:c => temperature) + def self.from_celsius(degrees) + self.new(:c => degrees) end - def self.from_fahrenheit (temperature) - self.new(:f => temperature) + def self.from_fahrenheit(degrees) + self.new(:f => degrees) end - + end class Celsius < Temperature - - def initialize temp - @temp = temp - @option = {} - @option[:c] = @temp + def initialize degrees + @celsius = degrees end - end class Fahrenheit < Temperature - - def initialize temp - @temp = temp - @option = {} - @option[:f] = @temp + 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 index ad019b208..1cc5ecf95 100644 --- a/11_dictionary/dictionary.rb +++ b/11_dictionary/dictionary.rb @@ -1,51 +1,43 @@ class Dictionary - - def initialize - @entries = {} - end - - def entries - @entries - end - - def add key_value - if key_value.is_a? String - @entries[key_value] = nil - else - @key = key_value.keys[0] - @value = key_value.values[0] - - @entries[@key] = @value - end - end - - def keywords - @entries.keys.sort - end - - def include? word - keywords.include?(word) - end - - def find prefix - matches = {} - - keywords.each do |word| - if word.scan(/^#{prefix}/).length > 0 - matches[word] = entries[word] - # matches.merge(@) - end - end - matches - end - + + 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.join("\n") + output * "\n" end - -end - + +end \ No newline at end of file diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb index c9290c745..f37e7f729 100644 --- a/12_rpn_calculator/rpn_calculator.rb +++ b/12_rpn_calculator/rpn_calculator.rb @@ -1,5 +1,12 @@ #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 @@ -71,10 +78,6 @@ def evaluate string end - def value - @value - end + end - - diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb index 1154a57de..6175885f5 100644 --- a/14_array_extensions/array_extensions.rb +++ b/14_array_extensions/array_extensions.rb @@ -1,16 +1,14 @@ class Array - - def sum - self.reduce(0) { |current, after| current + after } - end - - def square - self.map { |number| number ** 2 } - end - - def square! - self.map! { |number| number ** 2 } - end - - -end#class end \ No newline at end of file + 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/Untitled1 b/15_in_words/Untitled1 new file mode 100644 index 000000000..a3ef3e35a --- /dev/null +++ b/15_in_words/Untitled1 @@ -0,0 +1,75 @@ +x = 327644 + + + +def three_digit_splitter + + +y = x.to_s.split("") + +a = y.length - 1 + + +noob = [] +temp = "" + + +a.downto(0) do |i| + + temp.prepend(y[i]) + + if (temp.size == 3) || (i == 0) + noob.unshift(temp.to_i) + temp = "" + end + + +end + +p noob + + + + +~!~~~~ + + + 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) + + + + + + + + + def three_places(n) + + ans = [] + + a, b = n.divmod(100) + ans << DIGITS[a] + # ans << 'hundred' + + if DIGITS[b] + ans << DIGITS[b] + else + tens, ones = b.divmod(10) + tens *= 10 + + ans << DIGITS[tens] + ans << DIGITS[ones] + end + + ans.compact! + # puts ans.inspect + ans.join(" ") + + end + +w = ["1", "327", "000"] +w.each { |i| p three_places(i.to_i) } \ No newline at end of file diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb index 61580503b..4f4d9703c 100644 --- a/15_in_words/in_words.rb +++ b/15_in_words/in_words.rb @@ -1,112 +1,79 @@ class Fixnum - DIGITS = {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"} - ONES = {1=>"one", 2=>"two", 3=>"three", 4=>"four", 5=>"five", 6=>"six", 7=>"seven", 8=>"eight", 9=>"nine"} - TEENS = {11=>"eleven", 12=>"twelve", 13=>"thirteen", 14=>"fourteen", 15=>"fifteen", 16=>"sixteen", 17=>"seventeen", 18=>"eighteen", 19=>"nineteen"} + 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"} - - def in_words - - #as_string - as = self.to_s - #divvys up number into threes - y = as.reverse - z = y.scan(/\d{3}|\d{2}|\d{1}/).reverse - z = z.map { |x| x.reverse } - + DIGITS = SUB_TEN.merge(SUB_TWENTY).merge(TENS) - ans = [] - - if self == 0 - ans << 'zero' - - elsif self < 1000 - ans << triple_gem(self) - - elsif self < 1000000 - z.each_with_index { |x,i| - ans << triple_gem(x.to_i) unless x.to_i == 0 ; puts triple_gem(x.to_i) - ans << 'thousand' if i == 0 && x.to_i != 0 - } - puts z.inspect - - elsif self < 100000000 - z.each_with_index { |x,i| - ans << triple_gem(x.to_i) unless x.to_i == 0 - ans << 'million' if i == 0 - ans << 'thousand' if i == 1 if i == 0 && x.to_i != 0 - } - - elsif self < 10000000000 - z.each_with_index { |x,i| - ans << triple_gem(x.to_i) unless x.to_i == 0 - ans << 'billion' if i == 0 - ans << 'million' if i == 1 && x.to_i != 0 - ans << 'thousand' if i == 2 && x.to_i != 0 - } - - elsif self < 10000000000000 - z.each_with_index { |x,i| - ans << triple_gem(x.to_i) unless x.to_i == 0 - ans << 'trillion' if i == 0 - ans << 'billion' if i == 1 && x.to_i != 0 - ans << 'million' if i == 2 && x.to_i != 0 - ans << 'thousand' if i == 3 && x.to_i != 0 - } - + VALUES = ['thousand', 'million', 'billion', 'trillion'] + + def in_words + if self.zero? + return "zero" + elsif DIGITS[self] + return DIGITS[self] else - "Too high of a number!" - + return convert(self) end - - return ans.join(" ").strip - end + + + def convert(number) + first = three_digit_splitter(number) + wanted_values = values_selection(first.length) - - def sub_hundred num - output = [] - - if DIGITS[num] - output << DIGITS[num] - else - ten_greater = (num / 10.0).to_s - tens = ten_greater[0] - ones = ten_greater[2] - output << DIGITS[tens.to_i * 10] - output << DIGITS[ones.to_i] - + output = first.map do |i| + current = wanted_values.shift + three_places(i,current) end - - return output.join(" ") - - end + + output.reject { |i| i.empty? }.join(" ") + end - - def super_hundred num - output = [] - temp = num.to_s[0].to_i - output << ONES[temp] - output << 'hundred' - - return output - end #hundo end + def three_places(n, current) + output = [] + + a, b = n.divmod(100) + unless a.zero? + output << DIGITS[a] + output << 'hundred' + end - def triple_gem number + if DIGITS[b] + output << DIGITS[b] + else + tens, ones = b.divmod(10) + tens *= 10 - output = [] + output << DIGITS[tens] + output << DIGITS[ones] + end + + output << current unless b.zero? + + output.compact! + output.join(" ") + end - if number < 100 - output << sub_hundred(number) unless sub_hundred(number) == 0 - else - output << super_hundred(number) - output << sub_hundred(number.to_s[1..-1].to_i) unless number.to_s[1..-1].to_i == 0 - end - - output.join(" ") + 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 From 4f3a7f437243bf815e40846f886e6e21326201d9 Mon Sep 17 00:00:00 2001 From: Kenny Date: Tue, 11 Oct 2016 20:13:42 -0700 Subject: [PATCH 5/5] Delete Untitled1 --- 15_in_words/Untitled1 | 75 ------------------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 15_in_words/Untitled1 diff --git a/15_in_words/Untitled1 b/15_in_words/Untitled1 deleted file mode 100644 index a3ef3e35a..000000000 --- a/15_in_words/Untitled1 +++ /dev/null @@ -1,75 +0,0 @@ -x = 327644 - - - -def three_digit_splitter - - -y = x.to_s.split("") - -a = y.length - 1 - - -noob = [] -temp = "" - - -a.downto(0) do |i| - - temp.prepend(y[i]) - - if (temp.size == 3) || (i == 0) - noob.unshift(temp.to_i) - temp = "" - end - - -end - -p noob - - - - -~!~~~~ - - - 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) - - - - - - - - - def three_places(n) - - ans = [] - - a, b = n.divmod(100) - ans << DIGITS[a] - # ans << 'hundred' - - if DIGITS[b] - ans << DIGITS[b] - else - tens, ones = b.divmod(10) - tens *= 10 - - ans << DIGITS[tens] - ans << DIGITS[ones] - end - - ans.compact! - # puts ans.inspect - ans.join(" ") - - end - -w = ["1", "327", "000"] -w.each { |i| p three_places(i.to_i) } \ No newline at end of file