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 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 diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..d2da6ae22 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,19 @@ +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 + +def multiply + +end 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 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 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 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 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 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 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 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 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 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 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