Challenge: Reverse the digits.

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

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

#1 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Challenge: Reverse the digits.

Post icon  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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Challenge: Reverse the digits.

Posted 22 November 2012 - 09:33 PM

Yep, my bad. :)
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Challenge: Reverse the digits.

Posted 22 November 2012 - 10:45 PM

View PostAdamSpeight2008, on 23 November 2012 - 04:55 AM, said:

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

-43.21. :)/>

View PostTwoOfDiamonds, 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?

[edit]

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

Was This Post Helpful? 0
  • +
  • -

#7 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

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

Re: Challenge: Reverse the digits.

Posted 22 November 2012 - 10:47 PM

View PostButchDean, on 23 November 2012 - 07:45 AM, said:

View PostAdamSpeight2008, on 23 November 2012 - 04:55 AM, said:

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

-43.21. :)/>

View PostTwoOfDiamonds, 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 :D I'll try once I come back from school :) It's like 7AM here >.<
I knew it was too easy to be right :D
Was This Post Helpful? 0
  • +
  • -

#8 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • 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 ?
Was This Post Helpful? 0
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • 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

Was This Post Helpful? 0
  • +
  • -

#11 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

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

Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 08:42 AM

View PostButchDean, 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 :D
And as Adam said ... it can be done with recursion I guess ...
Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • 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.

View PostAdamSpeight2008, 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. :)
Was This Post Helpful? 0
  • +
  • -

#13 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 03:55 PM

You should be okay. :)
Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,639
  • Joined: 16-October 07

Re: Challenge: Reverse the digits.

Posted 23 November 2012 - 04:48 PM

View PostTwoOfDiamonds, 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! :P

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.
Was This Post Helpful? 0
  • +
  • -

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