# Challenge: Reverse the digits.

• (4 Pages)
• 1
• 2
• 3
• Last »

## 47 Replies - 35718 Views - Last Post: 01 February 2014 - 03:36 PM

### #1 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

# Challenge: Reverse the digits.

Posted 22 November 2012 - 07:52 PM

This challenge came to mind today and is hopefully interesting.

Challenge: Reverse the digits of a non-integral number. For instance 12.46 should become 64.21 as an example. Another one would be 3.78 should become 8.73.

Rules:

1. You cannot convert the number to another type, like a string and just reverse the string.

2. The non-integral type must be preserved, so if you float for instance the output should be float. Likewise for double.

3. It must be decimal.

Have fun! />

This post has been edited by ButchDean: 22 November 2012 - 09:34 PM

Is This A Good Question/Topic? 3

## Replies To: Challenge: Reverse the digits.

### #2 mostyfriedman

• The Algorithmi

Reputation: 728
• Posts: 4,473
• Joined: 24-October 08

## Re: Challenge: Reverse the digits.

Posted 22 November 2012 - 09:09 PM

I think 12.46 => 64.21 instead of 64.12

### #3 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Challenge: Reverse the digits.

Posted 22 November 2012 - 09:33 PM

• MrCupOfT

Reputation: 2292
• Posts: 9,531
• Joined: 29-May 08

## Re: Challenge: Reverse the digits.

Posted 22 November 2012 - 09:55 PM

And what about negative numbers? -12.34 =? 43.21 or -43.21?

### #5 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: Challenge: Reverse the digits.

Posted 22 November 2012 - 10:28 PM

Spoiler

Done
Is this right ?

This post has been edited by TwoOfDiamonds: 22 November 2012 - 10:43 PM

### #6 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Challenge: Reverse the digits.

Posted 22 November 2012 - 10:45 PM

AdamSpeight2008, on 23 November 2012 - 04:55 AM, said:

And what about negative numbers? -12.34 =? 43.21 or -43.21?

-43.21. />

TwoOfDiamonds, on 23 November 2012 - 05:28 AM, said:

Spoiler

Done />/>
Is this right ?

Dammit, I forgot to say no loops.

Can you think of a solution without using loops as well as the aforementioned restrictions?

This solution isn't correct. Try it with 0.1 or any other number without a whole part.

This post has been edited by ButchDean: 22 November 2012 - 10:54 PM

### #7 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: Challenge: Reverse the digits.

Posted 22 November 2012 - 10:47 PM

ButchDean, on 23 November 2012 - 07:45 AM, said:

AdamSpeight2008, on 23 November 2012 - 04:55 AM, said:

And what about negative numbers? -12.34 =? 43.21 or -43.21?

-43.21. />

TwoOfDiamonds, on 23 November 2012 - 05:28 AM, said:

Spoiler

Done />/>
Is this right ?

Dammit, I forgot to say no loops.

Can you think of a solution without using loops as well as the aforementioned restrictions?

Haha I'll try once I come back from school It's like 7AM here
I knew it was too easy to be right

### #8 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 05:20 AM

Have you succeded in completing the challenge ?

### #9 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 07:35 AM

I don't put up challenges I can't complete. If you think it through it isn't that hard, and at some point if people aren't getting it (which they never seem to do with my challenges), I'll put a solution up.

With hindsight not allowing loops makes it stupidly complicated (as in you need to account for numerous individual cases), so I'll allow loops. I can't see why you would need more than one though.

• MrCupOfT

Reputation: 2292
• Posts: 9,531
• Joined: 29-May 08

## Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 07:41 AM

You could use recursion instead, or if you're feel sadomasochistic the Y Combinator.

This post has been edited by AdamSpeight2008: 23 November 2012 - 07:54 AM

### #11 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 08:42 AM

ButchDean, on 23 November 2012 - 04:35 PM, said:

I don't put up challenges I can't complete. If you think it through it isn't that hard, and at some point if people aren't getting it (which they never seem to do with my challenges), I'll put a solution up.

With hindsight not allowing loops makes it stupidly complicated (as in you need to account for numerous individual cases), so I'll allow loops. I can't see why you would need more than one though. />

I was just curious . I'm not familiar with your challenges
And as Adam said ... it can be done with recursion I guess ...

### #12 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 10:16 AM

Recursion is just the extra layer of calling a function within behavior similar or like a loop, so I personally think the recursive approach is even worse.

I'm expecting some level of math to be involved in solving this problem. It might be an idea to look over my code snippets.

AdamSpeight2008, on 23 November 2012 - 02:41 PM, said:

You could use recursion instead, or if you're feel sadomasochistic the Y Combinator.

Seriously, it's not that bad a problem at all.

### #13 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 10:16 AM

don't think i have the math knowledge to do this :-? but i'll try

### #14 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 03:55 PM

You should be okay.

### #15 baavgai

• Dreaming Coder

Reputation: 6337
• Posts: 13,532
• Joined: 16-October 07

## Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 04:48 PM

TwoOfDiamonds, on 23 November 2012 - 12:16 PM, said:

don't think i have the math knowledge to do this :-? but i'll try

Stop that!

This isn't a problem of complex math, only programming logic. Computers are pretty bad at math, they just do very simple manipulations very very fast.

Recursion of some sort is the obvious choice for this kind of thing. However, I'm not sure it should be a requirement, just strongly encouraged.

I'll offer my solution in a few days.