## DEV Community is a community of 871,688 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

dev.to staff

Posted on

# Daily Challenge #195 - No Zeroes for Heroes

### Setup

It has officially been decided that numbers that end with zeroes are boring. They might be fun in your world, but definitely not here. Implement a function to eradicate any trailing zeroes. If the given number is 0, just leave him alone. Poor guy anyway.

1450 -> 145
960000 -> 96
1050 -> 105
-1050 -> -105

### Tests

9070
210000
10210
0

This challenge comes from thecodix 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!

## Discussion (26)

``````eradicateTrailingZeroes :: (Integral a) => a -> a
| n `rem` 10 == 0 = eradicateTrailingZeroes \$ n `quot` 10
| otherwise       = n
``````
Avalander

Does this work when passing `0` as argument?

Craig McIlwrath

Good catch, guess I missed that case. Updated it.

Avalander • Edited on

``````import Data.List (dropWhileEnd)

noZeroes :: Int -> Int
noZeroes 0 = 0
noZeroes n = (read . dropWhileEnd (== '0') . show) n
``````

And the output:

``````main = do
print (noZeroes 0)       -- 0
print (noZeroes 9070)    -- 907
print (noZeroes 210000)  -- 21
print (noZeroes 10210)   -- 1021
print (noZeroes 1450)    -- 145
print (noZeroes 960000)  -- 96
print (noZeroes 1050)    -- 105
print (noZeroes (-1050)) -- -105
``````
Cent | Shannon Myers • Edited on

Javascript

``````
const removeTrailingZeros = (number) => {
if (number === 0) return number;
if (number%10 === 0 ) removeTrailingZeros(number/10);
else return number;
}

``````

I just realized the function returns undefined when it's not 0 so let me try to fix that real quick lol

``````
const removeTrailingZeros = (number) => {
while(number % 10 === 0 && number !== 0) {
number= number / 10;
}
return number;
}

``````

codepen

Can someone tell my why the recursion I tried to do didn't work?

Vidit Sarkar • Edited on

I guess you just need to add a 'return' in front of

``````removeTrailingZeros(number/10)
``````

The code will be--

``````const removeTrailingZeros = (number) => {
if (number === 0) return number;
if (number%10 === 0 ) return removeTrailingZeros(number/10);
else return number;
}
``````
Cent | Shannon Myers

Yep, that does it. Thanks!

zenmumbler

I find it interesting that every solution here uses strings, here's one acting on the numbers itself. (tested against the above and other values).

``````function deathToZeroes(n) {
while ((Math.abs(n) > 9) && (n % 10 === 0)) {
n /= 10;
}
return n;
}
``````
SavagePixie

Elixir

Elixir with recursion:

``````def dezerofy(0), do: 0
def dezerofy(n), do: _dezerofy(n, rem(n, 10))
defp _dezerofy(n, 0), do: _dezerofy(div(n, 10), rem(n, 100))
defp _dezerofy(n, _), do: n
``````

Elixir without recursion, inspired by @avalander 's solution:

``````def dezerofy(0), do: 0
def dezerofy(n), do: n
|> Integer.digits
|> Enum.reverse
|> Enum.drop_while(&(&1 == 0))
|> Enum.reverse
|> Integer.undigits
``````
lormayna
``````>>> def eradicate(n):
n_as_str = str(n)
if n_as_str == "0":
return 0
if n_as_str[-1] == "0":
else:
return n

907
0
96
21
``````
Rafael Acioly

Nice solution :)

Take a loot at `strip` method from `str` objects;
dev.to/rafaacioly/comment/m1ib

Corentin Leffy

A Dart solution using TDD :

``````
void main() {
group("Eradicate trailing zeroes :", () {
final zeroesToHeroes = {
1450: 145,
960000: 96,
1050: 105,
-1050: -105,
9070: 907,
210000: 21,
10210: 1021,
0: 0
};
zeroesToHeroes.forEach((zero, hero) {
test("\$zero should become \$hero", () {
});
});
});
}

for (; (number % 10 == 0) ^ (number == 0); number /= 10) {}
return number;
}
``````
Amin • Edited on

Elm

``````eradicateZeros : Int -> Int
if number == 0 then
number

else if remainderBy 10 number == 0 then

else
number
``````
Vidit Sarkar • Edited on

C++

``````int removeZeros(int number){
// if number is 0 return 0
if(number == 0)
return 0;

// if number is not 0 then divide the number by 10 until last digit of number is non-zero
while(number%10 == 0){
number /= 10;
}
return number;
}
``````
Vidit Sarkar • Edited on

Python one liner

``````eradicate = lambda number : 0 if number==0 else int(str(number).strip('0'))

``````
Lucas Frigo de Souza

C#

``````        static void Main(string[] args)
{
System.Console.WriteLine("No Zero Challenge");
}

{
if (number == 0) return number;
while(number.ToString()[^1] == '0')
number /= 10;
return number;
}

``````
Lucas Frigo de Souza

and Dart

``````void main() {
}

if(number == 0) return number;

while(number.remainder(10) == 0)
number /= 10;

return number;
}

``````
Alexander Weleczka

Felt like a bit of recursion is always fun to do.
And I couldn't remember when I used extensions in C# last...

``````public static int StripTrailingZeros(this int input)
{
if(input != 0 && input % 10 == 0)
{
return (input / 10).StripTrailingZeros();
}

return input;
}
``````

Project on GitHub

Alexander Weleczka
``````Test Run Successful.
Total tests: 9
Passed: 9
Total time: 1.6941 Seconds
``````
Nijeesh Joshy • Edited on

## Ruby

### Regex

``````# using strings
def remove_trailing_zeros(number)
number
.to_s
.gsub(/0{1,}\$/,"")
.to_i || 0
end
``````

### Recursion

``````def remove_trailing_zeros(number)
return 0 if number == 0
return remove_trailing_zeros(number / 10) if number % 10 == 0
return number
end

``````

TESTS

``````require "test/unit"

class RemoveTrailingZerosTest < Test::Unit::TestCase
def test_remove_trailing_zeros
assert_equal 907, remove_trailing_zeros(9070)
assert_equal 21, remove_trailing_zeros(210000)
assert_equal 1021, remove_trailing_zeros(10210)
assert_equal 0, remove_trailing_zeros(0)
end
end

``````

TypeScript

``````function trimTrailingZeroes(input: number): number {
return parseFloat(input.toString().replace(/(?<=\d)0+\$/g, ""));
}
``````
Rafael Acioly

Python solution 🐍

``````def eradicate(value: int) -> int:
value_as_str = str(value)
is_zero = value_as_str == "0"

return 0 if is_zero else int(value_as_str.strip("0"))
``````
Ryan J • Edited on

Javascript

``````function removeLast0(input) {
if (input === 0) return 0;
if (input % 10 === 0) return removeLast0(input / 10);
return input;
}

``````
matej

Java with recursion

``````public int cutZeroes(int i) {
return i == 0 || i%10 != 0 ? i : cutZeroes(i/10);
}
``````
``````var = int(input("Enter the number: "))