DEV Community

dev.to staff
dev.to staff

Posted on

6

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!

Runner H image

An AI Agent That Handles Life, Not Just Work

From ordering flowers to booking your dinner — let Runner H turn your ideas into actions. No prompts, no hassle. Just outcomes.

Try for Free

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
 
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
 
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
 
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
 
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
 
lormayna profile image
lormayna
>>> def eradicate(n):
    n_as_str = str(n)
    if n_as_str == "0":
        return 0
    if n_as_str[-1] == "0":
        return eradicate(int(n_as_str[:-1]))
    else:
        return n


>>> eradicate(9070)
907
>>> eradicate(0)
0
>>> eradicate(96000)
96
>>> eradicate(21000)
21
Collapse
 
rafaacioly profile image
Rafael Acioly

Nice solution :)

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

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
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;
}

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay