Typically, reversing an array is a pretty straightforward task even for novice programmers. But not when a crowd of angry zombies scratching your door, looking for a fresh brains. In this case even skilled ninja-geek probably prefers to quickly push his code on github to have enough time to find a chainsaw. So there's two obstacles:
Your code needs to be as short as possible, in fact not longer than 28 characters
Because you are scared and stressed you have forgotten how to use the standard reverse() method
Input: an array containing data of any types.
Ex: [1,2,3,'a','b','c',[]]
Output: [[],'c','b','a',3,2,1]
Tests
[1,2,3,4,5]
[2,4,6,8,10]
["pineapple", "pumpkin", "pear", "peach"]
Good luck!
This challenge comes from avadakedavra on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (8)
ES6
Nice solution!
would also work since a is said to already be an array
Edit: Um, nup Will. You're on to it Birand. Got the feeling the spread was there for a reason :|
In case anyone else is fooled into thinking you don't need to clone the original array in this case...
map
is smart enough to handle some mutations of the input array, like deleting the currently iterated value, but it isn't exactly making a copy of the array before the call.In this case,
a
is being shrunk from the end as it's iterated.Once the iterator gets to halfway, the pop function has shaved as many entries off the end, so the map function early exits.
Interestingly, the map function sets the length of the output array to be the same as the original array, so my function returns an incomplete array, with some of the values you'd expect in an array being missing properties, a little like in the result of
Array(6)
Haskell solution in exactly 28 characters:
Longer version of the same, but more readable:
Couldn't skip type annotations as that raises ambiguous type error.
Explanation:
foldl
applies a given function to all container elements going from inside out. When applied to list, it effectively traverses list in a reverse order.foldl
is applied toflip (:)
and[]
.foldl
has type signatureFoldable t => (b -> a -> b) -> b -> t a -> b
, which can be rewritten as([a] -> a -> [a]) -> [a] -> [a] -> [a]
.(:)
appends a value (first argument) to a list (second argument). It's type signature isa -> [a] -> [a]
. It almost looks like the required signature from the 3. point.flip
function in order to switch around(:)
first and second argument. This results in a functionflip (:)
with type signature of[a] -> a -> [a]
.flip (:)
is partially applied tofoldl
, it gives a functionfoldl (flip (:))
with a type signature[a] -> [a] -> [a]
. Givingfoldl
a initial value of[]
, gives the final formfoldl (flip (:)) []
with a type signature[a] -> [a]
.Python 🐍
Tried it in C# (64 chars)
Well.. 11 characters?
Python
Seems to work:
Bit more elegant that mine.