diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..6a559c055 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,8 @@ +#write your code here +def hello + "Hello!" +end + +def greet which_person + "Hello, " + which_person + "!" +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..78d1cd471 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,8 @@ +#write your code here +def ftoc farenheit + centigrade = (farenheit - 32.0) * 5.0 / 9.0 +end + +def ctof centigrade + farenheit = centigrade * 9.0 / 5.0 + 32.0 +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..2ee6a2929 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,49 @@ +#write your code here +def add first_num, second_num + result = first_num + second_num +end + +def subtract first_num, second_num + result = first_num - second_num +end + +def sum array + result = 0; + array.length.times do |i| + result = result + array[i] + end + result +end + +def multiply first_num, second_num + result = first_num * second_num +end + +def multiply_arr array + result = 1; + array.length.times do |i| + result = result * array[i] + end + result +end + +def power first_num, second_num + result = first_num + (second_num - 1).times do + result = result * first_num + end + result +end + +def fact number + result = 1 + number.times do |i| + result = result * (i + 1) + end + if(number == 0) + 0 + else + result + end +end + diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..f00c66330 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,46 @@ +#write your code here +def echo what_to_echo + what_to_echo +end + +def shout what_to_shout + what_to_shout.upcase +end + +def repeat what_to_repeat, how_many_times = 2 + result = "" + (how_many_times - 1).times do + result = result + what_to_repeat + " " + end + result = result + what_to_repeat +end + +def start_of_word the_word, how_many_letters + how_many_letters_m_1 = how_many_letters - 1 + result = the_word[0..how_many_letters_m_1] +end + +def first_word the_sentence + the_first_word = the_sentence.split + the_first_word[0] +end + +def titleize the_sentence + little_words = ["and", "or", "over", "but", "the"] + the_split_version = the_sentence.split + (the_split_version.length - 1).times do |i| + if(i != 0) + if(little_words.include? the_split_version[i]) + the_split_version[i] = the_split_version[i] + " " + else + the_split_version[i].capitalize! + the_split_version[i] = the_split_version[i] + " " + end + else + the_split_version[i].capitalize! + the_split_version[i] = the_split_version[i] + " " + end + end + the_split_version[the_split_version.length - 1].capitalize! + the_split_version.join +end diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..9f3605685 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,45 @@ +def translate the_sentence + vowels_array = ["a", "e", "i", "o" , "u"] + the_words = the_sentence.split + the_words.length.times do |i| + the_word = the_words[i] + if (vowels_array.include? the_word[0]) + the_word = the_word + "ay" + else + the_word = word_piggy(the_word) + if (vowels_array.include? the_word[0]) + the_word = word_piggy(the_word) + "ay" + else + the_word = word_piggy(the_word) + if (vowels_array.include? the_word[0]) + the_word = word_piggy(the_word) + "ay" + else + the_word = word_piggy(the_word) + if (vowels_array.include? the_word[0]) + the_word = word_piggy(the_word) + "ay" + else + the_word = word_piggy(the_word) + end + end + end + end + the_word = the_word + " " + the_words[i] = the_word + end + new_words = the_words.join + new_words.chomp(" ") +end + +def word_piggy the_word + vowels_array = ["a", "e", "i", "o" , "u"] + if (the_word[0..1] == "qu") + x = the_word.length - 1 + new_word = the_word[2..x] + "qu" + elsif (vowels_array.include? the_word[0]) + new_word = the_word + else + x = the_word.length - 1 + new_word = the_word[1..x] + the_word[0] + end + new_word +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..e9e779a33 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,22 @@ +def reverser + str = yield + str_arr = str.split(" ") + str_arr.each do |string| + string.reverse! + end + new_arr = [] + str_arr.size.times do |index| + new_arr << str_arr[index] + new_arr << " " + end + new_arr.join.strip +end + +def adder (num = 1) + value = yield + value += num +end + +def repeater (num = 1) + num.times {yield} +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..328b2b4f3 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,11 @@ +def measure(num = 1) + cur_time = Time.now + all_times = [] + num.times { + cur_time = Time.now + yield + after_time = Time.now + all_times << after_time - cur_time + } + all_times.inject(0) { |sum, el| sum + el } / all_times.size +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..529400ce4 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,5 @@ +class Friend + def greeting (string = "") + (string.size > 0) ? "Hello, #{string}!" : "Hello!" + 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..68cae77ae --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,21 @@ +class Book +# write your code here + attr_accessor :title + def initialize + + end + def title=(title) + exceptions = ["a", "the", "and", "an", "in", "and", "of"] + title_words = title.split + title_words.length.times do |i| + if((i != 0) && (exceptions.include? title_words[i])) + title_words[i] = title_words[i] + " " + else + title_words[i].capitalize! + title_words[i] = title_words[i] + " " + end + end + @title = title_words.join + @title = @title.chomp(" ") + 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..c0f888c9b --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,32 @@ +class Timer + #write your code here + attr_accessor :hours, :minutes, :seconds, :time_string + + def initialize + @seconds = 0 + @hours = 0 + @minutes = 0 + @time_string = "00:00:00" + end + def seconds=(seconds) + @seconds = seconds + if(seconds > 3599) + @hours = seconds / 3600 + @minutes = (seconds - (@hours * 3600)) / 60 + @seconds = seconds - (@hours * 3600) - (@minutes * 60) + elsif(seconds > 59) + @seconds = seconds % 60 + @minutes = seconds / 60 + @hours = 0 + else + @seconds = seconds + @hours = 0 + @minutes = 0 + end + second_string = "%0.2d" % [@seconds] + minute_string = "%0.2d" % [@minutes] + hour_string = "%0.2d" % [@hours] + @time_string = hour_string + ":" + minute_string + ":" + second_string + end + +end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..9b74bd9f3 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,41 @@ +class Temperature + @in_fahrenheit + @in_celsius + def initialize(options={}) + if options[:f] + @in_fahrenheit = options[:f] + in_celsius + end + if options[:c] + @in_celsius = options[:c] + in_fahrenheit + end + end + + def self.from_celsius(cel_deg) + new(:c => cel_deg) + end + def self.from_fahrenheit(far_deg) + new(:f => far_deg) + end + + def in_fahrenheit + @in_fahrenheit = (@in_celsius) * 9.0 / 5.0 + 32.0 + end + + def in_celsius + @in_celsius = (@in_fahrenheit - 32.0) * 5.0 / 9.0 + end +end + +class Celsius < Temperature + def initialize(deg) + super(:c => deg) + end +end + +class Fahrenheit < Temperature + def initialize(deg) + super(:f => deg) + 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..9e479342a --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,45 @@ +class Dictionary + @entries + @printable_output + def initialize + @entries = {} + end + def entries + @entries + end + def add(hash={}) + if hash.is_a? Hash + hash.each do |key, value| + @entries[key] = value + end + else + @entries[hash] = nil + end + @entries + end + def keywords + @entries.keys.sort + end + + def include?(string) + @entries.include? string + end + + def find(string) + result = Hash.new + if @entries.empty? || string == 'nothing' + {} + else + @entries.select{|key, value| key.include? string } + end + end + + def printable + @printable_output = "" + @sorted_entries = @entries.sort_by {|key, value| key} + @sorted_entries.each do |key, value| + @printable_output = @printable_output + "\[#{key}\]" + " \"#{value}\"" + "\n" + end + @printable_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..93b0e2701 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,107 @@ +class RPNCalculator + @calculator + @value + @value1 + @value2 + def plus + if @calculator.size > 1 + @value1 = @calculator.pop + @value2 = @calculator.pop + @value = @value2 + @value1 + @calculator << @value + else + begin + raise "calculator is empty" + end + end + end + + def minus + if @calculator.size > 1 + @value1 = @calculator.pop + @value2 = @calculator.pop + @value = @value2 - @value1 + @calculator << @value + else + begin + raise "calculator is empty" + end + end + end + + def divide + if @calculator.size > 1 + @value1 = @calculator.pop + @value2 = @calculator.pop + @value = @value2.to_f / @value1.to_f + @calculator << @value + else + begin + raise "calculator is empty" + end + end + end + + def times + if @calculator.size > 1 + @value1 = @calculator.pop + @value2 = @calculator.pop + @value = @value2.to_f * @value1.to_f + @calculator << @value + else + begin + raise "calculator is empty" + end + end + end + + def initialize + @calculator = [] + end + + def push(num) + @calculator << num + end + + def value + @value + end + + def tokens(string) + tokens_string = string.split(" ") + tokens_array = [] + tokens_string.each do |item| + case item + when "+" + tokens_array << item.to_sym + when "-" + tokens_array << item.to_sym + when "*" + tokens_array << item.to_sym + when "/" + tokens_array << item.to_sym + else + tokens_array << item.to_i + end + end + tokens_array + end + + def evaluate(string) + tokens_array = tokens(string) + tokens_array.each do |item| + if item.is_a? Integer + push(item) + elsif item.to_s == "+" + plus + elsif item.to_s == "-" + minus + elsif item.to_s == "*" + times + elsif item.to_s == "/" + divide + end + end + @value + 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..8da7af934 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,23 @@ +class Array + def sum + if self.size < 1 + 0 + else + self.inject(0) {|sum, item| sum += item} + end + end + def square + if self.size < 1 + [] + else + self.map {|item| item ** 2} + end + end + def square! + if self.size < 1 + [] + else + self.map! {|item| item ** 2} + 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..c9e98cc55 --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,162 @@ +class Fixnum + + + def spit_out_string (index, which) + onesPlace = ['one', 'two', 'three', 'four', 'five', + 'six', 'seven', 'eight', 'nine'] + tensPlace = ['ten', 'twenty', 'thirty', 'forty', 'fifty', + 'sixty', 'seventy', 'eighty', 'ninety'] + teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', + 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + if which == 1 + onesPlace[index] + elsif which == 2 + tensPlace[index] + else + teenagers[index] + end + end + + def in_words + if self < 0 # No negative numbers. + return 'Please enter a number that isn\'t negative.' + end + if self == 0 + return 'zero' + end + numString = '' # This is the string we will return. + + left = self + + write = left/1000000000000 # How many hundreds left to write out? + left = left - write*1000000000000 # Subtract off those hundreds. + + if write > 0 + trillions = less_than_trillions write + numString = numString + trillions + ' trillion' + + if left > 0 + numString = numString + ' ' + end + end + numString = numString + less_than_trillions(left) + + numString + end + + def less_than_10 (number) + left = number + numString = "" + write = left/10 # How many tens left to write out? + left = left - write*10 # Subtract off those tens. + + if write > 0 + if ((write == 1) and (left > 0)) + index = left-1 + numString = numString + spit_out_string(index, 3) + left = 0 + else + index = write-1 + numString = numString + spit_out_string(index, 2) + end + + if left > 0 + numString = numString + ' ' + end + end + + write = left # How many ones left to write out? + left = 0 # Subtract off those ones. + + if write > 0 + index = write-1 + numString = numString + spit_out_string(index, 1) + end + + numString + end + + def less_than_hundred (number) + numString = "" + numString = numString + less_than_10(number) + numString + end + + def less_than_thousand (number) + numString = "" + left = number + write = left/100 # How many hundreds left to write out? + left = left - write*100 # Subtract off those hundreds. + + if write > 0 + hundreds = less_than_hundred write + numString = numString + hundreds + ' hundred' + if left > 0 + numString = numString + ' ' + end + end + numString = numString + less_than_10(left) + + numString + end + + def less_than_millions (number) + numString = "" + left = number + write = left/1000 # How many hundreds left to write out? + left = left - write*1000 # Subtract off those hundreds. + + if write > 0 + thousands = less_than_thousand write + numString = numString + thousands + ' thousand' + + if left > 0 + numString = numString + ' ' + end + end + numString = numString + less_than_thousand(left) + + numString + end + + def less_than_billions (number) + numString = "" + left = number + write = left/1000000 # How many hundreds left to write out? + left = left - write*1000000 # Subtract off those hundreds. + + if write > 0 + millions = less_than_millions write + numString = numString + millions + ' million' + + if left > 0 + numString = numString + ' ' + end + end + + numString = numString + less_than_millions(left) + + numString + end + + def less_than_trillions (number) + numString = "" + + left = number + write = left/1000000000 # How many hundreds left to write out? + left = left - write*1000000000 # Subtract off those hundreds. + + if write > 0 + billions = less_than_billions write + numString = numString + billions + ' billion' + + if left > 0 + numString = numString + ' ' + end + end + + numString = numString + less_than_billions(left) + + numString + end +end \ No newline at end of file