diff --git a/session1/3-challenge/1_arithmetic.rb b/session1/3-challenge/1_arithmetic.rb index 049ece773..30c55e232 100644 --- a/session1/3-challenge/1_arithmetic.rb +++ b/session1/3-challenge/1_arithmetic.rb @@ -9,5 +9,9 @@ # arithmetic1(-6) # => -50 def arithmetic1(n) - +<<<<<<< HEAD + n-20*5 +======= + n * 5 - 20 +>>>>>>> 83101f98e8bd2cec234ee9dbfbaf32fb6469153c end diff --git a/session1/3-challenge/2_arithmetic.rb b/session1/3-challenge/2_arithmetic.rb index 21445b427..eff1269a0 100644 --- a/session1/3-challenge/2_arithmetic.rb +++ b/session1/3-challenge/2_arithmetic.rb @@ -9,4 +9,5 @@ # arithmetic2(-6, -7) # => -3.5 def arithmetic2(a, b) + a > b ? b / 2.0 : a / 2.0 end diff --git a/session1/3-challenge/3_simple_logic.rb b/session1/3-challenge/3_simple_logic.rb index 29f8c5037..67e5196af 100644 --- a/session1/3-challenge/3_simple_logic.rb +++ b/session1/3-challenge/3_simple_logic.rb @@ -8,4 +8,6 @@ # ten_twenty(6) # => 10 def ten_twenty(n) +n.odd? ? 20 : 10 + end diff --git a/session1/3-challenge/5_string.rb b/session1/3-challenge/5_string.rb index ed15e87ad..43c53104e 100644 --- a/session1/3-challenge/5_string.rb +++ b/session1/3-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) -end \ No newline at end of file + string.gsub(/[sS]ad/, {'sad' =>'happy', 'Sad'=>'Happy'}) +end diff --git a/session1/3-challenge/6_string.rb b/session1/3-challenge/6_string.rb index 4dc3a4e83..ab48b0ae7 100644 --- a/session1/3-challenge/6_string.rb +++ b/session1/3-challenge/6_string.rb @@ -6,5 +6,13 @@ # def odds_and_evens(string, return_odds) +new_string = "" +string.length.times do |x| +next if return_odds == true && x.even? +next if return_odds == false && x.odd? -end \ No newline at end of file +new_string << string[x] +end + +new_string +end diff --git a/session2/3-challenge/12_classes.rb b/session2/3-challenge/12_classes.rb index 7bc1f194a..78a2c6bdc 100644 --- a/session2/3-challenge/12_classes.rb +++ b/session2/3-challenge/12_classes.rb @@ -23,8 +23,34 @@ # f.to_f # => 0.5 class Fraction + + attr_accessor :numerator, :denominator + + def initialize numerator, denominator + @numerator = numerator + @denominator = denominator + + end + def gcd(a,b) return a if b == 0 gcd(b, a%b) end + + def to_s + "#{@numerator}/#{@denominator}" + end + + def to_f + @numerator / denominator.to_f + end + + def lowest + div = gcd(@denominator, @numerator) + Fraction.new(@numerator/div, @denominator/div) + end end + +f = Fraction.new(20,60) +puts f.numerator +puts f.lowest.to_s \ No newline at end of file diff --git a/session2/3-challenge/1_input_output.rb b/session2/3-challenge/1_input_output.rb index 99f723341..5538eac06 100644 --- a/session2/3-challenge/1_input_output.rb +++ b/session2/3-challenge/1_input_output.rb @@ -8,6 +8,11 @@ # "11\n7\n18\n" to standard output. def sum_difference_product - # your code goes here - -end \ No newline at end of file + # num = gets.chomp + numbers = gets.chomp.split(" ") + puts numbers[0].to_i + numbers[1].to_i + puts numbers[0].to_i - numbers[1].to_i + puts numbers[0].to_i * numbers[1].to_i + + +end diff --git a/session2/3-challenge/2_input_output_control.rb b/session2/3-challenge/2_input_output_control.rb index 31ce263ee..8679ceb66 100644 --- a/session2/3-challenge/2_input_output_control.rb +++ b/session2/3-challenge/2_input_output_control.rb @@ -19,8 +19,17 @@ # and when you think it is correct, you can test it with $ rake 2:2 def hi_hi_goodbye - # your code here - + puts "Enter a number" + while true + number = gets.chomp + break if number == "bye" + number.to_i.times do + print "hi " + end + puts "" + # number.to_i.times print "hi " + end +puts "goodbye" end @@ -29,4 +38,4 @@ def hi_hi_goodbye # 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" # 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/3-challenge/3_array.rb b/session2/3-challenge/3_array.rb index 4dea8459c..d36da3195 100644 --- a/session2/3-challenge/3_array.rb +++ b/session2/3-challenge/3_array.rb @@ -7,6 +7,18 @@ class String def every_other_char + str = "" + split_string = self.split("") + count = 1 + split_string.each do |letter| + if count % 2 == 1 + str += letter + end + count += 1 + end + + str end - end + +puts "sdfsdfsdf".every_other_char diff --git a/session2/3-challenge/4_array.rb b/session2/3-challenge/4_array.rb index 85602ab21..3ffeb0949 100644 --- a/session2/3-challenge/4_array.rb +++ b/session2/3-challenge/4_array.rb @@ -8,5 +8,14 @@ # This time you will have to define the method, it's called: get_squares +def get_squares(num_arr) + return_arr = [] + num_arr.each do |number| + if num_arr.include?(number * number) + return_arr << number + end + end +return_arr.sort +end - +# puts get_squares([9,3,81]) diff --git a/session2/3-challenge/5_array.rb b/session2/3-challenge/5_array.rb index 3f1125b04..47f27f661 100644 --- a/session2/3-challenge/5_array.rb +++ b/session2/3-challenge/5_array.rb @@ -14,3 +14,10 @@ # # mod_three [0,1,2,3,4,5,6,7] # => [1, 2, 1, 2, 1] +def mod_three(num_arr) + new = [] + num_arr.map {|number| new << number % 3 if number % 3 > 0} +new +end + +puts mod_three([3,4,5,6,7,8,9]) diff --git a/session2/3-challenge/6_array.rb b/session2/3-challenge/6_array.rb index 90ae4534c..773539029 100644 --- a/session2/3-challenge/6_array.rb +++ b/session2/3-challenge/6_array.rb @@ -15,3 +15,13 @@ # prime_chars? ['a', 'bcd'] # => false # prime_chars? ['a', 'b', 'cd'] # => false +def prime_chars?(num_arr) +require 'prime' +total = 0 + num_arr.each do |x| + total += x.length + end +Prime.prime?(total) +end + +# puts prime_chars?(["s"]) diff --git a/session2/3-challenge/9_input_output_logic_string.rb b/session2/3-challenge/9_input_output_logic_string.rb index 626119b35..f2baf2860 100644 --- a/session2/3-challenge/9_input_output_logic_string.rb +++ b/session2/3-challenge/9_input_output_logic_string.rb @@ -24,7 +24,15 @@ # USER: BYE def deaf_grandma - +while true + input = gets.chomp + break if input == "BYE" + if input == input.upcase && input != "" + puts "NO, NOT SINCE 1938!" + else + puts "HUH?! SPEAK UP, SONNY!" + end +end end