-
Notifications
You must be signed in to change notification settings - Fork 395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
39 out 41 #210
base: master
Are you sure you want to change the base?
39 out 41 #210
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,152 +1,181 @@ | ||
# keep only the elements that start with an a | ||
def select_elements_starting_with_a(array) | ||
array.select { |item| item.start_with?('a') } | ||
end | ||
|
||
# keep only the elements that start with a vowel | ||
def select_elements_starting_with_vowel(array) | ||
array.grep(/^[aeiou]/) | ||
end | ||
|
||
# remove instances of nil (but NOT false) from an array | ||
def remove_nils_from_array(array) | ||
array.select { |item| !item.nil? } | ||
end | ||
|
||
# remove instances of nil AND false from an array | ||
def remove_nils_and_false_from_array(array) | ||
array.select { |item| !!item } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use reject instead of inverting select. |
||
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(&: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, | ||
def every_possible_pairing_of_students(array) | ||
array.combination(2) | ||
end | ||
|
||
# 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.unshift(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_by { |item| item[-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.size.to_f / 2).ceil] | ||
end | ||
|
||
# turn a positive integer into a negative integer. A negative integer | ||
# stays negative | ||
def make_numbers_negative(number) | ||
number.abs * -1 | ||
end | ||
|
||
# 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(&:even?), array.select(&:odd?)] | ||
end | ||
|
||
# count the numbers of elements in an element which are palindromes | ||
# a palindrome is a word that's the same backwards as forward | ||
# 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 { |element| element == element.reverse } | ||
end | ||
|
||
# return the shortest word in an array | ||
def shortest_word_in_array(array) | ||
array.min | ||
end | ||
|
||
# return the shortest word in an array | ||
def longest_word_in_array(array) | ||
array.max_by { |word| word.length } | ||
end | ||
|
||
# add up all the numbers in an array, so [1, 3, 5, 6] | ||
# returns 15 | ||
def total_of_array(array) | ||
array.sum | ||
end | ||
|
||
# turn an array into itself repeated twice. So [1, 2, 3] | ||
# becomes [1, 2, 3, 1, 2, 3] | ||
def double_array(array) | ||
array * 2 | ||
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) | ||
(array.sum.to_f / array.size).ceil | ||
end | ||
|
||
# get all the elements in an array, up until the first element | ||
# which is greater than five. e.g. | ||
# [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[0, array.find_index { |n| n > 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 | ||
# it as a array of letters, in alphabetical order | ||
# . 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.map { |word| word.split(//) }.flatten.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.reduce(:+).sum | ||
end | ||
|
||
# take out all the capital letters from a string | ||
# so 'Hello JohnDoe' becomes 'ello ohnoe' | ||
def remove_capital_letters_from_string(string) | ||
string.gsub(/[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.strftime("%d/%m/%Y") | ||
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[/(?<=@).*(?=\.)/] | ||
end | ||
|
||
# capitalize the first letter in each word of a string, | ||
|
@@ -155,30 +184,39 @@ 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) | ||
junctions = ["a", "and", "the"] | ||
str = string.split.map { |word| junctions.include?(word) ? word : word.capitalize } | ||
str.join(" ").sub(/^./, &:upcase) | ||
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.match?(/\W/) | ||
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.last | ||
end | ||
|
||
# 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.exclude_end? | ||
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.readlines(file_path).map(&:split).flatten.length | ||
`wc -w #{file_path}`[/\d+/].to_i | ||
end | ||
|
||
# --- tougher ones --- | ||
|
@@ -187,6 +225,7 @@ 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) | ||
String.str_method | ||
end | ||
|
||
# return true if the date is a uk bank holiday for 2014 | ||
|
@@ -200,6 +239,10 @@ 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) | ||
until birthday.friday? do | ||
birthday += 31536000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use underscores(_) as decimal mark and separate every 3 digits with them. |
||
end | ||
birthday.year | ||
end | ||
|
||
# in a file, total the number of times words of different lengths | ||
|
@@ -208,12 +251,28 @@ 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) | ||
result = Hash.new 0 | ||
lines = File.readlines(file_path) | ||
words = lines.map(&:split).flatten.map { |words| words.gsub(/\W/, "") } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shadowing outer local variable - words. |
||
words.each { |word| result[word.length] += 1 } | ||
result | ||
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.upto(100) do |x| | ||
if (x / 3.0).to_i == (x / 3.0) && (x / 5.0).to_i == (x / 5.0) | ||
puts " #{x} fizzbuzz" | ||
next | ||
elsif (x / 3.0).to_i == (x / 3.0) | ||
puts " #{x} fizz" | ||
next | ||
elsif (x / 5.0).to_i == (x / 5.0) | ||
puts " #{x} buzz" | ||
end | ||
end | ||
end | ||
|
||
# print the lyrics of the song 99 bottles of beer on the wall | ||
|
@@ -223,4 +282,5 @@ def fizzbuzz_without_modulo | |
# at the end. | ||
# (there's no RSpec test for this one) | ||
def ninety_nine_bottles_of_beer | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use reject instead of inverting select.