From 2a87ab7be590c3617a23ecfef21c17c575dbd9b1 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Mon, 19 Mar 2018 23:45:57 +0000 Subject: [PATCH 01/13] Completed the Max exercise --- exercises/max.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/max.rb b/exercises/max.rb index 65f6158..bca00f4 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 From 2abdd47cfcef459d7c3a1f4f21ce534722d9361c Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Mon, 19 Mar 2018 23:49:48 +0000 Subject: [PATCH 02/13] Completed the Min exercise --- exercises/min.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 From 1b16b7c9127354daa6be01085d5d3d2437db3c04 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Wed, 21 Mar 2018 10:47:45 +0000 Subject: [PATCH 03/13] Longest String and edited Max --- exercises/longest_string.rb | 17 ++++++++++++++++- exercises/max.rb | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/exercises/longest_string.rb b/exercises/longest_string.rb index ef06244..3ae3272 100644 --- a/exercises/longest_string.rb +++ b/exercises/longest_string.rb @@ -12,10 +12,25 @@ # 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"]) == "corner" end diff --git a/exercises/max.rb b/exercises/max.rb index bca00f4..58b547f 100644 --- a/exercises/max.rb +++ b/exercises/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. # From bea7dc4ce10898ae6bebf69b22ed9b6820e44813 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Tue, 3 Apr 2018 16:18:07 +0100 Subject: [PATCH 04/13] Changes so far --- exercises/count_in_list.rb | 10 ++++++++++ exercises/longest_string.rb | 3 ++- exercises/shortest_string.rb | 14 +++++++++++++- exercises/testrunfile.rb | 20 ++++++++++++++++++++ exercises/word_count.rb | 3 +++ 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 exercises/testrunfile.rb diff --git a/exercises/count_in_list.rb b/exercises/count_in_list.rb index 98a5a16..2321891 100644 --- a/exercises/count_in_list.rb +++ b/exercises/count_in_list.rb @@ -19,9 +19,19 @@ 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 + list = list.to_s + words = list.split + + num = words.count(item_to_count) + + 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 running total", "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/longest_string.rb b/exercises/longest_string.rb index 3ae3272..0cd910f 100644 --- a/exercises/longest_string.rb +++ b/exercises/longest_string.rb @@ -12,6 +12,7 @@ # str.length == 4 def longest_string(list) + 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 @@ -32,5 +33,5 @@ def longest_string(list) 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"]) == "corner" + p longest_string(["123456789", "might", "have", "23564.25689", "a", "corner", "case"]) == "23564.25689" end 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/testrunfile.rb b/exercises/testrunfile.rb new file mode 100644 index 0000000..7e85f08 --- /dev/null +++ b/exercises/testrunfile.rb @@ -0,0 +1,20 @@ +def count_in_list(list, item_to_count) + # You'll need three things: + # 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 + list = list.to_s + puts list + + words = list.split(" ") + + puts words + + num = words.count(item_to_count) + + + puts num + +end + +count_in_list([1,1,1], "to") 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 From 816e6969e943eeceae03fed9596083a255b079b6 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Thu, 5 Apr 2018 14:16:30 +0100 Subject: [PATCH 05/13] Completed so far --- exercises/count_in_list.rb | 14 +++++++++----- exercises/count_max.rb | 11 +++++++++++ exercises/mean.rb | 8 ++++++++ exercises/print_square.rb | 4 ++-- exercises/sum.rb | 6 ++++++ exercises/testrunfile.rb | 22 +++++++++++++++------- 6 files changed, 51 insertions(+), 14 deletions(-) diff --git a/exercises/count_in_list.rb b/exercises/count_in_list.rb index 2321891..96559ff 100644 --- a/exercises/count_in_list.rb +++ b/exercises/count_in_list.rb @@ -19,18 +19,22 @@ 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 - list = list.to_s - words = list.split - - num = words.count(item_to_count) + 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 running total", "to") == 2 + 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 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/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/print_square.rb b/exercises/print_square.rb index aa0b853..ad5c4e9 100644 --- a/exercises/print_square.rb +++ b/exercises/print_square.rb @@ -19,14 +19,14 @@ def print_line(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(2) # Fill in the blank, here. end 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 index 7e85f08..7ed10cb 100644 --- a/exercises/testrunfile.rb +++ b/exercises/testrunfile.rb @@ -3,18 +3,26 @@ 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 - list = list.to_s - puts list + mycount = [] - words = list.split(" ") + list = list.map(&:to_s) - puts words + print list - num = words.count(item_to_count) + list.each do |str| + if str == item_to_count + mycount.push(str) + puts + print mycount + + end + puts + end + + puts mycount.count - puts num end -count_in_list([1,1,1], "to") +count_in_list([1,1,1, "to", "to kill", 2562], "1") From 3c9d5d9efcedc19c5bce6959e99b743b2f1ad94d Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Fri, 6 Apr 2018 20:20:54 +0100 Subject: [PATCH 06/13] Printed the n*n Squares --- exercises/print_square.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/exercises/print_square.rb b/exercises/print_square.rb index ad5c4e9..9bc4484 100644 --- a/exercises/print_square.rb +++ b/exercises/print_square.rb @@ -13,7 +13,7 @@ # 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 "*" @@ -25,9 +25,12 @@ def print_line(count) # 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(2) # 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 From 6d8366fe8628d3c61d3afeeb358293343d91b444 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Fri, 6 Apr 2018 20:21:56 +0100 Subject: [PATCH 07/13] Printed the asterisks to show a triangle grid --- exercises/print_triangle.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 From 09f0c04dd40e2f9f0ca02328408578d5f899b263 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Fri, 6 Apr 2018 20:22:47 +0100 Subject: [PATCH 08/13] Printed the asterisk on an inverted pyramid grid-like format --- exercises/print_pyramid.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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 From bd155e1af875a71a13bb722250a719f46e94cfc7 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Sat, 7 Apr 2018 21:43:07 +0100 Subject: [PATCH 09/13] Bottle completed --- exercises/bottles.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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: From cc553c7ee90c55cb300be7690e07d99f08485d17 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Sat, 7 Apr 2018 21:43:33 +0100 Subject: [PATCH 10/13] Find_even completed --- exercises/find_even.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/exercises/find_even.rb b/exercises/find_even.rb index 08a0c7d..3d71e8e 100644 --- a/exercises/find_even.rb +++ b/exercises/find_even.rb @@ -10,6 +10,15 @@ # find_even([10,10,10,11,11,11]) == [10,10,10] def find_even(array) + array1 = [] + array.each do |ar| + if ar % 2 == 0 + array1.push(ar) + else + return array1 + end + + end end # Note #1 @@ -43,14 +52,15 @@ 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 p find_even([2, 4, 6, 8, 10]) == [2, 4, 6, 8, 10] # If the input array contains all ODD numbers, # find_even should return the empty array - + p find_even([3, 5, 7, 9, 11]) == [] # If an even number appears N times in the input array, # it should appear N times in the the array that find_even returns + p find_even([2, 4, 4, 6, 8, 10]) == [2, 4, 4, 6, 8, 10] end From b96cb8c3425593d9749e0c53c490e791be314f8e Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Sat, 7 Apr 2018 21:45:09 +0100 Subject: [PATCH 11/13] hot_or_cold completed --- exercises/hot_or_cold.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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. From 1ade5c4130f9e4cc40935c381d955e9a395127a3 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Fri, 13 Apr 2018 12:59:36 +0100 Subject: [PATCH 12/13] Find_even completed finally. Took me days to figure this out. Pheeeww!!! --- exercises/find_even.rb | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/exercises/find_even.rb b/exercises/find_even.rb index 3d71e8e..9368e86 100644 --- a/exercises/find_even.rb +++ b/exercises/find_even.rb @@ -11,14 +11,8 @@ def find_even(array) array1 = [] - array.each do |ar| - if ar % 2 == 0 - array1.push(ar) - else - return array1 - end - - end + array1 = array.find_all { |ar| ar % 2 == 0} + array = array1 end # Note #1 @@ -55,12 +49,25 @@ def find_even(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 From 3051c64bbd8dc0336c16d85973e54a2ec8832c28 Mon Sep 17 00:00:00 2001 From: gem install sinatra haml data_mapper rspec sqlite dm-sqlite-adapter Date: Thu, 19 Apr 2018 17:07:13 +0100 Subject: [PATCH 13/13] Commas, Mode --- exercises/_SUGGESTED_ORDER.md | 34 +++++++++++----------- exercises/commas.rb | 12 ++++++++ exercises/mode.rb | 8 +++++ exercises/print_horizontal_pyramid.rb | 19 +++++++++++- exercises/testrunfile.rb | 42 ++++++++++++--------------- 5 files changed, 73 insertions(+), 42 deletions(-) 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/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/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/testrunfile.rb b/exercises/testrunfile.rb index 7ed10cb..1792fa5 100644 --- a/exercises/testrunfile.rb +++ b/exercises/testrunfile.rb @@ -1,28 +1,22 @@ -def count_in_list(list, item_to_count) - # You'll need three things: - # 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 = [] - - list = list.map(&:to_s) - - print list - - list.each do |str| - if str == item_to_count - mycount.push(str) - puts - print mycount - +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 - puts - end - - puts mycount.count - + results.sort[0] +end -end +puts complex_ints 12 +puts complex_ints 456 +puts complex_ints 4567 +puts complex_ints 12345 -count_in_list([1,1,1, "to", "to kill", 2562], "1") +# Bonus 1 +start = Time.now +res = complex_ints 1234567891011 +finish = Time.now +puts "The result is #{res}. It took #{finish-start} seconds to calculate."