Tuesday, 12 July 2016

Select Map

Difficulty:
Easy

Tags:
enumerables, procs

Instructions:
The Pattern object not only functions as a Regexp. For example, check out what happens if you do Pattern === element. This problem demonstrates that using a Pattern with #grep will work much like using select and map in combination.

Problem and Solution:

# Solution to 'Select Map' on rubeque.com
# by jakthegr8
# http://www.rubeque.com/problems/select-map
assert_equal ["1", 2, "3", 4, "5"].grep(/[1-5]/){ |e| e.succ }, ["2", "4", "6"]
assert_equal [0, 1, 3, 5, 6].grep(1..5, &:succ), [2, 4, 6]
assert_equal ["1", "2", "3", "4", "5"].grep(proc{|ele| ele.to_i%2 ==1}){ |e| e.succ }, ["2", "4", "6"]
assert_equal [1, 2, 3, 4, 5].grep(proc{|ele| ele.to_i%2 ==1}, &:succ), [2, 4, 6]
view raw Select Map.rb hosted with ❤ by GitHub

No comments:

Post a Comment