0
$\begingroup$

So we all know it's easy to read a number from lowest place value to highest. A simple algorithm would be like :

while(n):
    a=n%10;
    n/=10;

but the question is how do you read it oppositely?

for eg : 12312. must result in me getting first 1 then 2 then 3 and so on.

I don't wanna form an array or reverse the number, unless there is a way to reverse the number with constant time-complexity.

Edit : I think the question is extremely vague so I'll try to explain what I need it for.

So i've been working on optimizing my code for an algorithm to convert binary to decimal.

My algorithm is : while(n) c+=(n%2)<<b++ , n/=10;

But then I figured I don't need the variable b, if i could somehow start from the front then an algorithm like c=2*c+n%2 would work.

I don't really wanna use a while loop to reverse a number as I think it would be inefficient than just having an extra variable.

An alternative that I thought of was having a float c then c=c/2+n%2 but the you'll need to know the number of digits of n. Which I could use logarithm for but I don't know that at that point if it is just better to use another variable. At its heart it is a time vs space complexity problem.

$\endgroup$
2
  • $\begingroup$ Usually questions that say "please accomplish X but don't do it using method Y" are not useful because they don't specify why you have rejected Y. It is better to specify what your requirements are rather than specifying what solutions you don't want to accept. If you are not happy with using an array or reversing the number, I suggest you spend some time to identify what requirements they violate (e.g., do you have limits on space complexity?). $\endgroup$
    – D.W.
    Commented Dec 20, 2020 at 19:45
  • $\begingroup$ @D.W. yeah it's more so time and space complexity issue I'll edit my post to specify it in detail. $\endgroup$
    – Avi
    Commented Dec 21, 2020 at 5:21

1 Answer 1

1
$\begingroup$

Well, you could construct the inverse of that number. 12312 will become 21321 and then use the piece of code you wrote. So: while (n) {inverse = inverse * 10 + (a % 10), a /= 10;}.

$\endgroup$
5
  • $\begingroup$ Yeah I could do that but I wanna know the most efficient way. I guess it's on me to not specify that. $\endgroup$
    – Avi
    Commented Dec 20, 2020 at 18:53
  • $\begingroup$ Well, it depends on what you want to do with those digits. $\endgroup$
    – andu eu
    Commented Dec 20, 2020 at 19:01
  • $\begingroup$ I don't understand what you mean by "inverse". Do you mean "reverse" the decimal representation? How do you "construct the inverse" of a number? What algorithm would you use? $\endgroup$
    – D.W.
    Commented Dec 20, 2020 at 19:46
  • $\begingroup$ My idea was to split the number per digit and return them back each $\endgroup$
    – Max N
    Commented Jan 20, 2021 at 12:54
  • $\begingroup$ @D.W. He just swaps the digit around in their ordering $\endgroup$
    – Max N
    Commented Jan 20, 2021 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.