diff --git a/session1/challenge/1_arithmetic.rb b/session1/challenge/1_arithmetic.rb index fbe40463..5df7d0fa 100644 --- a/session1/challenge/1_arithmetic.rb +++ b/session1/challenge/1_arithmetic.rb @@ -1,13 +1,16 @@ + # fill out the method below # then test to see if you did them correctly with # $ rake 1:1 # Given a number, return 20 less than, that number multiplied by 5 -# +# # arithmetic1(10) # => 30 # arithmeitc1(10.5) # => 32.5 # arithmeitc1(-6) # => -50 def arithmetic1(n) - # the code for this method goes in here + n * 5 - 20 end + +arithmetic1(10) diff --git a/session1/challenge/2_arithmetic.rb b/session1/challenge/2_arithmetic.rb index 21445b42..864570a2 100644 --- a/session1/challenge/2_arithmetic.rb +++ b/session1/challenge/2_arithmetic.rb @@ -9,4 +9,9 @@ # arithmetic2(-6, -7) # => -3.5 def arithmetic2(a, b) +if a < b + return a/2.0 +else + return b/2.0 +end end diff --git a/session1/challenge/3_simple_logic.rb b/session1/challenge/3_simple_logic.rb index c5f23c1c..54265178 100644 --- a/session1/challenge/3_simple_logic.rb +++ b/session1/challenge/3_simple_logic.rb @@ -8,5 +8,9 @@ # ten_twenty(6) # => 10 def ten_twenty(n) - # your code goes here + if n.even? + return 10 + elsif n.odd? + return 20 + end end diff --git a/session1/challenge/4_logic.rb b/session1/challenge/4_logic.rb index 6cc87dbc..496909a6 100644 --- a/session1/challenge/4_logic.rb +++ b/session1/challenge/4_logic.rb @@ -1,6 +1,6 @@ # A grad student at a local university thinks he has discovered a formula to -# predict what kind of grades a person will get. He says if you own less than -# 10 books, you will get a "D". If you own 10 to 20 books, you will get a "C", +# predict what kind of grades a person will get. He says if you own less than +# 10 books, you will get a "D". If you own 10 to 20 books, you will get a "C", # and if you own more than 20 books, you will get a "B". # He further hypothesizes that if you actually read your books, then you will # get a full letter grade higher in every case. @@ -10,4 +10,17 @@ # grade(15, true) # => "B" def grade(num_books, reads_books) -end \ No newline at end of file + if num_books < 10 && !reads_books + return "D" + elseif num_books < 10 && reads_books + return "C" +elseif num_books (10..20) && !reads_books +return "C" +elseif num_books (10..20) && reads_books +return "B" +elsif num_books > 20 && !reads_books + return "B" +elsif num_books > 20 &&reads_books + return "A" +end +end diff --git a/session1/challenge/5_string.rb b/session1/challenge/5_string.rb index 7901fc58..cfa605f5 100644 --- a/session1/challenge/5_string.rb +++ b/session1/challenge/5_string.rb @@ -1,8 +1,9 @@ # Given a string, replace every instance of sad to happy -# +# # add_more_ruby("The clowns were sad.") # => "The clowns were happy." # add_more_ruby("The sad dad said sad stuff.") # => "The happy dad said happy stuff." # add_more_ruby("Sad times are ahead!") # => "Happy times are ahead!" def add_more_ruby(string) + string.gsub("sad", "happy").gsub("Sad", "Happy") end diff --git a/session1/challenge/6_string.rb b/session1/challenge/6_string.rb index 0fc38d0f..e6d7af64 100644 --- a/session1/challenge/6_string.rb +++ b/session1/challenge/6_string.rb @@ -1,11 +1,12 @@ # You'll get a string and a boolean. # When the boolean is true, return a new string containing all the odd characters. # When the boolean is false, return a new string containing all the even characters. -# +# # If you have no idea where to begin, remember to check out the cheatsheets for string and logic/control -# +# # odds_and_evens("abcdefg",true) # => "bdf" # odds_and_evens("abcdefg",false) # => "aceg" def odds_and_evens(string, return_odds) + string.chars.select.with_index{|_, i| return_odds ? i.odd? : i.even?} end diff --git a/session1/challenge/7_string.rb b/session1/challenge/7_string.rb index 4d4ea2a8..801545a0 100644 --- a/session1/challenge/7_string.rb +++ b/session1/challenge/7_string.rb @@ -1,8 +1,22 @@ # given a string, return the character after every letter "r" -# +# # pirates_say_arrrrrrrrr("are you really learning Ruby?") # => "eenu" # pirates_say_arrrrrrrrr("Katy Perry is on the radio!") # => "rya" # pirates_say_arrrrrrrrr("Pirates say arrrrrrrrr") # => "arrrrrrrr" def pirates_say_arrrrrrrrr(string) + to_return = "" + add_next = false + string.size.times do |index| + current_char = string[index] + to_return << current_char if add_next + add_next = (current_char == "r" || current_char == "R") + end + to_return end + + + +# def pirates_say_arrrrrrrrr(string) +# string.gsub(/(? 99 + self.beers = beers +end + +def print_song + beers.downto 1 do |i| + print_stanza i + end +end + +def print_stanza(n) + if n.zero? + String.new + else + puts "#{translate n} #{bottle n} of beer on the wall," + "#{translate n} #{bottle n} of beer," + "Take on down, pass it around," + "#{translate n - 1} #{bottle n - 1} of beer on the wall." + end +end + +def bottle(n) + if n == 1 then 'bottle' else 'bottles' end + end + + def translate(n) + if 0 <= n && n <= 19 + %w(zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fiftenn sixteen seventeen eighteen nineteen) + elseif n % 10 == 0 + %w(zero ten twenty thirty forty fifty sixty seventy eigthy ninety)[n/10] + else + "#{translate n/10*10}-#{translate n%10}".downcase + end.capitalize +end +end diff --git a/session2/challenge/1_input_output.rb b/session2/challenge/1_input_output.rb index 06622c97..5de445d0 100644 --- a/session2/challenge/1_input_output.rb +++ b/session2/challenge/1_input_output.rb @@ -3,10 +3,14 @@ # Write a program that reads in two integers typed on the keybaord # and outputs their sum, difference, and product -# +# # Standard input will be like "9 2\n" and will expect you to print # "11\n7\n18\n" to standard output. + def sum_difference_product - # your code goes here -end \ No newline at end of file + a , b = gets.split.map { |num| num.to_i } + puts a + b + puts a - b + puts a * b +end diff --git a/session2/challenge/2_input_output_control.rb b/session2/challenge/2_input_output_control.rb index c618f11a..c4f23ab2 100644 --- a/session2/challenge/2_input_output_control.rb +++ b/session2/challenge/2_input_output_control.rb @@ -1,5 +1,5 @@ # Prompt the user for a number, then read it in and print out "hi" that many times -# +# # Repeat this process until the user submits "bye", then say "goodbye" and end the program # HINT: Check out example 2 if you get stuck @@ -18,14 +18,24 @@ # remember you can try your program out with $ ruby 2_input_output_control.rb # and when you think it is correct, you can test it with $ rake 2:2 +def prompt +puts "Enter a number or bye" +end + def hi_hi_goodbye - # your code here + prompt +while (line = gets) && (line !~ /bye/) + line.to_i.times { print 'hi ' } + puts prompt end +puts "Goodbye" +end + # This will just invoke the method if you run this program directly -# This way you can try it out by running "$ ruby 2_input_output_control.rb" +# This way you can try it out by running "$ ruby 2_input_output_control.rb" # but it will still work for our tests -hi_hi_goodbye if $0 == __FILE__ \ No newline at end of file +hi_hi_goodbye if $0 == __FILE__ diff --git a/session2/challenge/3_array.rb b/session2/challenge/3_array.rb index 8fa14a40..42c80c41 100644 --- a/session2/challenge/3_array.rb +++ b/session2/challenge/3_array.rb @@ -7,5 +7,10 @@ class String def every_other_char + to_return = '' + each_char.with_index do |char, index| + to_return << char if index.even? + end + to_return end end diff --git a/session2/challenge/4_array.rb b/session2/challenge/4_array.rb index 1f4d1952..d5d19a00 100644 --- a/session2/challenge/4_array.rb +++ b/session2/challenge/4_array.rb @@ -7,3 +7,7 @@ # get_squares [25, 4, 9, 6, 50, 16, 5] # => [4, 5] # This time you will have to define the method, it's called: get_squares + +def get_squares(numbers) + numbers.select { |n| numbers.include? n*n }.sort +end diff --git a/session2/challenge/5_array.rb b/session2/challenge/5_array.rb index 2f7de799..98c57f26 100644 --- a/session2/challenge/5_array.rb +++ b/session2/challenge/5_array.rb @@ -1,7 +1,7 @@ -# Write a function named mod_three which takes an array of numbers, +# Write a function named mod_three which takes an array of numbers, # and return a new array consisting of their remainder when divided by three. # Exclude any numbers which are actually dividible by three. -# +# # EXAMPLES: # mod_three [0] # => [] # mod_three [1] # => [1] @@ -13,3 +13,7 @@ # mod_three [7] # => [1] # # mod_three [0,1,2,3,4,5,6,7] # => [1, 2, 1, 2, 1] + +def mod_three(numbers) + numbers.select { |number| number % 3 != 0 }.map { |number| % 3 } +end diff --git a/session2/challenge/6_array.rb b/session2/challenge/6_array.rb index a233b0bf..a479687c 100644 --- a/session2/challenge/6_array.rb +++ b/session2/challenge/6_array.rb @@ -1,16 +1,30 @@ # Write a method named prime_chars? which takes array of strings -# and returns true if the sum of the characters is prime. -# -# Remember that a number is prime if the only integers that can divide it with no remainder are 1 and itself. -# +# and returns true if the sum of the characters is prime. +# +# Remember that a number is prime if the only integers that can divide it with no remainder are 1 and itself. +# # Examples of length three # prime_chars? ['abc'] # => true # prime_chars? ['a', 'bc'] # => true # prime_chars? ['ab', 'c'] # => true # prime_chars? ['a', 'b', 'c'] # => true -# +# # Examples of length four # prime_chars? ['abcd'] # => false # prime_chars? ['ab', 'cd'] # => false # prime_chars? ['a', 'bcd'] # => false # prime_chars? ['a', 'b', 'cd'] # => false + +class Integer + def prime? + return false if self < 2 + 2.upto Math.sqrt(self) do |i| + return false if self % i == 0 + end + true + end +end + +def prime_chars?(strings) + strings.join.length.prime? +end diff --git a/session2/challenge/7_array.rb b/session2/challenge/7_array.rb index 346f8307..200c9ec2 100644 --- a/session2/challenge/7_array.rb +++ b/session2/challenge/7_array.rb @@ -3,8 +3,21 @@ # In order to not have to write an actual language parser, there won't be any punctuation too complex. # There will be no "'" that is not part of a contraction. # Assume each of these charactsrs are not to be considered: ! @ $ # % ^ & * ( ) - = _ + [ ] : ; , . / < > ? \ | -# +# # Examples # alternate_words("Lorem ipsum dolor sit amet.") # => ["Lorem", "dolor", "amet"] # alternate_words("Can't we all get along?") # => ["Can't", "all", "along"] # alternate_words("Elementary, my dear Watson!") # => ["Elementary", "dear"] + +def alternate_words(sentence) + # this will get better when we learn regular expressions :) + '!@$#%^&*()-=_+[]:;,./<>?\\|'.split(//).each do |char| + sentence = sentence.gsub(char, ' ') + end + words = sentence.split + solution = [] + words.each_with_index do |word, index| + solution << word if index.even? + end + solution +end diff --git a/session2/challenge/8_array.rb b/session2/challenge/8_array.rb index 860850a0..81ab94aa 100644 --- a/session2/challenge/8_array.rb +++ b/session2/challenge/8_array.rb @@ -1,7 +1,14 @@ # Given an array of elements, return true if any element shows up three times in a row -# +# # Examples: # got_three? [1, 2, 2, 2, 3] # => true # got_three? ['a', 'a', 'b'] # => false # got_three? ['a', 'a', 'a'] # => true # got_three? [1, 2, 1, 1] # => false + +def got_three?(elements) + elements.each_cons 3 do |a, b, c| + return true if a == b && b == c + end + false +end diff --git a/session2/challenge/9_input_output_logic_string.rb b/session2/challenge/9_input_output_logic_string.rb index 626119b3..2ff47300 100644 --- a/session2/challenge/9_input_output_logic_string.rb +++ b/session2/challenge/9_input_output_logic_string.rb @@ -23,14 +23,28 @@ # GRANDMA: HUH?! SPEAK UP, SONNY! # USER: BYE -def deaf_grandma +def prompt +puts "Say hi to your grandma!" +end +def deaf_grandma + prompt + while line = gets + line.chomp! + break if line == "BYE" + if line == line.upcase && line != "" + puts "NO, NOT SINCE 1938!" + else + puts "HUH?! SPEAK UP, SONNY!" + end + end end + # This will call your code so you can run it from the terminal. # But not call it otherwise, so that it will work with our tests. deaf_grandma if $0 == __FILE__