From 0558fe6e1d84721e2f7fbe9f1b46681ede3bd033 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Thu, 20 Oct 2016 17:14:05 -0400 Subject: [PATCH 01/15] 00_hello completed --- 00_hello/hello.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 00_hello/hello.rb diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..06b0c5b42 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def greet(name) + return "Hello, #{name}!" +end + +def hello + return "Hello!" +end \ No newline at end of file From 274b840de6d9d878740c287cb3e81c44be5bb9b2 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Thu, 20 Oct 2016 17:59:35 -0400 Subject: [PATCH 02/15] finished 01_temperature --- 01_temperature/temperature.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 01_temperature/temperature.rb diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..003b02731 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,9 @@ +def ftoc(fahrenheit) + celsius = (fahrenheit-32)*5/9 + return celsius +end + +def ctof(celsius) + fahrenheit = (celsius*9.0/5.0)+32 + return fahrenheit +end \ No newline at end of file From 8fbd5ba1f69b0960df937fac6abf333ef58a44c1 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Fri, 21 Oct 2016 13:37:26 -0400 Subject: [PATCH 03/15] finished calculator.rb --- 02_calculator/calculator.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 02_calculator/calculator.rb diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..8ff2a8070 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,15 @@ +def add(num1, num2) + sum = num1 + num2 + return sum +end + +def subtract(num1, num2) + difference = num1 - num2 + return difference +end + +def sum(arr) + total = 0 + arr.each{|x| total += x } + return total +end \ No newline at end of file From 1c08e7cafed768aa411661b5e4c1fc927e009c3e Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 00:01:55 -0400 Subject: [PATCH 04/15] finished simon_says.rb --- 02_calculator/calculator.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb index 8ff2a8070..d2da6ae22 100644 --- a/02_calculator/calculator.rb +++ b/02_calculator/calculator.rb @@ -12,4 +12,8 @@ def sum(arr) total = 0 arr.each{|x| total += x } return total -end \ No newline at end of file +end + +def multiply + +end From d215924899466e49c981044471811584252ccb6b Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 00:38:41 -0400 Subject: [PATCH 05/15] mostly finished pig_latin --- 03_simon_says/simon_says.rb | 42 +++++++++++++++++++++++++++++++++++++ 04_pig_latin/pig_latin.rb | 23 ++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 03_simon_says/simon_says.rb create mode 100644 04_pig_latin/pig_latin.rb diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..d0fb33f07 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,42 @@ +def echo(str) + return str +end + +def shout(str) + return str.upcase +end + +def repeat(str, n=2) +# array where each element is str and there are n of them. + arr = [] + n.times{arr.push(str)} + return arr.join(" ") +# then i can join them with a space and return that + # return str+" "+str +end + +def start_of_word(word, int) + word[0..(int-1)] +end + +def first_word(str) + arr = str.split(" ") + return arr[0] +end + +def titleize(str) + little_words = ['and', 'the', 'over'] + arr = str.split(" ") + upcase_array = [] + i = 0 + arr.each do |word| + i += 1 + if little_words.include?(word) && i != 1 + upcase_array.push(word) + else + upcase_array.push(word.capitalize) + end + end + new_str = upcase_array.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..c6058c945 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,23 @@ +def translate(word) + vowels = ['a','e','i','o','u'] + + pre_latin = word.split(" ") + + arr=[] + + pre_latin.each do |piggy| + + if vowels.include?(piggy[0]) + arr.push(piggy + "ay") + elsif piggy[0..1] == "qu" + arr.push(piggy[2..(piggy.length-1)] + piggy[0..1] + "ay") + elsif vowels.include?(piggy[1]) + arr.push(piggy[1..(piggy.length-1)] + piggy[0] + "ay") + elsif vowels.include?(piggy[2]) + arr.push(piggy[2..(piggy.length-1)] + piggy[0..1] + "ay") + else + arr.push(piggy[3..(piggy.length-1)] + piggy[0..2] + "ay") + end + end + latinized = arr.join(" ") +end \ No newline at end of file From 05ab23567ad3198df85de3b63846a7a909dd5cf7 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 00:56:07 -0400 Subject: [PATCH 06/15] finished silly_blocks.rb --- 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..6064bab7e --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,14 @@ +def reverser + reversed = yield.split(" ").map{|word| word.reverse} + return reversed.join(" ") +end + +def adder(n=1) + yield + n +end + +def repeater(n=1) + block_was_executed = false + n.times { yield } + block_was_executed = true +end \ No newline at end of file From aaac92585a93888f7a65967944c28bb21d8a5a54 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 01:06:02 -0400 Subject: [PATCH 07/15] finished performance_monitor.rb --- 06_performance_monitor/performance_monitor.rb | 5 +++++ 1 file changed, 5 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..61b4c1b37 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,5 @@ +def measure(n=1) + start_time = Time.now + n.times{ yield } + elapsed_time = (Time.now - start_time)/n +end \ No newline at end of file From 35c88f73ef57bcd51b29b233279d2ffa370a9e02 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 01:15:55 -0400 Subject: [PATCH 08/15] finished friend.rb --- 07_hello_friend/friend.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 07_hello_friend/friend.rb diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb new file mode 100644 index 000000000..805b7da3b --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(name=nil) + if name != nil + "Hello, #{name}!" + else + "Hello!" + end + end +end \ No newline at end of file From df72f302fcacccb7c8372932e10d3ca512e3c8cf Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 02:44:13 -0400 Subject: [PATCH 09/15] finished book.rb --- 08_book_titles/book.rb | 21 +++++++++++++++++++++ 1 file changed, 21 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..84280e4db --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,21 @@ +class Book + + def title=(title) + no_nos = ['and','in','the','of','a','an'] + i = 0 + + @title = title.split(" ").map! do |word| + i+=1 + if !no_nos.include?(word) || i == 1 + word.capitalize + else + word + end + end.join(" ") + end + + def title + @title + end + +end \ No newline at end of file From ef17556a09d5021aa8c492936d8b8783bbb77cd1 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 03:10:41 -0400 Subject: [PATCH 10/15] finished timer.rb --- 09_timer/timer.rb | 20 ++++++++++++++++++++ 1 file changed, 20 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..493540853 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,20 @@ +class Timer + + def initialize + self.seconds = (0) + end + + def seconds=(seconds) + @seconds = seconds + @time_string = Time.at(seconds).utc.strftime("%H:%M:%S") + end + + def seconds + @seconds + end + + def time_string + @time_string + end + +end \ No newline at end of file From a3cd67945bec88397a9fde8431c26f3bbed42d74 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 21:33:12 -0400 Subject: [PATCH 11/15] finished temperature.rb --- 10_temperature_object/temperature.rb | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 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..ed3e78154 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,49 @@ +class Temperature + attr_accessor :f, :c + + def Temperature.from_celsius temp + Temperature.new(:c => temp) + end + + def Temperature.from_fahrenheit temp + Temperature.new(:f => temp) + end + + def initialize args + @f = args[:f] + @c = args[:c] + if @f + @c = ftoc + else + @f = ctof + end + end + + def ftoc + (@f-32)*5/9.0 + end + + def ctof + (@c*9/5.0)+32 + end + + def in_fahrenheit + @f + end + + def in_celsius + @c + end +end + +class Celsius < Temperature + def Celsius.new args + Temperature.new(:c => 50) + end +end + +class Fahrenheit < Temperature + def Fahrenheit.new args + Temperature.new(:f => 50) + end +end \ No newline at end of file From 02a8e71a62f3d1425832280a995f1bedcdcd5a35 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 22:25:25 -0400 Subject: [PATCH 12/15] finished dictionary.rb --- 11_dictionary/dictionary.rb | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 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..8fc1cf25a --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,38 @@ +class Dictionary + + attr_reader :entries + + def initialize + @entries = {} + end + + def add items + if items.is_a?(Hash) + items.each {|key, definition| @entries[key] = definition} + else + @entries[items] = nil + end + @entries = Hash[@entries.sort_by {|key, definition| key}] + end + + def find partial + @entries.select {|key, definition| key.include? partial} + end + + def include? key + @entries.has_key? key + end + + def keywords + @entries.keys + end + + def printable + output = "" + @entries.each do |key, definition| + output += "[#{key}] \"#{definition}\"\n" + end + output.chomp + end + +end \ No newline at end of file From f8d4c0ae4db84fe78a9010f2df8218f11989d74e Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 22:39:51 -0400 Subject: [PATCH 13/15] finished rpn_calculator.rb --- 12_rpn_calculator/rpn_calculator.rb | 49 +++++++++++++++++++++++++++++ 1 file changed, 49 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..137c28906 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,49 @@ +class RPNCalculator + + attr_reader :value + + def initialize + @value = 0 + @entered = [] + end + + def divide + operate &:/ + end + + def evaluate formula + formula = tokens formula + formula.each do |token| + (token.class == Symbol ? operate(&token) : push(token)) + end + return @value + end + + def minus + operate &:- + end + + def operate &operator + raise "calculator is empty" if @entered.length < 2 + @value = @entered.push(@entered.pop(2).reduce &operator)[-1] + end + + def plus + operate &:+ + end + + def push num + @entered << num.to_f + end + + def times + operate &:* + end + + def tokens token_list + token_list.split(" ").map do |token| + (token.to_i.to_s == token ? token.to_i : token.to_sym) + end + end + +end \ No newline at end of file From 229f3ab3d7fde7158ed003945eff1f5fbf357b19 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 23:07:03 -0400 Subject: [PATCH 14/15] finished array_extensions.rb --- 13_xml_document/xml_document.rb | 5 +++++ 14_array_extensions/array_extensions.rb | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 13_xml_document/xml_document.rb create mode 100644 14_array_extensions/array_extensions.rb diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb new file mode 100644 index 000000000..3e38ee13e --- /dev/null +++ b/13_xml_document/xml_document.rb @@ -0,0 +1,5 @@ +class XmlDocument + + def hello + end +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..aaac07cc3 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,24 @@ +class Array + + def sum + result = 0 + self.each do |i| + result += i + end + result + end + + def square + result = [] + self.map do |i| + result << i * i + end + result + end + + def square! + self.map! do |i| + i * i + end + end +end \ No newline at end of file From 07c0fc62be5f858dc3a3281b3be4c747b5f617d9 Mon Sep 17 00:00:00 2001 From: Jake Tingley Date: Sun, 30 Oct 2016 23:34:19 -0400 Subject: [PATCH 15/15] finished in_words.rb --- 15_in_words/in_words.rb | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 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..8502f404a --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,68 @@ +class Fixnum + def in_words + case + when self >= 1_000_000_000_000 then hundreds_and_up(1_000_000_000_000) + when self >= 1_000_000_000 then hundreds_and_up(1_000_000_000) + when self >= 1_000_000 then hundreds_and_up(1_000_000) + when self >= 1000 then hundreds_and_up(1_000) + when self >= 100 then hundreds_and_up(100) + when self/10 > 1 #tens + return tens(self/10) if self%10 == 0 + return tens(self/10) + " " + singles(self%10) + when self/10 == 1 # teens + return 'ten' if self == 10 + return 'eleven' if self == 11 + return 'twelve' if self == 12 + return teen_beginnings(self%10) + 'teen' + when self/10 == 0 + return singles(self) + else + return "Sorry, too high!" + end + end + + def descriptor(place) + return " trillion" if place == 1_000_000_000_000 + return " billion" if place == 1_000_000_000 + return " million" if place == 1_000_000 + return " thousand" if place == 1000 + return " hundred" + end + + def hundreds_and_up(place) + return (self/place).in_words + descriptor(place) if self%place == 0 + return (self/place).in_words + descriptor(place) + " " + (self%place).in_words + end + + def singles(n) + return 'zero' if n == 0 + return 'one' if n == 1 + return 'two' if n == 2 + return 'three' if n == 3 + return 'four' if n == 4 + return 'five' if n == 5 + return 'six' if n == 6 + return 'seven' if n == 7 + return 'eight' if n == 8 + return 'nine' if n == 9 + end + + def teen_beginnings(n) + return 'thir' if n == 3 + return 'four' if n == 4 + return 'fif' if n == 5 + return 'eigh' if n == 8 + return singles(n) + end + + def tens(n) + return 'twenty' if n == 2 + return 'thirty' if n == 3 + return 'forty' if n == 4 + return 'fifty' if n == 5 + return 'sixty' if n == 6 + return 'seventy' if n == 7 + return 'eighty' if n == 8 + return 'ninety' if n == 9 + end +end \ No newline at end of file