DEV Community

Daily Challenge #195 - No Zeroes for Heroes

dev.to staff on February 26, 2020

Setup It has officially been decided that numbers that end with zeroes are boring. They might be fun in your world, but definitely not h...
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
Collapse
 
nijeesh4all profile image
Nijeesh Joshy • Edited

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

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
 
empereol profile image
Empereol • Edited

TypeScript

function trimTrailingZeroes(input: number): number {
  return parseFloat(input.toString().replace(/(?<=\d)0+$/g, ""));
}
Collapse
 
jaxon profile image
Ryan J • Edited

Javascript

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

Collapse
 
rafaacioly profile image
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"))
Collapse
 
mategreen profile image
matej

Java with recursion

public int cutZeroes(int i) {
   return i == 0 || i%10 != 0 ? i : cutZeroes(i/10);
}
Collapse
 
maskedman99 profile image
Rohit Prasad

Python

var = int(input("Enter the number: "))

while var != 0:
        if var%10 == 0:
                var = var/10
        else:
                break
print(int(var))