Write a program to reverse the digits of a positive integer but without converting it to a string.
probably not the best solution but it works. (unless you want leading zeros...but I'm assuming that's not the case since it wants a number).
function reverseNum(n) { const divisor = 10; let reversed = 0; let holder = []; while(n > 0) { holder.push(n % divisor); n = (Math.floor(n / divisor)); } let place = Math.pow(10, holder.length-1); for(let i = 0; i < holder.length; i++) { reversed += (holder[i] * place); place /= 10; } return reversed; }
F#
let rec loop rev i = if i = 0 then rev else loop (rev * 10 + (i % 10)) (i / 10) // usage, returns 987654321 let reversed = loop 0 123456789
while
i % 10
i
rev * 10
i / 10
I happened to remember these little number tricks from a previous challenge. This is basically using integers as digit stacks.
Thank you, Ali. This has been one of the most fun challenges.
Took me awhile but here is the C# version.
The gist is that, remainder is calculated for each digit, stored in a stack (LIFO - last in first out) to reverse the remainder.
Lastly the total is reconstructed from the stack.
total
Runnable code on .NET Fiddle
using System; using System.Collections.Generic; public class Program { public static void Main() { var a = new int[] {123456, 5, 654321, 1234567890}; foreach (var n in a) { Console.WriteLine($"Reversed = {Reverse(n)}"); } } private static int Reverse(int input) { var stack = BuildStack(input); return ConvertStackToNumber(stack); } private static Stack<int> BuildStack(int input) { var power = 0; var stack = new Stack<int>(); while (true) { power++; var modBy = (int) Math.Pow(10, power); var divisior = (int) Math.Pow(10, power - 1); var remainder = (input % modBy) / divisior; if (remainder == 0 && power != 1) break; stack.Push(remainder); } return stack; } private static int ConvertStackToNumber(Stack<int> stack) { var total = 0; var power = 0; while (stack.Count > 0) { var current = stack.Pop(); total += current * (int)Math.Pow(10, power++); } return total; } }
Oh that's a really cool approach!
#! /usr/bin/perl use warnings; use strict; sub rev { my ($x) = @_; my $r = my $z = 0; my $start = 1; while ($x) { ! ($r *= 10) and $z += $start or undef $start; $r += $x % 10; $x = ($x - $x % 10) / 10; } return '0' x ($z - 1) . $r } use Test::More tests => 4; is rev(3), 3; is rev(123456789), 987654321; is rev(4444), 4444; is rev(1000), '0001';
My Python solution:
def reverse_number(num, running_value=0): if num == 0: return running_value // 10 quotient, remainder = divmod(num, 10) running_value += remainder return reverse_number(quotient, running_value * 10) print(reverse_number(123456)) print(reverse_number(5))
And now this is... 😮
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Wednesday
Write a program to reverse the digits of a positive integer but without converting it to a string.
probably not the best solution but it works. (unless you want leading zeros...but I'm assuming that's not the case since it wants a number).
F#
whileloop on compile)i % 10gets the right-most digit ofirev * 10shifts the numbers left, with right-most zeroi / 10shifts the numbers right, dropping right-most digitI happened to remember these little number tricks from a previous challenge. This is basically using integers as digit stacks.
Thank you, Ali.
This has been one of the most fun challenges.
Took me awhile but here is the C# version.
The gist is that, remainder is calculated for each digit, stored in a stack (LIFO - last in first out) to reverse the remainder.
Lastly the
totalis reconstructed from the stack.Runnable code on .NET Fiddle
Oh that's a really cool approach!
My Python solution:
And now this is... 😮