From f3f563181f1d49235725ce718fea998b8df35d35 Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Fri, 8 Apr 2016 17:39:21 +0200 Subject: [PATCH 01/11] passes all tests except the last three --- Gemfile | 2 +- lib/questions.rb | 61 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index b9b134e..30c86ce 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.2.1' +ruby '2.3.0' group :development do gem 'rspec' diff --git a/lib/questions.rb b/lib/questions.rb index 823db48..253a293 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -1,61 +1,74 @@ # keep only the elements that start with an a def select_elements_starting_with_a(array) + array.select{ |word| word[0] == 'a' } end # keep only the elements that start with a vowel def select_elements_starting_with_vowel(array) + vowels = ['a','e','i','o','u','y'] + array.select{ |word| vowels.index(word[0]) != nil } end # remove instances of nil (but NOT false) from an array def remove_nils_from_array(array) + array.select{ |ele| ele != nil } end # remove instances of nil AND false from an array def remove_nils_and_false_from_array(array) + array.select{ |ele| ele != nil && ele != false } end # 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) + array.map{ |ele| ele.reverse } end # given an array of student names, like ['Bob', 'Dave', 'Clive'] # give every possible pairing - in this case: # [['Bob', 'Clive'], ['Bob', 'Dave'], ['Clive', 'Dave']] -# make sure you don't have the same pairing twice, +# make sure you don't have the same pairing twice, def every_possible_pairing_of_students(array) + array.combination(2).to_a end -# discard the first 3 elements of an 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..-1] end # add an element to the beginning of an array def add_element_to_beginning_of_array(array, element) + array.insert(0, element) end # sort an array of words by their last letter, e.g. # ['sky', 'puma', 'maker'] becomes ['puma', 'maker', 'sky'] def array_sort_by_last_letter_of_word(array) + array.sort{ |x, y| x[-1] <=> y[-1] } end # cut strings in half, and return the first half, e.g. # '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) + string[0..-(string.length/2 + 1)] end # turn a positive integer into a negative integer. A negative integer # stays negative def make_numbers_negative(number) + -number.abs end -# turn an array of numbers into two arrays of numbers, one an array of +# turn an array of numbers into two arrays of numbers, one an array of # even numbers, the other an array of odd numbers # even numbers come first # so [1, 2, 3, 4, 5, 6] becomes [[2, 4, 6], [1, 3, 5]] def separate_array_into_even_and_odd_numbers(array) + [array.select{ |ele| ele.even? }, array.select{ |ele| ele.odd? }] end # count the numbers of elements in an element which are palindromes @@ -63,33 +76,41 @@ def separate_array_into_even_and_odd_numbers(array) # e.g. 'bob'. So in the array ['bob', 'radar', 'eat'], there # are 2 palindromes (bob and radar), so the method should return 2 def number_of_elements_that_are_palindromes(array) + array.count{ |ele| ele == ele.reverse } end # return the shortest word in an array def shortest_word_in_array(array) + array.sort{ |x, y| x.size <=> y.size }[0] end # return the shortest word in an array def longest_word_in_array(array) + array.sort{ |x, y| y.size <=> x.size }[0] end # add up all the numbers in an array, so [1, 3, 5, 6] # returns 15 def total_of_array(array) + array.inject{ |sum, ele| sum += ele } end # turn an array into itself repeated twice. So [1, 2, 3] # becomes [1, 2, 3, 1, 2, 3] def double_array(array) + array.concat(array) end # convert a symbol into a string def turn_symbol_into_string(symbol) + symbol.to_s end # get the average from an array, rounded to the nearest integer # so [10, 15, 25] should return 17 def average_of_array(array) + avg = array.inject{ |sum, ele| sum += ele }/(array.length).to_f + avg.round end # get all the elements in an array, up until the first element @@ -97,12 +118,14 @@ 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) + array.take_while{ |ele| ele <= 5 } end # turn an array (with an even number of elements) into a hash, by # pairing up elements. e.g. ['a', 'b', 'c', 'd'] becomes # {'a' => 'b', 'c' => 'd'} def convert_array_to_a_hash(array) + Hash[*array] end # get all the letters used in an array of words and return @@ -110,75 +133,90 @@ def convert_array_to_a_hash(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) + array.join.split('').uniq.sort end # swap the keys and values in a hash. e.g. # {'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 # add all the keys and all the values together, e.g. # {1 => 1, 2 => 2} becomes 6 def add_together_keys_and_values(hash) + hash.flatten.inject{ |sum, ele| sum += ele } end # take out all the capital letters from a string # so 'Hello JohnDoe' becomes 'ello ohnoe' def remove_capital_letters_from_string(string) + string.tr('A-Z', '') end # 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, # so 9.52 becomes 9 def round_down_number(float) + float.floor end # take a date and format it like dd/mm/yyyy, so Halloween 2013 # becomes 31/10/2013 def format_date_nicely(date) + "#{date.day}/#{date.month}/#{date.year}" end # get the domain name *without* the .com part, from an email address # so alex@makersacademy.com becomes makersacademy def get_domain_name_from_email_address(email) + email[/@(.+)\.com/, 1] end -# capitalize the first letter in each word of a string, +# capitalize the first letter in each word of a string, # except 'a', 'and' and 'the' # *unless* they come at the start of the start of the string, e.g. # 'the lion the witch and the wardrobe' becomes # 'The Lion the Witch and the Wardrobe' def titleize_a_string(string) + except = ['a', 'and', 'the'] + string.capitalize.split(' ').each{ |ele| ele.capitalize! if except.index(ele) == nil }.join(' ') end # return true if a string contains any special characters # where 'special character' means anything apart from the letters # a-z (uppercase and lower) or numbers def check_a_string_for_special_characters(string) + string =~ /[^a-zA-Z1-9]+/ ? true : false end # 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 +# 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.include?(range.end) ? false : true end # get the square root of a number def square_root_of(number) + Math.sqrt(number) end # count the number of words in a file def word_count_a_file(file_path) + File.read(file_path).split(' ').count end # --- tougher ones --- @@ -187,12 +225,15 @@ def word_count_a_file(file_path) # called call_method_from_string('foobar') # the method foobar should be invoked def call_method_from_string(str_method) + "test".send(:str_method) end # return true if the date is a uk bank holiday for 2014 # the list of bank holidays is here: # https://www.gov.uk/bank-holidays def is_a_2014_bank_holiday?(date) + bank_holidays_2014 = [Time.new(2014, 1, 1), Time.new(2014, 4, 18), Time.new(2014, 1, 21), Time.new(2014, 5, 5), Time.new(2014, 8, 25), Time.new(2014, 12, 25), Time.new(2014, 12, 26)] + bank_holidays_2014.include?(date) end # given your birthday this year, this method tells you @@ -200,6 +241,13 @@ def is_a_2014_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) + t = birthday + y = t.year + while !t.friday? + y += 1 + t = Time.new(y, t.month, t.day) + end + return t.year end # in a file, total the number of times words of different lengths @@ -208,6 +256,7 @@ def your_birthday_is_on_a_friday_in_the_year(birthday) # 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) + puts File.read(file_path).split(' ') end # implement fizzbuzz without modulo, i.e. the % method @@ -218,7 +267,7 @@ def fizzbuzz_without_modulo # print the lyrics of the song 99 bottles of beer on the wall # http://www.99-bottles-of-beer.net/lyrics.html -# make sure you use the singular when you have one bottle of +# make sure you use the singular when you have one bottle of # beer on the wall, and print 'no more bottles of beer on the wall' # at the end. # (there's no RSpec test for this one) From 05bc5161c3d04a336383b5b98228ca7649497f8c Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 09:54:36 +0200 Subject: [PATCH 02/11] completes last three questions --- lib/questions.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/questions.rb b/lib/questions.rb index 253a293..56af23a 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -256,13 +256,25 @@ def your_birthday_is_on_a_friday_in_the_year(birthday) # 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) - puts File.read(file_path).split(' ') + all_words = File.read(file_path).gsub!(/[^0-9A-Za-z]/, ' ').split(' ') + hash_count = Hash.new + all_words.each do |word| + hash_count[word.length] ||= 0 + hash_count[word.length] += 1 + end + return hash_count end # implement fizzbuzz without modulo, i.e. the % method # go from 1 to 100 # (there's no RSpec test for this one) def fizzbuzz_without_modulo + (1..100).each{|i| + x = '' + x += 'Fizz' if i/3.to_f == (i/3) + x += 'Buzz' if i/5.to_f == (i/5) + puts(x.empty? ? i : x); +} end # print the lyrics of the song 99 bottles of beer on the wall @@ -272,4 +284,13 @@ def fizzbuzz_without_modulo # at the end. # (there's no RSpec test for this one) def ninety_nine_bottles_of_beer + for i in (0..99).to_a.reverse + if i == 0 + puts "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall." + elsif i == 1 + puts "1 bottle of beer on the wall, 1 bottle of beer.\nTake one down and pass it around, no more bottles of beer on the wall.\n\n" + else + puts "#{i} bottles of beer on the wall, #{i} bottles of beer.\nTake one down and pass it around, #{i-1} bottles of beer on the wall.\n\n" + end + end end From 79cef7172463ca267ad20382a24aa78fe097f76d Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 10:57:59 +0200 Subject: [PATCH 03/11] refactors some answers --- lib/questions.rb | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index 56af23a..e0efeda 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -5,8 +5,7 @@ def select_elements_starting_with_a(array) # keep only the elements that start with a vowel def select_elements_starting_with_vowel(array) - vowels = ['a','e','i','o','u','y'] - array.select{ |word| vowels.index(word[0]) != nil } + array.select{ |word| word.match(/^[aeiouy]/) } end # remove instances of nil (but NOT false) from an array @@ -109,8 +108,7 @@ def turn_symbol_into_string(symbol) # get the average from an array, rounded to the nearest integer # so [10, 15, 25] should return 17 def average_of_array(array) - avg = array.inject{ |sum, ele| sum += ele }/(array.length).to_f - avg.round + (array.inject{ |sum, ele| sum += ele }.to_f/array.size).round end # get all the elements in an array, up until the first element @@ -186,8 +184,7 @@ 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) - except = ['a', 'and', 'the'] - string.capitalize.split(' ').each{ |ele| ele.capitalize! if except.index(ele) == nil }.join(' ') + string.capitalize.split(' ').each{ |ele| ele.capitalize! unless ['a', 'and', 'the'].index(ele) }.join(' ') end # return true if a string contains any special characters @@ -256,9 +253,8 @@ def your_birthday_is_on_a_friday_in_the_year(birthday) # 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) - all_words = File.read(file_path).gsub!(/[^0-9A-Za-z]/, ' ').split(' ') hash_count = Hash.new - all_words.each do |word| + File.read(file_path).gsub!(/[^0-9A-Za-z]/, ' ').split(' ').each do |word| hash_count[word.length] ||= 0 hash_count[word.length] += 1 end @@ -269,12 +265,12 @@ def count_words_of_each_length_in_a_file(file_path) # go from 1 to 100 # (there's no RSpec test for this one) def fizzbuzz_without_modulo - (1..100).each{|i| - x = '' - x += 'Fizz' if i/3.to_f == (i/3) - x += 'Buzz' if i/5.to_f == (i/5) - puts(x.empty? ? i : x); -} + for i in (0..99) + x = '' + x += 'Fizz' if i/3.to_f == (i/3) + x += 'Buzz' if i/5.to_f == (i/5) + puts(x.empty? ? i : x); + end end # print the lyrics of the song 99 bottles of beer on the wall From 19fbcdf30d2c7755457057b4164e627c86451ef4 Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 17:05:24 +0200 Subject: [PATCH 04/11] makes Hound much happier --- lib/questions.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index e0efeda..6a748f7 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -1,27 +1,27 @@ # keep only the elements that start with an a def select_elements_starting_with_a(array) - array.select{ |word| word[0] == 'a' } + array.select { |word| word[0] == 'a' } end # keep only the elements that start with a vowel def select_elements_starting_with_vowel(array) - array.select{ |word| word.match(/^[aeiouy]/) } + array.select { |word| word.match(/^[aeiouy]/) } end # remove instances of nil (but NOT false) from an array def remove_nils_from_array(array) - array.select{ |ele| ele != nil } + array.select { |ele| ele != nil } end # remove instances of nil AND false from an array def remove_nils_and_false_from_array(array) - array.select{ |ele| ele != nil && ele != false } + array.select { |ele| ele != nil && ele != false } end # 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) - array.map{ |ele| ele.reverse } + array.map(&:reverse) end # given an array of student names, like ['Bob', 'Dave', 'Clive'] @@ -53,7 +53,7 @@ def array_sort_by_last_letter_of_word(array) # '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) - string[0..-(string.length/2 + 1)] + string[0..-(string.length / 2 + 1)] end # turn a positive integer into a negative integer. A negative integer @@ -67,7 +67,7 @@ def make_numbers_negative(number) # even numbers come first # so [1, 2, 3, 4, 5, 6] becomes [[2, 4, 6], [1, 3, 5]] def separate_array_into_even_and_odd_numbers(array) - [array.select{ |ele| ele.even? }, array.select{ |ele| ele.odd? }] + [array.select(&:even?), array.select(&:odd?)] end # count the numbers of elements in an element which are palindromes @@ -91,7 +91,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) - array.inject{ |sum, ele| sum += ele } + array.inject { |sum, ele| sum += ele } end # turn an array into itself repeated twice. So [1, 2, 3] @@ -108,7 +108,7 @@ def turn_symbol_into_string(symbol) # get the average from an array, rounded to the nearest integer # so [10, 15, 25] should return 17 def average_of_array(array) - (array.inject{ |sum, ele| sum += ele }.to_f/array.size).round + (array.inject { |sum, ele| sum += ele }.to_f / array.size).round end # get all the elements in an array, up until the first element @@ -145,7 +145,7 @@ 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) - hash.flatten.inject{ |sum, ele| sum += ele } + hash.flatten.inject { |sum, ele| sum += ele } end # take out all the capital letters from a string @@ -267,9 +267,9 @@ def count_words_of_each_length_in_a_file(file_path) def fizzbuzz_without_modulo for i in (0..99) x = '' - x += 'Fizz' if i/3.to_f == (i/3) - x += 'Buzz' if i/5.to_f == (i/5) - puts(x.empty? ? i : x); + x += 'Fizz' if i / 3.to_f == (i / 3) + x += 'Buzz' if i / 5.to_f == (i / 5) + puts(x.empty? ? i : x) end end From 1282f615c121af5935faad8f063635c4602628f6 Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 17:13:57 +0200 Subject: [PATCH 05/11] Trying to please the Hound --- lib/questions.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index 6a748f7..8c66529 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -10,12 +10,12 @@ def select_elements_starting_with_vowel(array) # remove instances of nil (but NOT false) from an array def remove_nils_from_array(array) - array.select { |ele| ele != nil } + array.select { |ele| !ele.nil? } end # remove instances of nil AND false from an array def remove_nils_and_false_from_array(array) - array.select { |ele| ele != nil && ele != false } + array.select { |ele| !ele.nil? && ele != false } end # don't reverse the array, but reverse every word inside it. e.g. @@ -240,11 +240,11 @@ def is_a_2014_bank_holiday?(date) def your_birthday_is_on_a_friday_in_the_year(birthday) t = birthday y = t.year - while !t.friday? + until t.friday? y += 1 t = Time.new(y, t.month, t.day) end - return t.year + t.year end # in a file, total the number of times words of different lengths @@ -258,14 +258,14 @@ def count_words_of_each_length_in_a_file(file_path) hash_count[word.length] ||= 0 hash_count[word.length] += 1 end - return hash_count + hash_count end # implement fizzbuzz without modulo, i.e. the % method # go from 1 to 100 # (there's no RSpec test for this one) def fizzbuzz_without_modulo - for i in (0..99) + (0..99).each do |i| x = '' x += 'Fizz' if i / 3.to_f == (i / 3) x += 'Buzz' if i / 5.to_f == (i / 5) @@ -280,13 +280,13 @@ def fizzbuzz_without_modulo # at the end. # (there's no RSpec test for this one) def ninety_nine_bottles_of_beer - for i in (0..99).to_a.reverse + (0..99).to_a.reverse.each do |i| if i == 0 puts "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall." elsif i == 1 puts "1 bottle of beer on the wall, 1 bottle of beer.\nTake one down and pass it around, no more bottles of beer on the wall.\n\n" else - puts "#{i} bottles of beer on the wall, #{i} bottles of beer.\nTake one down and pass it around, #{i-1} bottles of beer on the wall.\n\n" + puts "#{i} bottles of beer on the wall, #{i} bottles of beer.\nTake one down and pass it around, #{i - 1} bottles of beer on the wall.\n\n" end end end From f8a7fd7d5cf329ff80ec68ac3d23c9499429d0b1 Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 17:17:56 +0200 Subject: [PATCH 06/11] Be nice Hound --- lib/questions.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index 8c66529..2df2d30 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -184,7 +184,9 @@ 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) - string.capitalize.split(' ').each{ |ele| ele.capitalize! unless ['a', 'and', 'the'].index(ele) }.join(' ') + string.capitalize.split(' ').each do |ele| + ele.capitalize! unless ['a', 'and', 'the'].index(ele) + end.join(' ') end # return true if a string contains any special characters @@ -282,11 +284,14 @@ def fizzbuzz_without_modulo def ninety_nine_bottles_of_beer (0..99).to_a.reverse.each do |i| if i == 0 - puts "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall." + puts "No more bottles of beer on the wall, no more bottles of beer. + Go to the store and buy some more, 99 bottles of beer on the wall." elsif i == 1 - puts "1 bottle of beer on the wall, 1 bottle of beer.\nTake one down and pass it around, no more bottles of beer on the wall.\n\n" + puts "1 bottle of beer on the wall, 1 bottle of beer. + Take one down and pass it around, no more bottles of beer on the wall.\n\n" else - puts "#{i} bottles of beer on the wall, #{i} bottles of beer.\nTake one down and pass it around, #{i - 1} bottles of beer on the wall.\n\n" + puts "#{i} bottles of beer on the wall, #{i} bottles of beer. + Take one down and pass it around, #{i - 1} bottles of beer on the wall.\n\n" end end end From 44997c16ec8cb890d6baa4a933c3cf412c4cbbba Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 17:39:42 +0200 Subject: [PATCH 07/11] reduces long lines --- lib/questions.rb | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index 2df2d30..1a1f175 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -46,7 +46,7 @@ def add_element_to_beginning_of_array(array, element) # sort an array of words by their last letter, e.g. # ['sky', 'puma', 'maker'] becomes ['puma', 'maker', 'sky'] def array_sort_by_last_letter_of_word(array) - array.sort{ |x, y| x[-1] <=> y[-1] } + array.sort { |x, y| x[-1] <=> y[-1] } end # cut strings in half, and return the first half, e.g. @@ -75,17 +75,17 @@ def separate_array_into_even_and_odd_numbers(array) # e.g. 'bob'. So in the array ['bob', 'radar', 'eat'], there # are 2 palindromes (bob and radar), so the method should return 2 def number_of_elements_that_are_palindromes(array) - array.count{ |ele| ele == ele.reverse } + array.count { |ele| ele == ele.reverse } end # return the shortest word in an array def shortest_word_in_array(array) - array.sort{ |x, y| x.size <=> y.size }[0] + array.sort { |x, y| x.size <=> y.size }[0] end # return the shortest word in an array def longest_word_in_array(array) - array.sort{ |x, y| y.size <=> x.size }[0] + array.sort { |x, y| y.size <=> x.size }[0] end # add up all the numbers in an array, so [1, 3, 5, 6] @@ -116,7 +116,7 @@ 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) - array.take_while{ |ele| ele <= 5 } + array.take_while { |ele| ele <= 5 } end # turn an array (with an even number of elements) into a hash, by @@ -185,7 +185,7 @@ def get_domain_name_from_email_address(email) # 'The Lion the Witch and the Wardrobe' def titleize_a_string(string) string.capitalize.split(' ').each do |ele| - ele.capitalize! unless ['a', 'and', 'the'].index(ele) + ele.capitalize! unless %w(a and the).index(ele) end.join(' ') end @@ -284,14 +284,23 @@ def fizzbuzz_without_modulo def ninety_nine_bottles_of_beer (0..99).to_a.reverse.each do |i| if i == 0 - puts "No more bottles of beer on the wall, no more bottles of beer. - Go to the store and buy some more, 99 bottles of beer on the wall." + string = "No more bottles of beer on the wall, "\ + "no more bottles of beer.\n"\ + "Go to the store and buy some more, "\ + "99 bottles of beer on the wall.\n\n" + puts string elsif i == 1 - puts "1 bottle of beer on the wall, 1 bottle of beer. - Take one down and pass it around, no more bottles of beer on the wall.\n\n" + string = "1 bottle of beer on the wall, "\ + "1 bottle of beer.\n"\ + "Take one down and pass it around, "\ + "no more bottles of beer on the wall.\n\n" + puts string else - puts "#{i} bottles of beer on the wall, #{i} bottles of beer. - Take one down and pass it around, #{i - 1} bottles of beer on the wall.\n\n" + string = "#{i} bottles of beer on the wall, "\ + "#{i} bottles of beer.\n"\ + "Take one down and pass it around, "\ + "#{i - 1} bottles of beer on the wall.\n\n" + puts string end end end From 914202187cb196cdfca9328a88225e9d8f835c87 Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 17:41:44 +0200 Subject: [PATCH 08/11] dries the last question --- lib/questions.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index 1a1f175..c6413c5 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -231,7 +231,9 @@ def call_method_from_string(str_method) # the list of bank holidays is here: # https://www.gov.uk/bank-holidays def is_a_2014_bank_holiday?(date) - bank_holidays_2014 = [Time.new(2014, 1, 1), Time.new(2014, 4, 18), Time.new(2014, 1, 21), Time.new(2014, 5, 5), Time.new(2014, 8, 25), Time.new(2014, 12, 25), Time.new(2014, 12, 26)] + bank_holidays_2014 = [Time.new(2014, 1, 1), Time.new(2014, 4, 18), + Time.new(2014, 1, 21), Time.new(2014, 5, 5), Time.new(2014, 8, 25), + Time.new(2014, 12, 25), Time.new(2014, 12, 26)] bank_holidays_2014.include?(date) end @@ -288,19 +290,17 @@ def ninety_nine_bottles_of_beer "no more bottles of beer.\n"\ "Go to the store and buy some more, "\ "99 bottles of beer on the wall.\n\n" - puts string elsif i == 1 string = "1 bottle of beer on the wall, "\ "1 bottle of beer.\n"\ "Take one down and pass it around, "\ "no more bottles of beer on the wall.\n\n" - puts string else string = "#{i} bottles of beer on the wall, "\ "#{i} bottles of beer.\n"\ "Take one down and pass it around, "\ "#{i - 1} bottles of beer on the wall.\n\n" - puts string end + puts string end end From 05bd2ba5696971286cbbd7994c8deb65a9f01b5e Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 17:44:15 +0200 Subject: [PATCH 09/11] Align the elements of an array literal if they span more than one line. --- lib/questions.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index c6413c5..7fb7b7f 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -231,9 +231,15 @@ def call_method_from_string(str_method) # the list of bank holidays is here: # https://www.gov.uk/bank-holidays def is_a_2014_bank_holiday?(date) - bank_holidays_2014 = [Time.new(2014, 1, 1), Time.new(2014, 4, 18), - Time.new(2014, 1, 21), Time.new(2014, 5, 5), Time.new(2014, 8, 25), - Time.new(2014, 12, 25), Time.new(2014, 12, 26)] + bank_holidays_2014 = [ + Time.new(2014, 1, 1), + Time.new(2014, 4, 18), + Time.new(2014, 1, 21), + Time.new(2014, 5, 5), + Time.new(2014, 8, 25), + Time.new(2014, 12, 25), + Time.new(2014, 12, 26) + ] bank_holidays_2014.include?(date) end From cebb3392da34d37d923475cfe0d436383892305d Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 17:46:24 +0200 Subject: [PATCH 10/11] improves indentation --- lib/questions.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index 7fb7b7f..0db44b1 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -231,14 +231,14 @@ def call_method_from_string(str_method) # the list of bank holidays is here: # https://www.gov.uk/bank-holidays def is_a_2014_bank_holiday?(date) - bank_holidays_2014 = [ - Time.new(2014, 1, 1), - Time.new(2014, 4, 18), - Time.new(2014, 1, 21), - Time.new(2014, 5, 5), - Time.new(2014, 8, 25), - Time.new(2014, 12, 25), - Time.new(2014, 12, 26) + bank_holidays_2014 = [ + Time.new(2014, 1, 1), + Time.new(2014, 4, 18), + Time.new(2014, 1, 21), + Time.new(2014, 5, 5), + Time.new(2014, 8, 25), + Time.new(2014, 12, 25), + Time.new(2014, 12, 26) ] bank_holidays_2014.include?(date) end From 395e09b04194cdb665fc44f9fb19d5aa61219c38 Mon Sep 17 00:00:00 2001 From: Fabien Albi Date: Sat, 9 Apr 2016 17:49:19 +0200 Subject: [PATCH 11/11] improves indentation v2 --- lib/questions.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/questions.rb b/lib/questions.rb index 0db44b1..d940f19 100644 --- a/lib/questions.rb +++ b/lib/questions.rb @@ -231,7 +231,7 @@ def call_method_from_string(str_method) # the list of bank holidays is here: # https://www.gov.uk/bank-holidays def is_a_2014_bank_holiday?(date) - bank_holidays_2014 = [ + bank_holidays_2014 = [ Time.new(2014, 1, 1), Time.new(2014, 4, 18), Time.new(2014, 1, 21), @@ -239,7 +239,7 @@ def is_a_2014_bank_holiday?(date) Time.new(2014, 8, 25), Time.new(2014, 12, 25), Time.new(2014, 12, 26) - ] + ] bank_holidays_2014.include?(date) end