DEV Community

Qroia FAK(C)E
Qroia FAK(C)E

Posted on

Codewars Challenge Day 3: Moving Zeros To The End

Details

Name Kata: Moving Zeros To The End
5kuy
Description:
Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.
Example

moveZeros([false,1,0,1,2,0,1,3,"a"]) // returns[false,1,1,2,1,3,"a",0,0]
Enter fullscreen mode Exit fullscreen mode

My Solutions

JavaScript

const moveZeros = (arr) => {
  if (arr[0]===0){arr.push(arr.splice(i, 1).pop())}
  for(var i=arr.length;i>0;i--){if(arr[i]===0){arr.push(arr.splice(i,1).pop())}}
  return arr
}
Enter fullscreen mode Exit fullscreen mode

Python

from typing import List

def move_zeros(lst: List) -> List:
    return [x for x in lst if x != 0] + [x for x in lst if x == 0]
Enter fullscreen mode Exit fullscreen mode

PHP

function moveZeros(array $items): array
{
    return array_pad(
      array_filter(
        $items, function($x) {return $x !== 0 and $x !== 0.0;}
      ), count($items), 
     0);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
seraph776 profile image
Seraph★776

You're violating Codewars Code of Conduct by posting solutions. You're honestly missing the point and purpose of sites like Codewars when you your post solutions like this...

Collapse
 
qroia profile image
Qroia FAK(C)E

I don't publish them so that people will just take the solutions and that's it. This is certainly not right, but there are people who don't really want to sit on Codewars, and just sometimes want to look at some problems and their solutions, without even using Codewars itself.
If a person would like to solve tasks in Codewars, I would certainly recommend him not to watch any complete solutions at all. And after solving it is of course possible to look, as Kodvars himself says. Also, do not forget that this is my own personal challange, which I publish purely for personal purposes for accountability).

Collapse
 
seraph776 profile image
Seraph★776

My friend, do you know why magicians never reveal their secrets? It's because the trick would lose its prestige... If you want a personal challenge of accountability then try creating a Kata, instead of posting the solutions to them.

Furthermore, regardless of your sincerest intentions, you're still violating Codewars Code of Conduct where it clearly says:

Please do not post your solutions on Github (or alike).

What you are doing is frowned upon and should not be done.

Collapse
 
mdev34765 profile image
Marshall

Why do we have to start from the back? And why is it testing for +0 as a value?