0

Trying to take an integer and return it from the function as an descending integer.

I did it first like this:

def Descending_Order(num):
    n = str(num)
    return(int(n[::-1]))

Because the tests only had numbers that were in descending order; kinda cheezy but hey it worked.. Now I want to do a number that is any number and make it return the largest to smallest number.

As you can see i tried to dump the int into a string (n) and manipulate it like an array I suppose.

EG:

1201 would return 2110

def Descending_Order(num):
    n = str(num)
    i = 0
    swap = 0

    while i < len(n):
        if i+1>len(n):
            break
        elif n[i] < n[i+1]:
            swap = n[i]
            n[i]= n[i+1]
            n[i+1]=swap
            i+=1
        else:    
            i+=1
    return(n)

print(Descending_Order(1201))
ADM
  • 20,406
  • 11
  • 52
  • 83
SirToews
  • 43
  • 1
  • 5
  • You just have to sort the numbers in ascending order and then just reverse it. That way you'll get the largest number that can be formed with the given number. – bharath Mar 18 '18 at 02:48
  • 1
    b.sort(key=lambda x: int(x), reverse=True) – syntax Mar 18 '18 at 03:06

3 Answers3

1

Strings are iterable so you can pass them into sorted like so:

def largest_number(num):
    num = str(num)
    ordered = ''.join(sorted(num, reverse=True))
    return int(ordered)

largest_number(87491)
>> 98741

sorted returns a list, so sorted('87491', reverse=True) will return:

['9', '8', '7', '4', '1']

Passing the list into join(link to explanation of the join method) will return:

'98741'
foxyblue
  • 2,859
  • 2
  • 21
  • 29
1

As other answers noted, this is a sorting problem. You could use the built in sorting function, but that can take a (relatively) long time, and if you are just starting out, it can be good to write your own implementations rather than rely on library magic.

Because we are only sorting integers between 0 and 9, we can use a faster sorting algorithm than the timsort that python's sorted uses.

What we do is count the number of occurrences of each digit. We then construct a number with as many 9s as the we found in the original, then 8s... until 0.

def Decending_Order(num):
    arr = [0,0,0,0,0,0,0,0,0,0]
    res = []
    for i in str(num):
        arr[int(i)] += 1
    for i in range(0,9):
        while arr[i] > 0:
            res.append(str(i))
            arr[i] -= 1
    return res.reverse()
  • Have you timed your function and mine? – foxyblue Mar 18 '18 at 03:18
  • Also python is a high level language so it's probably better to promote library magic. – foxyblue Mar 18 '18 at 03:23
  • 1
    Python is a high level language, but we should still be mindful of how our programs scale. Timsort runs in O(n log n) worst case and average case, while counting sort runs in O(n) worst and average. I timed the two functions and the built in sort ran faster for inputs up to size 100000. But the built in sort likely relies on real, fast arrays rather than lists, etc. – Joseph Espy Mar 18 '18 at 04:05
  • 1
    I guess my point is that as someone learns to code, it is more important to learn how to approach a problem correctly and construct a good solution than it is to know how to call the libraries. – Joseph Espy Mar 18 '18 at 04:09
-1

A nice one-liner (Basically the same as above: turns into a string, reverse sorts it, back into int):

def largest_num (num): return int (sorted (str (num), reverse = True))
Levi Lesches
  • 1,508
  • 1
  • 13
  • 23
  • As the OP mentions they are a novice (at least to python) providing a one-liner will not teach them good code writing etiquette, and will make the code unclear to them, especially not without any sort of explanation – tox123 Mar 18 '18 at 03:01
  • 1
    @LeviLesches I recommend you read some [python conventions](https://www.python.org/dev/peps/pep-0008/) :) – foxyblue Mar 18 '18 at 03:08
  • @GiantsLoveDeathMetal yah I'm not known for readability; My motto is: "EVERYTHING ON ONE LINE!" – Levi Lesches Mar 18 '18 at 03:11