DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #286 - Reverse It Quickly!

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)

Collapse
 
_bkeren profile image
''

ES6


a=>[...a].map(a.pop,a)

Collapse
 
willsmart profile image
willsmart • Edited

Nice solution!

a=>a.map(a.pop,a)

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)

> revHalf([1,2,3,4,5,6])
< [6, 5, 4, empty × 3]
<    0: 6
<    1: 5
<    2: 4
<    length: 6
<    __proto__: Array(0)

// You'd expect keys 3, 4, 5 too
Collapse
 
cipharius profile image
Valts Liepiņš • Edited

Haskell solution in exactly 28 characters:

r=flip(:)`foldl`[]::[a]->[a]

Longer version of the same, but more readable:

myReverse :: [a] -> [a]
myReverse = flip (:) `foldl` []

Couldn't skip type annotations as that raises ambiguous type error.

Explanation:

  1. Function 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.
  2. Binary operator foldl is applied to flip (:) and [].
  3. foldl has type signature Foldable t => (b -> a -> b) -> b -> t a -> b, which can be rewritten as ([a] -> a -> [a]) -> [a] -> [a] -> [a].
  4. Binary operator (:) appends a value (first argument) to a list (second argument). It's type signature is a -> [a] -> [a]. It almost looks like the required signature from the 3. point.
  5. Use the flip function in order to switch around (:) first and second argument. This results in a function flip (:) with type signature of [a] -> a -> [a].
  6. Now when this flip (:) is partially applied to foldl, it gives a function foldl (flip (:)) with a type signature [a] -> [a] -> [a]. Giving foldl a initial value of [], gives the final form foldl (flip (:)) [] with a type signature [a] -> [a].
Collapse
 
rafaacioly profile image
Rafael Acioly

Python 🐍


from typing import List, Any

def reverse_(values: List[Any]) -> List[Any]:
  return values[::-1]

Collapse
 
pbouillon profile image
Pierre Bouillon

Tried it in C# (64 chars)

Guess I will die meme

var r=new ArrayList();foreach(var e in l)r.Insert(0,e);return r;
Collapse
 
szy13 profile image
szy13

Well.. 11 characters?

a = a[::-1]
Collapse
 
georgeoffley profile image
George Offley • Edited

Python

Seems to work:

def reverse(arr):
    rtnArr = []

    for i in arr[::-1]:
        rtnArr.append(i)

    return rtnArr
Collapse
 
georgeoffley profile image
George Offley

Bit more elegant that mine.