From 772eea8da54980322fddd9e1a723f7d132553c75 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 13:26:59 +0100 Subject: [PATCH 01/37] update versions --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 10c7880c..47a71f57 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source "https://rubygems.org" -ruby '2.5.0' +ruby '2.6.3' gem "rspec" gem "pry" diff --git a/Gemfile.lock b/Gemfile.lock index b1d90fb8..58c9cc89 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,7 +29,7 @@ DEPENDENCIES rspec RUBY VERSION - ruby 2.5.0p0 + ruby 2.6.3p62 BUNDLED WITH - 1.16.1 + 1.17.3 From ce9d4881f045068d5d3641499df1b08cb7fa031f Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 13:27:21 +0100 Subject: [PATCH 02/37] round_up_number --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index 50e7fe21..8b1d262e 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -1,6 +1,7 @@ # round up a float up and convert it to an Integer, # so 3.214 becomes 4 def round_up_number(float) + float.ceil end # round down a float up and convert it to an Integer, From 383238c35ca1395015e6eb5f53f6e14a596d99cf Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 13:27:51 +0100 Subject: [PATCH 03/37] round_down_numbers --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index 8b1d262e..ae0d83dc 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -7,6 +7,7 @@ def round_up_number(float) # round down a float up and convert it to an Integer, # so 9.52 becomes 9 def round_down_number(float) + float.floor end From da8fa7281f1d83a05757dfa3c44297efb9421c9c Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 13:28:54 +0100 Subject: [PATCH 04/37] make_number_negative --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index ae0d83dc..8ead415c 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -14,6 +14,7 @@ def round_down_number(float) # turn a positive integer into a negative integer. A negative integer # stays negative def make_numbers_negative(number) + -number.abs end # swap the keys and values in a hash. e.g. From 9a9bc003bc040d2b0f4e9e1fb229ea3d977f178e Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 14:05:41 +0100 Subject: [PATCH 05/37] swap_keys_and_values_in_a_hash --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index 8ead415c..10f53190 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -21,6 +21,7 @@ def make_numbers_negative(number) # {'a' => 'b', 'c' => 'd'} becomes # {'b' => 'a', 'd' => 'c'} def swap_keys_and_values_in_a_hash(hash) + hash.invert end # in a hash where the keys and values are all numbers From dfd1588f1ada6bbe700b71d04c09f2e1d19acfee Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 14:11:19 +0100 Subject: [PATCH 06/37] add_together_keys_and_values --- lib/questions.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 10f53190..fe1f7c9e 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -28,6 +28,12 @@ def swap_keys_and_values_in_a_hash(hash) # add all the keys and all the values together, e.g. # {1 => 1, 2 => 2} becomes 6 def add_together_keys_and_values(hash) + total = 0 + hash.each do |key, value| + total += key + value + end + + total end # turn an array (with an even number of elements) into a hash, by From b3a80154a0d2cce4701fef53f722406d403855f9 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 14:26:21 +0100 Subject: [PATCH 07/37] convert_array_to_a_hash --- lib/questions.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index fe1f7c9e..7d736b43 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -40,6 +40,16 @@ def add_together_keys_and_values(hash) # pairing up elements. e.g. ['a', 'b', 'c', 'd'] becomes # {'a' => 'b', 'c' => 'd'} def convert_array_to_a_hash(array) + hash = Hash.new + + for i in (0...array.length).step 2 do + key = array[i] + value = array[i + 1] + + hash[key] = value + end + + hash end From fccfb06dc26328f013c4a9033326b5d700bbe770 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 14:30:46 +0100 Subject: [PATCH 08/37] remove_capital_letters_from_string --- lib/questions.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 7d736b43..46a01bd0 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -56,6 +56,9 @@ def convert_array_to_a_hash(array) # take out all the capital letters from a string # so 'Hello JohnDoe' becomes 'ello ohnoe' def remove_capital_letters_from_string(string) + upper_case_filter = /[[:upper:]]/ + + string.gsub(upper_case_filter, '') end From 8eb8f066408f58ae29ac4df2314873b70e37bf68 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 14:38:37 +0100 Subject: [PATCH 09/37] get_first_half_of_string --- lib/questions.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 46a01bd0..5c49c334 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -66,6 +66,9 @@ def remove_capital_letters_from_string(string) # 'banana' becomes 'ban'. If the string is an odd number of letters # round up - so 'apple' becomes 'app' def get_first_half_of_string(string) + halfway = round_up_number(string.length.to_f / 2) + + string[0, halfway] end # convert a symbol into a string From 78ff89d8d0f43599a8eaebb88798ecbd017ac993 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 14:40:08 +0100 Subject: [PATCH 10/37] turn_symbol_into_string --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index 5c49c334..b9241312 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -73,6 +73,7 @@ def get_first_half_of_string(string) # convert a symbol into a string def turn_symbol_into_string(symbol) + symbol.to_s end From 80060330de3e0e109d00c12a7293a8b802cf70b4 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 14:49:58 +0100 Subject: [PATCH 11/37] get_domain_name_from_email_address --- lib/questions.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index b9241312..41d86ec5 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -80,6 +80,9 @@ def turn_symbol_into_string(symbol) # get the domain name *without* the .com part, from an email address # so onboarding@makersacademy.com becomes makersacademy def get_domain_name_from_email_address(email) + full_domain = email.partition("@").last + + full_domain.partition(".").first end # capitalize the first letter in each word of a string, From a46c1bb511e21043368528b9286531fab2c68e91 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 15:12:31 +0100 Subject: [PATCH 12/37] titleize_a_string --- lib/questions.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 41d86ec5..65c7f7f8 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -91,6 +91,21 @@ def get_domain_name_from_email_address(email) # 'the lion the witch and the wardrobe' becomes # 'The Lion the Witch and the Wardrobe' def titleize_a_string(string) + word_separator = " " + words = string.split(word_separator) + + exceptions = %w(a and the) + + words.each_with_index do |word, index| + is_first_word = index == 0 + is_excepted = exceptions.include? word + + if (!is_excepted) or (is_excepted && is_first_word) + words[index] = word.capitalize + end + end + + words.join(word_separator) end # return true if a string contains any special characters From 8e5c1a642386d718c182b9a8ee5c5d33d08b9ab8 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 15:28:04 +0100 Subject: [PATCH 13/37] check_a_string_for_special_characters --- lib/questions.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 65c7f7f8..0286c5c8 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -112,6 +112,15 @@ def titleize_a_string(string) # where 'special character' means anything apart from the letters # a-z (uppercase and lower) or numbers def check_a_string_for_special_characters(string) + non_alphanumeric_filter = /\W/ + + string.each_char do |char| + if char =~ non_alphanumeric_filter + return true + end + end + + false end # keep only the elements that start with an a From eb312c532c44dbbe039f9f538fca07bdee3b1b73 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 15:36:11 +0100 Subject: [PATCH 14/37] select_elements_starting_with_a --- lib/questions.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 0286c5c8..2624562c 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -125,6 +125,15 @@ def check_a_string_for_special_characters(string) # keep only the elements that start with an a def select_elements_starting_with_a(array) + clean_array = Array.new + + array.each do |el| + if el[0] == "a" + clean_array.push el + end + end + + clean_array end # keep only the elements that start with a vowel From 34bceeb381396f7da754cf228c77a3c92dd47b1f Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:03:54 +0100 Subject: [PATCH 15/37] select_elements_starting_with_vowel --- lib/questions.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 2624562c..b86102c9 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -138,6 +138,16 @@ def select_elements_starting_with_a(array) # keep only the elements that start with a vowel def select_elements_starting_with_vowel(array) + clean_array = Array.new + vowels = %w(a e i o u) + + array.each do |el| + if vowels.include? el[0].downcase + clean_array.push el + end + end + + clean_array.join(" ") end From 64bef754f5592ec86eef84b95556fcb5045954e3 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:06:00 +0100 Subject: [PATCH 16/37] remove_nils_from_array --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index b86102c9..0671282f 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -153,6 +153,7 @@ def select_elements_starting_with_vowel(array) # remove instances of nil (but NOT false) from an array def remove_nils_from_array(array) + array.reject { |x| x == nil } end # remove instances of nil AND false from an array From c83411c21155f8bde4b5cc76c46daad47c4ef8e2 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:08:45 +0100 Subject: [PATCH 17/37] remove_nils_and_false_from_array --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index 0671282f..9c7742f9 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -158,6 +158,7 @@ def remove_nils_from_array(array) # remove instances of nil AND false from an array def remove_nils_and_false_from_array(array) + array.reject { |x| !x || x == nil } end # don't reverse the array, but reverse every word inside it. e.g. From 6354ce96fc8daf1307f59b76912d4d070a5ecf1e Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:13:49 +0100 Subject: [PATCH 18/37] reverse_every_element_in_array --- lib/questions.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 9c7742f9..e6e7ad21 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -164,6 +164,13 @@ def remove_nils_and_false_from_array(array) # don't reverse the array, but reverse every word inside it. e.g. # ['dog', 'monkey'] becomes ['god', 'yeknom'] def reverse_every_element_in_array(array) + reversed_array = Array.new + + array.each do |el| + reversed_array.push el.chars.reverse.join + end + + reversed_array end # discard the first 3 elements of an array, From 4ef68a09e3fe4acaa58e4e18b6dab621df55265f Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:17:51 +0100 Subject: [PATCH 19/37] all_elements_except_first_3 --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index e6e7ad21..003a6cae 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -176,6 +176,7 @@ def reverse_every_element_in_array(array) # discard the first 3 elements of an array, # e.g. [1, 2, 3, 4, 5, 6] becomes [4, 5, 6] def all_elements_except_first_3(array) + array[3..array.length] end # add an element to the beginning of an array From 61eef3b3124bdf6c58f51632cfe1447d3ba5c910 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:18:42 +0100 Subject: [PATCH 20/37] add_element_to_beginning_of_array --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index 003a6cae..cc5510cd 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -181,6 +181,7 @@ def all_elements_except_first_3(array) # add an element to the beginning of an array def add_element_to_beginning_of_array(array, element) + array.prepend element end # return the shortest word in an array From 6a55276fa232b9c3809085bdaa6c9c99ed0a517f Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:32:29 +0100 Subject: [PATCH 21/37] shortest_word_in_array --- lib/questions.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index cc5510cd..55cd192d 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -186,6 +186,15 @@ def add_element_to_beginning_of_array(array, element) # return the shortest word in an array def shortest_word_in_array(array) + shortest_word_index = 0 + + array.each_with_index do |word, index| + if word.length < array[shortest_word_index].length + shortest_word_index = index + end + end + + array[shortest_word_index] end # return the shortest word in an array From cc682ed672b1f5da2fca11451c28c071090567eb Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:33:23 +0100 Subject: [PATCH 22/37] longest_word_in_array --- lib/questions.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 55cd192d..188277b2 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -199,6 +199,15 @@ def shortest_word_in_array(array) # return the shortest word in an array def longest_word_in_array(array) + longest_word_index = 0 + + array.each_with_index do |word, index| + if word.length > array[longest_word_index].length + longest_word_index = index + end + end + + array[longest_word_index] end # add up all the numbers in an array, so [1, 3, 5, 6] From d62eb9d5504337ffec1199697b46a64bdd10a1d2 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:34:44 +0100 Subject: [PATCH 23/37] better shortest_word_in_array --- lib/questions.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index 188277b2..61af27b5 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -186,15 +186,7 @@ def add_element_to_beginning_of_array(array, element) # return the shortest word in an array def shortest_word_in_array(array) - shortest_word_index = 0 - - array.each_with_index do |word, index| - if word.length < array[shortest_word_index].length - shortest_word_index = index - end - end - - array[shortest_word_index] + array.min_by(&:length) end # return the shortest word in an array @@ -213,6 +205,7 @@ def longest_word_in_array(array) # add up all the numbers in an array, so [1, 3, 5, 6] # returns 15 def total_of_array(array) + end # get the average from an array, rounded to the nearest integer From 380cf9ef9cccc5bedd1090e387c9aa282a2dafdd Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:35:12 +0100 Subject: [PATCH 24/37] better longest_word_in_array --- lib/questions.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index 61af27b5..80674820 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -191,15 +191,7 @@ def shortest_word_in_array(array) # return the shortest word in an array def longest_word_in_array(array) - longest_word_index = 0 - - array.each_with_index do |word, index| - if word.length > array[longest_word_index].length - longest_word_index = index - end - end - - array[longest_word_index] + array.max_by(&:length) end # add up all the numbers in an array, so [1, 3, 5, 6] From 7ab85c3d42b785102da19877577aa3c5a2473617 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:38:06 +0100 Subject: [PATCH 25/37] total_of_array --- lib/questions.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 80674820..c075d4d3 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -197,7 +197,13 @@ def longest_word_in_array(array) # add up all the numbers in an array, so [1, 3, 5, 6] # returns 15 def total_of_array(array) + total = 0 + + array.each do |num| + total += num + end + total end # get the average from an array, rounded to the nearest integer From dc50e0f016232b25ea9a77a87cfcee99f79b3112 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:42:17 +0100 Subject: [PATCH 26/37] average_of_array --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index c075d4d3..683a94d1 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -209,6 +209,7 @@ def total_of_array(array) # get the average from an array, rounded to the nearest integer # so [10, 15, 25] should return 17 def average_of_array(array) + (total_of_array(array).to_f / array.length).round end # get all the elements in an array, up until the first element From 6d2577b3bc855fd9dd4b366fd813a37afe1639aa Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:46:29 +0100 Subject: [PATCH 27/37] get_elements_until_greater_than_five --- lib/questions.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 683a94d1..5ce8afee 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -217,6 +217,17 @@ def average_of_array(array) # [1, 3, 5, 4, 1, 2, 6, 2, 1, 3, 7] # becomes [1, 3, 5, 4, 1, 2] def get_elements_until_greater_than_five(array) + short_numbers = Array.new + + array.each do |num| + if num > 5 + break + else + short_numbers.push num + end + end + + short_numbers end # get all the letters used in an array of words and return From 8399a91b6788862872dce4d93654787b82cb91ce Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 16:50:27 +0100 Subject: [PATCH 28/37] get_all_letters_in_array_of_words --- lib/questions.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 5ce8afee..34da9225 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -235,6 +235,17 @@ def get_elements_until_greater_than_five(array) # . e.g. the array ['cat', 'dog', 'fish'] becomes # ['a', 'c', 'd', 'f', 'g', 'h', 'i', 'o', 's', 't'] def get_all_letters_in_array_of_words(array) + characters = Array.new + + array.each do |word| + puts word + word.each_char do |char| + puts char + characters.push char + end + end + + characters.sort { |a, b| a <=> b } end # BONUS SECTION From 507df3e44ef8b61a497c3cf6d2226306328ce5e9 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 17:20:16 +0100 Subject: [PATCH 29/37] format_date_nicely --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index 34da9225..e69696ab 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -253,6 +253,7 @@ def get_all_letters_in_array_of_words(array) # take a date and format it like dd/mm/yyyy, so Halloween 2013 # becomes 31/10/2013 def format_date_nicely(date) + date.strftime("%d/%m/%Y") end # get the upper limit of a range. e.g. for the range 1..20, you From 47a720e12f90dadfe7ef3e639a3a56f979a084b4 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 17:21:16 +0100 Subject: [PATCH 30/37] get_upper_limit_of --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index e69696ab..648a8262 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -259,6 +259,7 @@ def format_date_nicely(date) # get the upper limit of a range. e.g. for the range 1..20, you # should return 20 def get_upper_limit_of(range) + range.max end # should return true for a 3 dot range like 1...20, false for a From 462fca42c53578ee1a004f51b8ad975d6ebca186 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 17:32:59 +0100 Subject: [PATCH 31/37] is_a_3_dot_range? --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index 648a8262..5bb780df 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -265,6 +265,7 @@ def get_upper_limit_of(range) # should return true for a 3 dot range like 1...20, false for a # normal 2 dot range def is_a_3_dot_range?(range) + range.max < range.last end # get the square root of a number From ac4745ddab39c4056258f98dd03fcf3c289fd0ac Mon Sep 17 00:00:00 2001 From: jtsalva Date: Thu, 19 Sep 2019 17:33:48 +0100 Subject: [PATCH 32/37] square_root_of --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index 5bb780df..ccc0fd75 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -270,6 +270,7 @@ def is_a_3_dot_range?(range) # get the square root of a number def square_root_of(number) + Math.sqrt number end # --- tougher ones --- From 0fcd82a2c2fce54acaf5c7aa59f198a0ced887d7 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Fri, 20 Sep 2019 07:59:37 +0100 Subject: [PATCH 33/37] call_method_from_string --- lib/questions.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/questions.rb b/lib/questions.rb index ccc0fd75..9dda6027 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -279,6 +279,7 @@ def square_root_of(number) # called call_method_from_string('foobar') # the method foobar should be invoked def call_method_from_string(str_method) + eval(str_method) end # return true if the date is a uk bank holiday for 2014 From 8c8c689fd7fc7460ca8a5984c554f6e6fd1eaa97 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Fri, 20 Sep 2019 08:32:00 +0100 Subject: [PATCH 34/37] is_a_2018_bank_holiday? --- lib/questions.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/questions.rb b/lib/questions.rb index 9dda6027..6288267c 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -282,10 +282,21 @@ def call_method_from_string(str_method) eval(str_method) end -# return true if the date is a uk bank holiday for 2014 +# return true if the date is a uk bank holiday for 2018 # the list of bank holidays is here: # https://www.gov.uk/bank-holidays def is_a_2018_bank_holiday?(date) + bank_holidays = [ + Time.new(2018, 1, 1), + Time.new(2018, 3, 30), + Time.new(2018, 4, 2), + Time.new(2018, 5, 7), + Time.new(2018, 5, 28), + Time.new(2018, 8, 27), + Time.new(2018, 12, 25), + Time.new(2018, 12, 26)] + + bank_holidays.include? date end # given your birthday this year, this method tells you From a5dfb6a38d07bda862b4cc3c61ce925e173899fe Mon Sep 17 00:00:00 2001 From: jtsalva Date: Fri, 20 Sep 2019 15:41:42 +0100 Subject: [PATCH 35/37] word_count_a_file --- lib/questions.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index 6288267c..c1a4598d 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -308,6 +308,13 @@ def your_birthday_is_on_a_friday_in_the_year(birthday) # count the number of words in a file def word_count_a_file(file_path) + total_word_count = 0 + + File.open(file_path).each_line do |line| + total_word_count += line.split(" ").size + end + + total_word_count end From ef552a25a867cc096e7d163c2d394c0e735e6be5 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Fri, 20 Sep 2019 16:31:08 +0100 Subject: [PATCH 36/37] count_words_of_each_length_in_a_file --- lib/questions.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/questions.rb b/lib/questions.rb index c1a4598d..d9074dbb 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -324,4 +324,22 @@ def word_count_a_file(file_path) # and 1 that is 4 letters long. Return it as a hash in the format # word_length => count, e.g. {2 => 1, 3 => 5, 4 => 1} def count_words_of_each_length_in_a_file(file_path) + word_lengths = Hash.new + + # remove all non alphabetic and non spaces + f = File.read(file_path).gsub(/[^0-9a-z ]/i, '') + + f.each_line do |line| + + line.split(" ").each do |word| + if word_lengths.key? word.length + word_lengths[word.length] += 1 + else + word_lengths[word.length] = 1 + end + end + + end + + word_lengths end From 396d52c59d851c75c607b6e6f40369b0e4b4fb56 Mon Sep 17 00:00:00 2001 From: jtsalva Date: Fri, 20 Sep 2019 19:44:14 +0100 Subject: [PATCH 37/37] next birthday which is on friday is 2021 not 2020 --- lib/questions.rb | 17 +++++++++++++++++ spec/questions_spec.rb | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/questions.rb b/lib/questions.rb index d9074dbb..875deb6c 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -304,6 +304,23 @@ def is_a_2018_bank_holiday?(date) # e.g. january 1st, will next be a friday in 2016 # return the day as a capitalized string like 'Friday' def your_birthday_is_on_a_friday_in_the_year(birthday) + year = birthday.year + 1 + + year_found = nil + + while year_found == nil do + + future_birthday = Time.new(year, birthday.month, birthday.day) + + if future_birthday.strftime("%A") == "Friday" + year_found = future_birthday.year + else + year += 1 + end + + end + + year_found end # count the number of words in a file diff --git a/spec/questions_spec.rb b/spec/questions_spec.rb index 4f5fd96b..704bafae 100644 --- a/spec/questions_spec.rb +++ b/spec/questions_spec.rb @@ -192,7 +192,7 @@ it 'your_birthday_is_on_a_friday_in_the_year' do n = your_birthday_is_on_a_friday_in_the_year(Time.new(2018, 1, 1)) - expect(n).to eq 2020 + expect(n).to eq 2021 end it 'word_count_a_file' do