From 099acf0c58fd60c01acb9c8a930eab371430d25e Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Tue, 3 Jan 2017 12:31:52 -0500 Subject: [PATCH 01/13] test temperature --- 00_hello/hello.rb | 7 +++++++ 01_temperature/temperature.rb | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 00_hello/hello.rb create mode 100644 01_temperature/temperature.rb diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..52008fc13 --- /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..80cbbdb24 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(temp) + (temp - 32) * 5.0 / 9.0 +end + +def ctof(temp) + temp * 9 / 5.0 + 32 +end \ No newline at end of file From d4e073bcec98b916c2553978d9aaf095a9488525 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Tue, 3 Jan 2017 14:59:23 -0500 Subject: [PATCH 02/13] add calculator and simon says --- .DS_Store | Bin 0 -> 8196 bytes 02_calculator/calculator.rb | 27 +++++++++++++++++++++ 02_calculator/calculator_spec.rb | 37 ++++++++++++++++++++--------- 03_simon_says/simon_says.rb | 39 +++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 .DS_Store create mode 100644 02_calculator/calculator.rb create mode 100644 03_simon_says/simon_says.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0c123036376b578ef4a57738be5fd7a773a9ded8 GIT binary patch literal 8196 zcmeHM&2G~`5T0!db!bZp1zL%V4oEu_HTa2q7pJ z9s-UWka!fHfCu3L;G6Xl*={2Bgh1Vmc4zIKZ+7>a8N2HektnyFBGD2N8K^AhXV8o& zJkIr0nNW9bLJHuC>g3@U?0G@$l(xR18PE)91~dbj0nNa_zyRLaT#PaA{diQjngPwg zxnzK!4<;(hl9Gd1%0~wpc?5u*!fjd5$2mY^OeIT74q_>xuo<5ogn|ln#1JtY<0h*^ zmXsXCGKQ0g;Uv_|LY+{AoE_0lz98CC~4+7uXo%bh?Ln z=a($#5nYCM5&A9YH>im=Bnww<$U2|>6WDJV>>RMGz}A59(6-SsHlQ$#6pJH}Yd}Wi zV%C6s*|(@mQ3m<(>`1#4HPvdr+fC1nT7|+7F)=xH;o`KB%Ne;j<8|dI=u}!(s~wfC z@TI(Row}|1&w{`k_AUFlTkllouRRR>mgV~OFrohHh3h*(ryR6>Cz5uaSgj*qyxC|KSt1ha%LIkn-{Mu>Fi4 z(>#W(lS=4&fq%#*P-mgJ4l0|V^$;?4hf98j)Su8z20KY%?KmV9zD`{1xj zqB)qifv~ZL04wLP=6OA!M+H_A%ZYPWAsUj#KHXlt9S!DHEN|T zF@NG#g1|)#FxpXhwOOgji@;Gi6h@c5G!G%Fjv5#Y5My`#sETx}={w_VB9eND8 zf<2AMtP~;-pG5NYPy^338KFGaATTe;A@~LX|NkB?qy@9hATSAwa+XnRosEXBN(H`3+KRR Date: Tue, 3 Jan 2017 15:14:51 -0500 Subject: [PATCH 03/13] add pig latin --- 04_pig_latin/pig_latin.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 04_pig_latin/pig_latin.rb diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..008135194 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,19 @@ +def translate(words) + vowels = ["a", "e", "i", "o", "u"] + result = "" + words.split(" ").each do |word| + if vowels.include?(word[0]) + result += word + "ay" + " " + else + while !vowels.include?(word[0]) + if word.slice(0, 2) == "qu" + word = word.slice(2, word.length) + word.slice(0, 2) + else + word = word.slice(1, word.length) + word[0] + end + end + result += word + "ay" + " " + end + end + result.strip +end \ No newline at end of file From a57155b40fbb1c3f3130faf6ffeeacdb6573ca19 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Tue, 3 Jan 2017 15:53:21 -0500 Subject: [PATCH 04/13] add silly blocks --- 05_silly_blocks/silly_blocks.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 05_silly_blocks/silly_blocks.rb diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..66316c8f3 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,14 @@ +def reverser + result = yield.split(" ").map { |word| word.reverse } + result.join(" ") +end + +def adder(num = 1) + yield + num +end + +def repeater(num = 1) + num.times do + yield + end +end \ No newline at end of file From 0112d017c3a44b8f952e17ab5a1d90161dd65145 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Tue, 3 Jan 2017 16:04:40 -0500 Subject: [PATCH 05/13] add performance monitor --- 06_performance_monitor/performance_monitor.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 06_performance_monitor/performance_monitor.rb diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..d933df63a --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,6 @@ +def measure(num = 1) + beg_time = Time.now + num.times { yield } + finish_time = Time.now + (finish_time - beg_time) / num +end \ No newline at end of file From 842e54a0231d5f90e56cd9b450c3e9c6b00d04f0 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Tue, 3 Jan 2017 16:12:18 -0500 Subject: [PATCH 06/13] add hello friend --- .DS_Store | Bin 8196 -> 10244 bytes 03_simon_says/.DS_Store | Bin 0 -> 6148 bytes 07_hello_friend/friend.rb | 9 +++++++++ 3 files changed, 9 insertions(+) create mode 100644 03_simon_says/.DS_Store create mode 100644 07_hello_friend/friend.rb diff --git a/.DS_Store b/.DS_Store index 0c123036376b578ef4a57738be5fd7a773a9ded8..b7be55eb79e23a86e58c97f0dff767acd1776620 100644 GIT binary patch delta 149 zcmZp1XbF&DU|?W$DortDU{C-uIe-{M3-C-V6q~50$SAupU^hRb>|!1P=E>)T5%f^P=Y=X={E)Xbi0|{45oTrv02;g=K>z>% delta 96 zcmZn(XmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD7`UYH$S8FVjcnJ&4wbsSp=DZ kvOplg4J2Ga3O5#hXP(Tj63D>_(atbAo@d%-Wid8p01*rjX#fBK diff --git a/03_simon_says/.DS_Store b/03_simon_says/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7c71df58dfe1aff903c7d0419731f9c74345f471 GIT binary patch literal 6148 zcmeHKJxc>Y5PhR5f+7SPEw{7^SlcWi7S{d%^%5b%#S=e3yKDU2`etSoFK%TaA~Ue_ zCcE?I_8w&S27t_-mdC&Zz?ef(G^m(PSC_U*@-|Vd#xtf^;Sp=Jl|p}UO835l8{A@v zYkb-NS=%j|wrjYq;+$|Cy_qor_V7T~8XcJzjIZ!QKgB(oAyZyAnEO?x*0|#i7S$RD z8#QzXVWB`M5DJ6>p}@}+;GL~D-gOKg3WNfoz^4LwJ|qssT(Ebvql3;`0OE+#W{mZ7 zSvjd-F4#MAg(glVI#o)GAx`IfDtWnJ@91<$X+ESp`J;IedpheUONV5~@S#8`FjPQ$ zU)n_P|5N^Ds!hJ7gbM{if&Z$2jOOR_8Q-qlt>4~L@7lz1$)Tosy#_SKgO33I&~xO_ d7JWTw%)DH%cho96Z**dO2$&$@LV;gU;2oO8I!*up literal 0 HcmV?d00001 diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb new file mode 100644 index 000000000..1cd3936de --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(who = nil) + if who + "Hello, #{who}!" + else + "Hello!" + end + end +end \ No newline at end of file From b92098037518221bf134bcbf2f7fcf2b24a5a993 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Wed, 4 Jan 2017 10:47:15 -0500 Subject: [PATCH 07/13] add book --- 08_book_titles/book.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 08_book_titles/book.rb diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb new file mode 100644 index 000000000..f26ba563a --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,12 @@ +class Book + def initialize + @title = '' + @untitlized_words = ['a', 'an', 'of', 'in', 'and', 'the', 'over'] + end + def title=(value) + @title = value.split(' ') + end + def title + @title.map.with_index { |word, index| @untitlized_words.include?(word) && index > 0 ? word : word.capitalize }.join(' ') + end +end \ No newline at end of file From 24502cc9524ded8f9c59dd3e5bb6c47f3f0abc1f Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Wed, 4 Jan 2017 10:55:26 -0500 Subject: [PATCH 08/13] add timer --- 09_timer/timer.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 09_timer/timer.rb diff --git a/09_timer/timer.rb b/09_timer/timer.rb new file mode 100644 index 000000000..55694112c --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,14 @@ +class Timer + def initialize + @seconds = 0 + end + def seconds + @seconds + end + def seconds=(value) + @seconds = value + end + def time_string + [@seconds / 3600, @seconds / 60 % 60, @seconds % 60].map { |t| t.to_s.rjust(2, '0') }.join(':') + end +end \ No newline at end of file From b6ae3035579526e52fc238ef0436ca4528173308 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Wed, 4 Jan 2017 11:10:46 -0500 Subject: [PATCH 09/13] add temperature --- 10_temperature_object/temperature.rb | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 10_temperature_object/temperature.rb diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..19dbeddf8 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,31 @@ +class Temperature + def initialize(options = {}) + @f = options[:f] + @c = options[:c] + end + def in_fahrenheit + return @f if @f + @c * 9 / 5.0 + 32 + end + def in_celsius + return @c if @c + (@f - 32) * 5 / 9.0 + end + def self.from_celsius(c) + Temperature.new(:c => c) + end + def self.from_fahrenheit(f) + Temperature.new(:f => f) + end +end + +class Celsius < Temperature + def initialize(t) + super(:c => t) + end +end +class Fahrenheit < Temperature + def initialize(t) + super(:f => t) + end +end \ No newline at end of file From ef3c8838a5983134f9e1668f04a506950741f25d Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Wed, 4 Jan 2017 11:37:57 -0500 Subject: [PATCH 10/13] add dictionary --- 11_dictionary/dictionary.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 11_dictionary/dictionary.rb diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..9ad2a5012 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,33 @@ +class Dictionary + def initialize + @entries = {} + end + def entries + @entries + end + def add(entry) + return @entries.merge!(entry) if entry.is_a?(Hash) + @entries[entry] = nil + end + def keywords + @entries.keys.sort + end + def include?(value) + keywords.include?(value) + end + def find(word) + results = {} + @entries.each do |key, value| + results[key] = value if key.match(/#{word}/) + end + results + end + def printable + s = '' + keywords.each do |key| + s += "[#{key}] \"#{@entries[key]}\"" + s += "\n" if keywords.index(key) != keywords.length - 1 + end + s + end +end \ No newline at end of file From ab25523491f20b0d2eef2ce5318fc3d23f9955cd Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Thu, 5 Jan 2017 10:44:06 -0500 Subject: [PATCH 11/13] add rpn calculator --- 12_rpn_calculator/rpn_calculator.rb | 66 +++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 12_rpn_calculator/rpn_calculator.rb diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..a9cb1941a --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,66 @@ +class RPNCalculator + attr_accessor :stack + attr_accessor :value + def initialize + @stack = [] + @value = 0 + end + def push(num) + @stack.push(num) + end + def operate + error_empty if @stack.length < 2 + second = @stack.pop + first = @stack.pop + new_value = yield(second, first) + @stack.push(new_value) + @value = new_value + end + def plus + operate { |second, first| first + second } + @value + end + def minus + operate { |second, first| first - second } + @value + end + def divide + operate { |second, first| first.to_f / second.to_f } + @value + end + def times + operate { |second, first| first * second } + @value + end + def error_empty + raise "calculator is empty" + end + def tokens(string) + string.split(" ").map do |token| + if token.match(/\d/) + token.to_i + elsif ['+', '-', '*', '/'].include?(token) + token.to_sym + end + end + end + def evaluate(string) + tokens(string).each do |token| + if token.is_a?(Integer) + push(token) + else + case token + when :+ + plus + when :- + minus + when :* + times + when :/ + divide + end + end + end + @value + end +end \ No newline at end of file From 0cead7df0d44ade739c5622eb3b846ecc0e88604 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Thu, 5 Jan 2017 10:52:39 -0500 Subject: [PATCH 12/13] add array extensions --- 14_array_extensions/array_extensions.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 14_array_extensions/array_extensions.rb diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..dcabcc5de --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,12 @@ +class Array + def sum + return 0 if self.empty? + self.inject(:+) + end + def square + self.map { |e| e**2 } + end + def square! + self.map! { |e| e**2 } + end +end \ No newline at end of file From 579fb03faa87a78b53b465e6c801a7c1b7e4d4a9 Mon Sep 17 00:00:00 2001 From: Egle Libby Date: Thu, 5 Jan 2017 12:20:22 -0500 Subject: [PATCH 13/13] add in words --- 15_in_words/in_words.rb | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 15_in_words/in_words.rb diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb new file mode 100644 index 000000000..94e7e45cc --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,42 @@ +class Fixnum + def in_words + words_map = {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'} + if self == 0 + return "zero" + end + words = "" + number = self + if number / 1_000_000_000_000 > 0 + words += (number / 1_000_000_000_000).in_words + " trillion " + number %= 1_000_000_000_000 + end + if number / 1_000_000_000 > 0 + words += (number / 1_000_000_000).in_words + " billion " + number %= 1_000_000_000 + end + if number / 1_000_000 > 0 + words += (number / 1_000_000).in_words + " million " + number %= 1_000_000 + end + if number / 1000 > 0 + words += (number / 1000).in_words + " thousand " + number %= 1000 + end + if number / 100 > 0 + words += (number / 100).in_words + " hundred " + number %= 100 + end + if number > 0 + if number < 20 + words += words_map[number] + else + words += words_map[(number.to_s[0].to_i) * 10] + if number % 10 > 0 + words += " " + words_map[number%10] + end + end + end + words.strip + end +end +