Python Forum
Input a number with any number of digits and output it in reverse order of digits.
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input a number with any number of digits and output it in reverse order of digits.
#6
Little necronitpicking:

sort with reverse keyword gives a list ordered in a reverse order, not a reversed list. For the number/list used as an example it does not matter, as it was ordered, but otherwise it could. Beside [::-1] it could be done with .reverse() or reversed().

As "reversing" list is equivalent to sorting it on index in decreasing order, I tried to abuse sort/sorted to get such result and ended with:

In [38]: a = [1, 2, 5, 3]

In [39]: sorted(a, key = lambda x, i=iter(range(len(a))) : -next(i))
Out[39]: [3, 5, 2, 1]
it looks ugly, but probably less ugly than:

In [41]: list(map(lambda x:x[1], sorted(enumerate(a), reverse=True)))
Out[41]: [3, 5, 2, 1]
Perhaps there are other ways how to reverse list with sort/sorted one-liner and built-ins ...
Reply


Messages In This Thread
RE: Input a number with any number of digits and output it in reverse order of digits. - by zivoni - Mar-12-2017, 10:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 188 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  intering int number akbarza 1 297 Apr-28-2024, 08:55 AM
Last Post: Gribouillis
Brick Number stored as text with openpyxl CAD79 2 631 Apr-17-2024, 10:17 AM
Last Post: CAD79
  If a set element has digits in the element tester_V 3 398 Mar-25-2024, 04:43 PM
Last Post: deanhystad
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 445 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Prime number detector Mark17 5 918 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 1,108 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 558 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  capturing multiline output for number of parameters jss 3 889 Sep-01-2023, 05:42 PM
Last Post: jss
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 646 Aug-22-2023, 10:14 AM
Last Post: GYKR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020