diff --git a/exercises/_SUGGESTED_ORDER.md b/exercises/_SUGGESTED_ORDER.md index 8dc09c2..e8f1542 100644 --- a/exercises/_SUGGESTED_ORDER.md +++ b/exercises/_SUGGESTED_ORDER.md @@ -1,23 +1,23 @@ This is the **suggested order** for completing the exercises: -1. [max.rb](max.rb) -1. [min.rb](min.rb) -1. [longest_string.rb](longest_string.rb) -1. [shortest_string.rb](shortest_string.rb) -1. [word_count.rb](word_count.rb) -1. [count_in_list.rb](count_in_list.rb) -1. [count_max.rb](count_max.rb) -1. [sum.rb](sum.rb) -1. [mean.rb](mean.rb) -1. [print_square.rb](print_square.rb) -1. [print_triangle.rb](print_triangle.rb) -1. [print_pyramid.rb](print_pyramid.rb) +1. [max.rb](max.rb) => DONE +1. [min.rb](min.rb) => DONE +1. [longest_string.rb](longest_string.rb) => DONE +1. [shortest_string.rb](shortest_string.rb) => DONE +1. [word_count.rb](word_count.rb) => DONE +1. [count_in_list.rb](count_in_list.rb) => DONE +1. [count_max.rb](count_max.rb) => DONE +1. [sum.rb](sum.rb) => DONE +1. [mean.rb](mean.rb) => DONE +1. [print_square.rb](print_square.rb) =>DONE +1. [print_triangle.rb](print_triangle.rb) => DONE +1. [print_pyramid.rb](print_pyramid.rb) => DONE 1. [print_horizontal_pyramid.rb](print_horizontal_pyramid.rb) -1. [hot_or_cold.rb](hot_or_cold.rb) -1. [bottles.rb](bottles.rb) -1. [find_even.rb](find_even.rb) -1. [mode.rb](mode.rb) -1. [commas.rb](commas.rb) +1. [hot_or_cold.rb](hot_or_cold.rb) => DONE +1. [bottles.rb](bottles.rb) => DONE +1. [find_even.rb](find_even.rb) => DONE +1. [mode.rb](mode.rb) => DONE +1. [commas.rb](commas.rb) => DONE 1. [factorial.rb](factorial.rb) 1. [fibonacci.rb](fibonacci.rb) 1. [find_links.rb](find_links.rb) diff --git a/exercises/bottles.rb b/exercises/bottles.rb index fb44001..9322d35 100644 --- a/exercises/bottles.rb +++ b/exercises/bottles.rb @@ -27,11 +27,21 @@ # etc. def bottles(start_number) + puts "Counting down..." + start_number.downto(1) do |i| + if i > 1 + print "#{i} bottles of beer on the wall, #{i} bottles of beer. Take one down, pass it around, " + puts "#{i-1} bottles of beer on the wall!" + else + print "#{i} bottles of beer on the wall, #{i} bottles of beer. Take one down, pass it around, no more bottles of beer on the wall!" + end + end +#I could not implement the method at the moment so I just coded it logically straight end if __FILE__ == $PROGRAM_NAME # What *should* this print? - bottles(5) + bottles(50) end # Hint #1: diff --git a/exercises/commas.rb b/exercises/commas.rb index 9bd1ccb..b9a77d3 100644 --- a/exercises/commas.rb +++ b/exercises/commas.rb @@ -20,12 +20,24 @@ # insert the commas? Which comma would you insert first? def commas(num) + num = num.to_s.reverse # converts to string and reverses the numbers + + num = num.gsub(/(\d{3})/,"\\1,") # inserts a comma after every 3rd letter + + num.chomp(",").reverse #reverses the number again end +#one line solution + if __FILE__ == $PROGRAM_NAME # What are the common cases? What are the corner cases? # Your sanity checks should look like # p commas(input) == ...expected return value... + p commas(123) == "123" + p commas(1234) == "1,234" + p commas(12345) == "12,345" + p commas(1234567) == "1,234,567" + p commas(1235649) == "1,235,649" end # Hint #1 diff --git a/exercises/count_in_list.rb b/exercises/count_in_list.rb index 98a5a16..96559ff 100644 --- a/exercises/count_in_list.rb +++ b/exercises/count_in_list.rb @@ -19,9 +19,23 @@ def count_in_list(list, item_to_count) # 1. A running total of the number of times you've seen the item # 2. A way to loop/iterate through the list # 3. A way to add to the running total as you see the item + mycount = [] # Sets the initial count of item_to_count to empty array + + list.each do |str| # For each str in list + if str == item_to_count # If str is equal to item_to_count + mycount.push(str) # Push str to the mycount array + end + + end + + return mycount.count # Return the count of str in mycount end if __FILE__ == $PROGRAM_NAME # I'd advise putting some sanity checks here. # How else will you be sure your code does what you think it does? + p count_in_list(["A", "way", "to", "add", "to", "the"], "to") == 2 + p count_in_list([1,1,1], 1) == 3 + p count_in_list([1,2,3], -1) == 0 + p count_in_list([1,2,3], 1) == 1 end diff --git a/exercises/count_max.rb b/exercises/count_max.rb index a62b111..85ce4aa 100644 --- a/exercises/count_max.rb +++ b/exercises/count_max.rb @@ -22,9 +22,20 @@ def count_max(list) # # But remember: inelegant, working code is better than elegant, # unfinished code. + item = max(list) # Sets item to the maximum number in the list + + list.each do |num| # For each num in the list do + maxcount = count_in_list(list, item) # Sets citem equal to the count of item + return maxcount # returns the count + end + end if __FILE__ == $PROGRAM_NAME # I'd advise putting some sanity checks here. # How else will you be sure your code does what you think it does? + p count_max([1, 2, 3]) == 1 + p count_max([50, -100, 50, -200]) == 2 + p count_max([-200, -400, -100, -300]) == 1 + p count_max([10, 1,2,10,10]) == 3 end diff --git a/exercises/find_even.rb b/exercises/find_even.rb index 08a0c7d..9368e86 100644 --- a/exercises/find_even.rb +++ b/exercises/find_even.rb @@ -10,6 +10,9 @@ # find_even([10,10,10,11,11,11]) == [10,10,10] def find_even(array) + array1 = [] + array1 = array.find_all { |ar| ar % 2 == 0} + array = array1 end # Note #1 @@ -43,14 +46,28 @@ def find_even(array) # If the input is the empty array, # find_even should return the empty array - + p find_even([]) == [] # If the input array contains all EVEN numbers, # find_even should return the input array + puts p find_even([2, 4, 6, 8, 10]) == [2, 4, 6, 8, 10] + print find_even([2, 4, 6, 8, 10]) #Just to make double sure onscreen that I got it ;) + puts # If the input array contains all ODD numbers, # find_even should return the empty array - + puts + p find_even([3, 5, 7, 9, 11]) == [] + print find_even([3, 5, 7, 9, 11, 13]) #Just to make double sure onscreen that I got it ;) + puts # If an even number appears N times in the input array, # it should appear N times in the the array that find_even returns + puts + p find_even([2, 4, 4, 6, 8, 10]) == [2, 4, 4, 6, 8, 10] + print find_even([2, 4, 4, 6, 8, 10]) #Just to make double sure onscreen that I got it ;) + puts + + puts + p find_even([1, 2, 3, 4, 5, -6, 7, -8, 9, 10]) == [2, 4, -6, -8, 10] + print find_even([1, 2, 3, 4, 5, -6, 7, -8, 9, 10]) #Just to make double sure onscreen that I got it ;) end diff --git a/exercises/hot_or_cold.rb b/exercises/hot_or_cold.rb index d7ecac5..4e6e49a 100644 --- a/exercises/hot_or_cold.rb +++ b/exercises/hot_or_cold.rb @@ -27,11 +27,11 @@ def hot_or_cold(num_to_guess) guess = get_user_guess() # "guess" is now an integer if guess < num_to_guess # The guess is too cold - ____ + puts "The guess is too cold. Try again" elsif guess > num_to_guess # The guess is too hot - ____ + puts "The guess is too hot. Try again" else # The guess is juuuust right - ____ + puts "The guess is juuuust right. You're the man!!!" # This "return" will make the program return from hot_or_cold, even from # inside the while loop. diff --git a/exercises/longest_string.rb b/exercises/longest_string.rb index ef06244..0cd910f 100644 --- a/exercises/longest_string.rb +++ b/exercises/longest_string.rb @@ -12,10 +12,26 @@ # str.length == 4 def longest_string(list) - # This is your job. :) + + my_long_str = list[0] # assigns a varible to the first array index + list.each do |str| # for each str in list + if str.length > my_long_str.length # if the lengthof current str is greater than my_long_str + my_long_str = str # set str to my_long_str + end + end + return my_long_str end +# A far better and shorter solution (on StackOverflow) is my_long_str = list.max_by(&:length) +# But I will stick with my own homegrown solution + + if __FILE__ == $PROGRAM_NAME # I'd advise putting some sanity checks here. # How else will you be sure your code does what you think it does? + + p longest_string(["We", "might", "have", "missed", "a", "corner", "case"]) == "missed" + p longest_string(["Remember", "that", "conceptually", "x"]) == "conceptually" + + p longest_string(["123456789", "might", "have", "23564.25689", "a", "corner", "case"]) == "23564.25689" end diff --git a/exercises/max.rb b/exercises/max.rb index 65f6158..58b547f 100644 --- a/exercises/max.rb +++ b/exercises/max.rb @@ -27,7 +27,7 @@ def max(list) # "If this file is the file currently being executed, then..." # # __FILE__ is always the name of the current file. -# $0 is always the name of the program beind executed. +# $0 is always the name of the program being executed. # # This means that if we run this file directly, i.e., # ruby max.rb @@ -61,7 +61,7 @@ def max(list) # prints out "true."" For example, # 1. We might have missed a corner case # 2. The code does what it should, but is conceptually confused -# 3. Something else we haven't though of +# 3. Something else we haven't thought of # # Remember: Option #3 is *always* possible. # diff --git a/exercises/mean.rb b/exercises/mean.rb index 515fa2b..b6b945f 100644 --- a/exercises/mean.rb +++ b/exercises/mean.rb @@ -24,9 +24,17 @@ def mean(list) total = sum(list) # This is the "sum" method from our sum.rb file # result = ____ # Given the list's sum, how can we calculate the average? + + result = (total)/list.count # Divides total by list count and set value to result + + return result # return result end if __FILE__ == $PROGRAM_NAME # I'd advise putting some sanity checks here. # How else will you be sure your code does what you think it does? + p mean([64, 10, 21, -69, 33]) == 11 + p mean([44, -59, 98, 82, 13]) == 35 + p mean([84, 55, -62, 3, 99]) == 35 + p mean([12, 68, 21, -86, 80]) == 19 end diff --git a/exercises/min.rb b/exercises/min.rb index 1018519..3e29aca 100644 --- a/exercises/min.rb +++ b/exercises/min.rb @@ -13,14 +13,14 @@ # This is going to be very similar to max, so don't be afraid if # these two methods look almost identical def min(list) - ____ = ____ - ____.each do |____| - if ____ - ____ = ____ + start_small = list[0] #or equivalently, list.first + list.each do |numba| # or equivalently, for each item in list, do something + if numba < start_small # if current numba is less than start_small + start_small = numba # set start_small to the numba end end - return ____ + return start_small # Since we have gone through the list, return start_small end if __FILE__ == $PROGRAM_NAME diff --git a/exercises/mode.rb b/exercises/mode.rb index 0fece00..3472034 100644 --- a/exercises/mode.rb +++ b/exercises/mode.rb @@ -16,6 +16,10 @@ # Break it down as clearly as you can in your own head first. def mode(array) + maxi = Hash.new(0) # Creates an empty hash + array.select{ |nums| maxi[nums] += 1} # For each element in the array, collects the element and frequencies (key/value pair) and appends to the maxi Hash + maxi.max_by { |a, b| b}.first # Checks each key/value pair in the maxi hash and returns the "first" key with the highest value. + end if __FILE__ == $PROGRAM_NAME @@ -49,4 +53,8 @@ def mode(array) p mode(["a", "a", "a", "b"]) == "a" p mode(["b", "a", "a", "a"]) == "a" p mode(["a", "b", "a", "a"]) == "a" + + # This `mode depends on the order of the data` + p mode(["a", "b", "b", "a"]) == "a" + p mode(["b", "a", "b", "a"]) == "b" end diff --git a/exercises/print_horizontal_pyramid.rb b/exercises/print_horizontal_pyramid.rb index e020dd9..48a5ee9 100644 --- a/exercises/print_horizontal_pyramid.rb +++ b/exercises/print_horizontal_pyramid.rb @@ -9,9 +9,26 @@ # * # *** # ***** -# ******* +# # ******* +# def asterisks(count) +# count.upto(2 * 1-i ) do +# print "*" +# +# end +# end + +# def spaces(space) +# space.downto(1) do |s| +# print "*" +# +# end +# end def print_horizontal_pyramid(height) + height.times {|n| + print ' ' * (height - n) + puts '*' * (2 * n + 1) + } end if __FILE__ == $PROGRAM_NAME diff --git a/exercises/print_pyramid.rb b/exercises/print_pyramid.rb index 3c1f86e..29ec6c3 100644 --- a/exercises/print_pyramid.rb +++ b/exercises/print_pyramid.rb @@ -25,9 +25,29 @@ def print_pyramid(height) # Suggestion: you can call print_triangle to print out the first, "upward" # half of the pyramid. You'll have to write code to print out the second, # "downward" half of the pyramid. + + print_triangle(height) + height.downto(1) do |i| # or, equivalently, reverse the range from height to 1 + print_line(i) # I called the print_line method to repeat the same process as in print_triangle.rb + + end + end if __FILE__ == $PROGRAM_NAME # I'd advise putting some sanity checks here. # How else will you be sure your code does what you think it does? + print_pyramid(1) + + print "\n\n\n" # This is here to make the separation between pyramids clearer + + print_pyramid(2) + + print "\n\n\n" # This is here to make the separation between pyramids clearer + + print_pyramid(3) + + print "\n\n\n" # This is here to make the separation between pyramids clearer + + print_pyramid(10) end diff --git a/exercises/print_square.rb b/exercises/print_square.rb index aa0b853..9bc4484 100644 --- a/exercises/print_square.rb +++ b/exercises/print_square.rb @@ -13,21 +13,24 @@ # The print_line method is here to help you. # Conceptually, it prints out a row of "count" *'s. Run it yourself to -# see how it works. Experiment with different inputs. +# see how it works. Experiment with xRails params explained?different inputs. def print_line(count) (1..count).each do |i| # or, equivalently, for i in (1..count) print "*" # This prints a single "*" end - print "\n" # This forces the output to the next line, like hitting "return" + #print "\n" # This forces the output to the next line, like hitting "return" end # We can call methods we've defined ourselves. In this case, we want # to call the print_line method we've defined to help us print out a square. def print_square(dimension) + (1..dimension).each do |i| # or, equivalently, for i in (1..dimension) - print_line(____) # Fill in the blank, here. + print_line(dimension) # Recursion. Kind of. I called the print_line method on the print_square method using the print_square arguement. I don't know what its called. It just works ;). + print "\n" end + end # There are no rumble strips this time. It's up to you to decide whether diff --git a/exercises/print_triangle.rb b/exercises/print_triangle.rb index d0e3af8..a0549fd 100644 --- a/exercises/print_triangle.rb +++ b/exercises/print_triangle.rb @@ -23,7 +23,11 @@ def print_line(count) end def print_triangle(height) - # You have to fill in the details here. + (1..height).each do |i| # or, equivalently, for i in (1..height) + print_line(i) # I called the print_line method on the print_triangle method using each item on print_triangle as arguement. I don't know what its called. It just works ;). I acctually solved this by mistake, trying to solve print_square.rb + + end + end # There are no rumble strips this time. It's up to you to decide whether diff --git a/exercises/shortest_string.rb b/exercises/shortest_string.rb index 4e56203..c47e0fb 100644 --- a/exercises/shortest_string.rb +++ b/exercises/shortest_string.rb @@ -4,10 +4,22 @@ # Prints: Nothing def shortest_string(list) - # This is your job. :) + my_short_str = list[0] # assigns a varible to the first array index + list.each do |str| # for each str in list + if str.length < my_short_str.length # if the lengthof current str is greater than my_short_str + my_short_str = str # set str to my_short_str + end + end + return my_short_str end if __FILE__ == $PROGRAM_NAME # I'd advise putting some sanity checks here. # How else will you be sure your code does what you think it does? + + p shortest_string(["We", "might", "have", "missed", "a", "corner", "case"]) == "a" + p shortest_string(["Remember", "that", "conceptually", "x"]) == "x" + + p shortest_string(["123456789", "might", "have", "23564.25689", "a", "corner", "case"]) == "a" + end diff --git a/exercises/sum.rb b/exercises/sum.rb index 28106f5..8d8129a 100644 --- a/exercises/sum.rb +++ b/exercises/sum.rb @@ -21,6 +21,12 @@ def sum(list) # This is your job. :) + + sum = 0 # Set initial value to zero + + list.each {|x| sum += x } # Iterate through list, get an index, add to sum and set it equal to sum. Repeat until last array + + return sum # Return final value of sum end if __FILE__ == $PROGRAM_NAME diff --git a/exercises/testrunfile.rb b/exercises/testrunfile.rb new file mode 100644 index 0000000..1792fa5 --- /dev/null +++ b/exercises/testrunfile.rb @@ -0,0 +1,22 @@ +def complex_ints n + results = Array.new + puts Math.sqrt(n) + (1..Math.sqrt(n).round).each do |int| + if n % int == 0 + results << n/int + int + end + end + results.sort[0] +end + + +puts complex_ints 12 +puts complex_ints 456 +puts complex_ints 4567 +puts complex_ints 12345 + +# Bonus 1 +start = Time.now +res = complex_ints 1234567891011 +finish = Time.now +puts "The result is #{res}. It took #{finish-start} seconds to calculate." diff --git a/exercises/word_count.rb b/exercises/word_count.rb index f5d6396..223eae5 100644 --- a/exercises/word_count.rb +++ b/exercises/word_count.rb @@ -11,6 +11,9 @@ def word_count(string) # Hint: You'll want to use String#split # See: http://www.ruby-doc.org/core-2.1.2/String.html#method-i-split + + mystring = string.split(' ') #spilts the value of string into an array delimited by spaces and assigns to mystring + mystring = mystring.count #counts the number of array elements and assigns to mystring end if __FILE__ == $PROGRAM_NAME