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
  • 50,409
  • 9
  • 43
  • 69
b.tchman
  • 112
  • 2
  • 12
  • 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
  • 503
  • 6
  • 19
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,133
  • 1
  • 13
  • 33