A representative but thoroughly useless and unscientific way to see how people write short pieces of code.
Write a function, method, or other similar construct in your favorite language that:
- accepts an array
arr
and a valuev
- returns the element in
arr
whose successor isv
, if there is such an element; otherwise return nothing
Test cases:
find_before([5, 6, 7], 6)
# => 5
find_before([5, 6, 7], 7)
# => 6
find_before([5, 6, 7], 5)
# => nothing
find_before([5, 6, 7], 999)
# => nothing
Some examples in Ruby:
CS 101
Entry-level Rubyist
Another flavor
Module-opening variant