diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..546ddf311 Binary files /dev/null and b/.DS_Store differ diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..375bf71b4 --- /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..3ee7e4f70 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f) + (f-32)*(5.0/9.0) +end + +def ctof(c) + f=(c*(9.0/5.0))+32 +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..9ff12f667 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,23 @@ +def add(x,y) + sum = x + y +end + +def subtract(x,y) + subtract = x - y +end + +def sum(array) + sum = 0 + array.each do |x| + sum += x + end + sum +end + +def multiply(num1, *nums) + product = num1 + product.each do |x| + pruduct *= x + end + product +end \ No newline at end of file diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..8b7c7e982 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,28 @@ +def echo(x) + x +end + +def shout(x) + x.upcase +end + +def repeat(word, n=2) + (word.split* n).join(" ") +end + +def start_of_word(s, n) + s[0...n] +end + +def first_word(word) + word.split[0] +end + +def titleize(word) + word.capitalize! + words = word.split(" ") + words.each do |x| + x.capitalize! unless ["and", "over", "the"].include? x + end + words.join(" ") +end diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..6fdaa9d19 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,73 @@ +# def translate(arr) +# vowels = %w{a e i o u y} +# consonants = %w{b c d f g h j k l m n p q r s t v w x y z} + +# a = arr.split.map do |word| +# if vowels.include?(word[0]) +# word + "ay" +# elsif word[0..1] == "qu" +# word[2..-1] + "quay" +# elsif word[0..2] == "sch" +# word[3..-1] + "schay" +# elsif word[0..2] == "squ" +# word[3..-1] + "squay" +# elsif consonants.include?(word[0]) && consonants.include?(word[1]) && consonants.include?(word[2]) +# word[3..-1] + word[0..2] + "ay" +# elsif consonants.include?(word[0]) && consonants.include?(word[1]) +# word[2..-1] + word[0..1] + "ay" +# elsif consonants.include?(word[0]) +# word[1..-1] + word[0] + "ay" +# end +# end + +# a.join(" ") +# end + + + # Word_array = Word.split + +def translate(arr) + + alpha = ('a'..'z').to_a + vowels = %w[a e i o u] + consonants = alpha - vowels + + a = arr.split.map do |word| + if vowels.include?(word[0]) + word + 'ay' + elsif word[0..1] == 'qu' + word[2..-1] + 'quay' + elsif word[0..2] == 'squ' + word[3..-1] + 'squay' + elsif consonants.include?(word[0]) && consonants.include?(word[1]) && consonants.include?(word[2]) + word[3..-1] + word[0..2] + 'ay' + elsif consonants.include?(word[0]) && consonants.include?(word[1]) + word[2..-1] + word[0..1] + 'ay' + else consonants.include?(word[0]) + word[1..-1] + word[0] + 'ay' + end + end + + a.join(" ") +end + + + + + +# def consonant?(character) +# Consonants.include?(character) +# end + + + +# def translate(word) +# if Vowels.include?(word[0]) +# word + 'ay' +# elsif Consonants.include?(word[0]) && +# Consonants.include?(word[1]) +# word[2..-1] + word[0..1] + 'ay' +# else Consonants.include?(word[0]) +# word[1..-1] + word[0..0] + 'ay' +# end +# 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..d9d076562 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,20 @@ +def reverser + + words = yield + output = [] + + words.split.map do |word| + output << word.reverse + end + output.join(' ') +end + +def adder(n=1) + yield + n +end + +def repeater(n=1) + n.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..3700d3386 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,10 @@ +def measure(n=1) + total = 0 + n.times do + start_time = Time.now + yield + end_time = Time.now + total += end_time - start_time + end + total / 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..81df5eee7 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(who=nil) + if who == nil + "Hello!" + else + "Hello, #{who}!" + 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..cd4e30ea1 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,39 @@ +# class Book + +# attr_accessor :title + +# def title=(some_word) +# # word_arr = some_word.split(' ') + +# # word_arr.map do |e| +# # e[0].capitalize +# @title = some_word.capitalize +# end + +# end + +class Book + def title + @title + end + + def title=(title) + special_words = %w(and in the of a an) + formatted_title = [] + + @title = title.split.each_with_index do |w,i| + case + when i == 0 + formatted_title << w.capitalize + + when i > 0 && !special_words.include?(w) + formatted_title << w.capitalize + + when special_words.include?(w) + formatted_title << w + end + end + + @title = formatted_title.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..b2cde07aa --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,26 @@ +class Timer + attr_accessor :seconds + def initialize + @seconds = 0 + end + + def time_string + [hours, minutes, m_seconds].map(&method(:padded)).join(":") + end + + def hours + seconds / 3600 + end + + def minutes + (seconds % 3600)/60 + end + + def m_seconds + (seconds % 60) + end + + def padded(n) + "#{n}".rjust(2, '0') + end +end \ No newline at end of file diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index 40b33f23f..b998bf1b2 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -44,17 +44,17 @@ # One way to implement the Timer is with a helper method. # Uncomment these specs if you want to test-drive that # method, then call that method from inside of time_string. - # - # describe 'padded' do - # it 'pads zero' do - # expect(@timer.padded(0)).to eq('00') - # end - # it 'pads one' do - # expect(@timer.padded(1)).to eq('01') - # end - # it "doesn't pad a two-digit number" do - # expect(@timer.padded(12)).to eq('12') - # end - # end + + describe 'padded' do + it 'pads zero' do + expect(@timer.padded(0)).to eq('00') + end + it 'pads one' do + expect(@timer.padded(1)).to eq('01') + end + it "doesn't pad a two-digit number" do + expect(@timer.padded(12)).to eq('12') + end + end end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..d25f80218 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,59 @@ +# Did not know what an options was or how to implement it. Found the use case on stack overflow and was able to implement it here. STill a little confusing but I get the general idea to have the option to use a has as an argument in your fuction. I was also able to re-use code from the previous temperature section. +class Temperature + attr_accessor :f, :c + + def initialize (options={}) #Utilized options hash for fahrenheit/celsius methods + @f = options[:f] + @c = options[:c] + + if f + @c = Temperature.ftoc(@f) #Converts fahrenheit to celsius using the Temperature.ftoc. Reused + else + @f = Temperature.ctof(@c) #Converts celcius to fahrenheit using the Temperature.ctof. Reused + end + end + + def in_fahrenheit + @f + end + + def in_celsius + @c + end + + # method to convert fahrenheit to celsius where 1 degree F is 5/9 of 1 degree C. Re-used this code from the first exercise. Took some trial and error to get *self to work but was able to implement. I understand it's useful when there are instance methods, class methods and variables in order to distinguish your class method. Still not 100% clear on it but makes sense in this example. + def self.ftoc(f) + (f-32) * (5.0/9) + end + + # method to convert celcius to fahrenheit + def self.ctof(c) + (c * (9/5.0)) + 32 + end + + # Factory Methods + def self.from_fahrenheit(temp) + self.new(:f => temp) #called on the Temperature class + end + + def self.from_celsius(temp) + self.new(:c => temp) #called on the Temperature class + end +end + +class Fahrenheit < Temperature #Fahrenheit subclass + + def initialize(temp) + @f = temp + @c = Temperature.ftoc(@f) + end +end + +class Celsius < Temperature #Celsius subclass + + def initialize(temp) + @c = temp + @f = Temperature.ctof(@c) + 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..cc6b969e4 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,55 @@ +#Creates your very own dictionary! +#Add words and definitions, search, and print them out. + +class Dictionary + + attr_accessor :entries + + def initialize(entries = {}) + @entries = entries + end + + def add(entry) + + if entry.class == String + entry = {entry => nil} + end + + entry.each do |key, value| + @entries[key] = value + end + end + + def keywords + keys = [] + + @entries.each do |key, value| + keys << key + end + keys.sort + end + + def include?(key) + @entries.keys.include?(key) + end + + def find(prefix) + found = {} + + @entries.each do |key, value| + if key =~ /^#{prefix}/ + found[key] = value + end + end + found + end + + def printable + print_out = [] + + @entries.sort.collect do |key, value| + print_out << "[#{key}] \"#{value}\"" + end + print_out.join("\n") + 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..17f37030d --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,79 @@ +class RPNCalculator + attr_accessor :calculator + + def initialize + @calculator = [] #set calculator instance to an empty array to hold values + end + + def push(num) + @calculator << num #adds the num to the calculator instance + end + + def plus + if @calculator.length < 2 #Check if there's anything to add + # puts "There's nothing to add!" + raise "calculator is empty" #Raise exception if there is nothing there + else + @calculator << @calculator.pop + @calculator.pop #adds the sum of the popped numbers to the @calculator instance + end + end + + def minus + if @calculator.length < 2 #Check if there's anything to subtract + # puts "There's nothing to add!" + raise "calculator is empty" #Raise exception if there is nothing there + else + num1 = @calculator.pop + num2 = @calculator.pop + @calculator << num2 - num1 #adds the value remaining of the subtraction of popped numbers to the @calculator instance + end + end + + def times + if @calculator.length < 2 #Check if there's anything to multiply + # puts "There's nothing to add!" + raise "calculator is empty" #Raise exception if there is nothing there + else + num1 = @calculator.pop + num2 = @calculator.pop + @calculator << num1 * num2 #adds the product of popped numbers to the @calculator instance + end + end + + def divide + if @calculator.length < 2 #Check if there's anything to multiply + # puts "There's nothing to add!" + raise "calculator is empty" #Raise exception if there is nothing there + else + num1 = @calculator.pop.to_f + num2 = @calculator.pop.to_f + @calculator << (num2 / num1) #adds the quotient of popped numbers to the @calculator instance (used the method .to_f to make sure it's a floating point) + end + end + + def value + @calculator.last #return last value of the array which is the calculator's most recent value + end + + def tokens(string) + string.split(" ").collect do |token| #split string into an array and iterate over the words. Initially tried + if token == "+" || token == "-" || token == "*" || token == "/" #this with .each but did not work. Found out + token.to_sym # that you should use .collect when you want to actually change array + else + token.to_i #if statement matches the special characters and if there's a match change the character + end #to symbol terminology a la ":)". If not then just return the integer value of token. I + end #admit I initially tried to use ":" + "token" but this was a pretty cool hack that I found. + end + + def evaluate(string) + @calculator = [] #assign an empty string to keep track of evaluations + tokens(string).each do |token| + push token if token.is_a?(Numeric) # if token is a number add it to the calculator + plus if token == :+ # add if token is the :+ symbol + minus if token == :- # subtract if token is :- symbol + times if token == :* # multiply if token is :* symbol + divide if token == :/ # divide if token is :/ symbol + 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..c13876ed7 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,19 @@ +# Re-used some code from the original app academy test here +class Array + + def sum + result = 0 + self.each do |num| #Iterate through every number on the list using self (instance of the class) + result += num # add each number to the result + end + result # return result which is the sum of all the numbers we iterated over + end + + def square + self.collect{ |num| num*num} # iterate through each number and produce a new array with the + end # square of that number (num*num) using collect method from before + + def square! + self.collect!{ |num| num*num} # iterate through every number of original array (square!) and produce a + end # new array with the square of that number (num*num) +end # ! modifies the original object it was called on (stackoverflow) \ No newline at end of file