0

I have an assignment to take a number and to sort it in a ascending and descending order and then add it together to get a result. I know how to get the number sorted in a ascending order but not descending. For my code I have:

def addition(num):
    listedDes = list(str(num))
    listedDes.sort()
    print("".join(listedDes))
addition(3524)
Patrick Artner
  • 48,339
  • 8
  • 43
  • 63
KingNex
  • 120
  • 1
  • 10
  • Descending: `listedDes.sort(reverse=True)`. – Austin Jan 07 '19 at 16:44
  • 1
    You know how to sort - the problem reduces down to [How can I reverse a list in Python?](https://stackoverflow.com/questions/3940128/how-can-i-reverse-a-list-in-python) .. this is more awkward then `.sort(reverse=True)` or `.sort(key=lambda x:-x)` but would be a solution. – Patrick Artner Jan 07 '19 at 17:58

2 Answers2

1

You can sort by Descending by doing like this.

listedDes.sort(reverse=True)

Here's more in the docs: https://docs.python.org/3/howto/sorting.html#ascending-and-descending

sumpen
  • 493
  • 3
  • 18
0
def addition(num):
    listedDes = list(str(num))
    listedDes.sort()
    listedDes.reverse()
    print("".join(listedDes))
addition(3524)

This is already known answer but here I am writing for your understanding.

kvk30
  • 1,020
  • 1
  • 14
  • 30