Created
February 26, 2015 14:20
-
-
Save katiebuilds/d9c990d8e6133ed36fb3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'minitest/autorun' | |
require 'minitest/pride' | |
# Write a series of methods which use the .any, .all, .map, .select, .reject, and | |
# .reduce methods in Enumerable. Each of your methods should be one line. | |
# WRITE YOUR CODE HERE. In this challenge, names longer than 5 characters are | |
# considered long. | |
def has_even?(array) | |
array.any? {|n| n.even?} | |
end | |
def all_short?(array) | |
array.all? {|w| w.length < 6} | |
end | |
def squares(array) | |
array.map {|n| n * n} | |
end | |
def just_short(array) | |
array.select {|w| w.length < 6} | |
end | |
def no_long(array) | |
array.reject {|w| w.length > 5} | |
end | |
def product(array) | |
array.reduce(:*) | |
end | |
class EnumerableChallenge < MiniTest::Test | |
def test_any | |
assert has_even?([2, 3, 4, 5, 6]) | |
assert has_even?([-2, 3, -4, 5, -6]) | |
refute has_even?([3, 5]) | |
refute has_even?([-3, -5]) | |
end | |
def test_all | |
assert all_short?(["Amy", "Bob", "Cam"]) | |
assert all_short?(["Zeke", "Yoo", "Xod"]) | |
refute all_short?(["Amy", "Bob", "Cammie"]) | |
refute all_short?(["Zachary", "Yoo", "Xod"]) | |
end | |
def test_map | |
assert_equal [1, 4, 9], squares([1, 2, 3]) | |
assert_equal [16, 36, 81], squares([4, 6, 9]) | |
end | |
def test_select | |
assert_equal ["Amy", "Bob", "Cam"], just_short(["Amy", "Bob", "Cam"]) | |
assert_equal ["Zeke", "Yoo", "Xod"], just_short(["Zeke", "Yoo", "Xod"]) | |
assert_equal ["Amy", "Bob"], just_short(["Amy", "Bob", "Cammie"]) | |
assert_equal ["Yoo", "Xod"], just_short(["Zachary", "Yoo", "Xod"]) | |
end | |
def test_reject | |
assert_equal ["Amy", "Bob", "Cam"], no_long(["Amy", "Bob", "Cam"]) | |
assert_equal ["Zeke", "Yoo", "Xod"], no_long(["Zeke", "Yoo", "Xod"]) | |
assert_equal ["Amy", "Bob"], no_long(["Amy", "Bob", "Cammie"]) | |
assert_equal ["Yoo", "Xod"], no_long(["Zachary", "Yoo", "Xod"]) | |
end | |
def test_reduce | |
assert_equal 1, product([1, 1, 1]) | |
assert_equal 150, product([3, 5, 10]) | |
assert_equal 0, product([18, 13, 0]) | |
assert_equal 12, product([2, 3, 2]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment