Forked from nextacademy-private/power_of_enumerables.rb
Last active
March 1, 2016 09:38
-
-
Save yclim95/6ea1310c4d7fd3d91bb9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# TODO: Print the elements at indices 1, 3, 5, 7, etc. on separate lines. | |
# You should make use of Enumerable#each_with_index | |
def print_odd_indexed_integers(array) | |
array.each_with_index { |key,value| | |
if value.odd? | |
puts value.to_s | |
end | |
} | |
end | |
# TODO: Return the odd numbers from a list of integers. | |
# You should make use of Enumerable#select | |
def odd_integers(array) | |
array.select{ |num| | |
num.odd? | |
} | |
end | |
# TODO: Return the first number from an Array that is less than a particular number - 'limit.' | |
# You should make use of Enumerable#find | |
def first_under(array, limit) | |
array.find {|i| | |
i<limit | |
} | |
end | |
# TODO: Take an Array of Strings and return a new Array with an exclamation point appended to each String. | |
# You should make use of Enumerable#map | |
def add_bang(array) | |
array.map {|i| | |
a + "!" | |
} | |
end | |
# TODO: Calculate the sum of an Array of numbers. | |
# You should make use of Enumerable#reduce | |
def sum(array) | |
array.reduce(:+) | |
end | |
# TODO: Reorganize an Array of the elements into groups of 3, and then sort each group alphabetically. | |
# You should make use of Enumerable#each_slice | |
def sorted_triples(array) | |
temporary=array.each_slice(3).to_a | |
temporary.map{|index| | |
index.sort | |
} | |
end | |
# Driver code... don't change this. | |
print_odd_indexed_integers([2, 4, 6, 8, 10, 12]) # this should print "4\n8\n12" | |
puts odd_integers([3, 4, 7, 9, 10, 16]) == [3, 7, 9] | |
puts first_under([13, 21, 7, 0, 11, 106], 10) == 7 | |
puts add_bang(["hi", "mom"]) == ["hi!", "mom!"] | |
puts sum([1, 1, 2, 3, 5]) == 12 | |
words = %w(the salted pork is particularly good) | |
puts sorted_triples(words) == [["pork", "salted", "the"], | |
["good", "is", "particularly"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment