DEV Community

dev.to staff
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.

Examples

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!

Top comments (25)

Collapse
 
craigmc08 profile image
Craig McIlwrath • Edited

Recursive Haskell solution

eradicateTrailingZeroes :: (Integral a) => a -> a
eradicateTrailingZeroes 0 = 0
eradicateTrailingZeroes n
  | n `rem` 10 == 0 = eradicateTrailingZeroes $ n `quot` 10
  | otherwise       = n
Collapse
 
avalander profile image
Avalander

Does this work when passing 0 as argument?

Collapse
 
craigmc08 profile image
Craig McIlwrath

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

Collapse
 
zenmumbler profile image
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;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
centanomics profile image
Cent • Edited

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?

Collapse
 
vidit1999 profile image
Vidit Sarkar • Edited

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;
}
Collapse
 
centanomics profile image
Cent

Yep, that does it. Thanks!

Collapse
 
savagepixie profile image
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
Collapse
 
avalander profile image
Avalander • Edited

Haskell

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
Collapse
 
vidit1999 profile image
Vidit Sarkar • Edited

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;
}
Collapse
 
vidit1999 profile image
Vidit Sarkar • Edited

Python one liner

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

print(eradicate(9070)) # 907
print(eradicate(210000)) # 21
print(eradicate(10210)) # 1021
print(eradicate(0)) # 0
print(eradicate(-1050)) # -105
Collapse
 
lfrigodesouza profile image
Lucas Frigo de Souza

C#

        static void Main(string[] args)
        {
            System.Console.WriteLine("No Zero Challenge");
            Console.WriteLine(Eradicate(1450));   //145
            Console.WriteLine(Eradicate(960000)); //96
            Console.WriteLine(Eradicate(1050));   //105
            Console.WriteLine(Eradicate(-1050));  //-105
            Console.WriteLine(Eradicate(9070));   //907
            Console.WriteLine(Eradicate(210000)); //21
            Console.WriteLine(Eradicate(10210));  //1021
            Console.WriteLine(Eradicate(0));      //0
            Console.WriteLine(Eradicate(1234));   //1234
        }

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

Collapse
 
lfrigodesouza profile image
Lucas Frigo de Souza

and Dart

void main() {
  print(eradicate(1450));   //145
  print(eradicate(960000)); //96
  print(eradicate(1050));   //105
  print(eradicate(-1050));  //-105
  print(eradicate(9070));   //907
  print(eradicate(210000)); //21
  print(eradicate(10210));  //1021
  print(eradicate(0));      //10
  print(eradicate(1234));   //1234
}

double eradicate (double number){
  if(number == 0) return number;

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

  return number;
}

Collapse
 
aweleczka profile image
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

Collapse
 
aweleczka profile image
Alexander Weleczka
Test Run Successful.
Total tests: 9
     Passed: 9
Total time: 1.6941 Seconds
Collapse
 
corentinleffy profile image
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", () {
        expect(eradicateTrailingZeroes(zero), equals(hero));
      });
    });
  });
}

num eradicateTrailingZeroes(num number) {
  for (; (number % 10 == 0) ^ (number == 0); number /= 10) {}
  return number;
}
Collapse
 
aminnairi profile image
Amin • Edited

Elm

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

    else if remainderBy 10 number == 0 then
        eradicateZeros <| number // 10

    else
        number