w3resource

Python Exercises: Rearrange the digits of a number

Python Math: Exercise-93 with Solution

Write a Python program that takes an integer and rearranges the digits to create two maximum and minimum numbers.

Sample Solution-1:

Python Code:

def test(n):
    temp = []
    for e in str(n):
        temp.append(e)
        max_num = "".join(sorted(temp)[::-1])
        min_num = "".join(sorted(temp))        
    return int(max_num), int(min_num)
                                      
n = 1254
print("Original number:", n)
print("Rearrange the digits of the said number to get Maximum and Minimum numbers:")
print("Maximum and Minimum Numbers:",test(n))
n = 6
print("\nOriginal number:", n)
print("Rearrange the digits of the said number to get Maximum and Minimum numbers:")
print("Maximum and Minimum Numbers:",test(n))
n = 1000
print("\nOriginal number:", n)
print("Rearrange the digits of the said number to get Maximum and Minimum numbers:")
print("Maximum and Minimum Numbers:",test(n))

Sample Output:

Original number: 1254
Rearrange the digits of the said number to get Maximum and Minimum numbers:
Maximum and Minimum Numbers: (5421, 1245)

Original number: 6
Rearrange the digits of the said number to get Maximum and Minimum numbers:
Maximum and Minimum Numbers: (6, 6)

Original number: 1000
Rearrange the digits of the said number to get Maximum and Minimum numbers:
Maximum and Minimum Numbers: (1000, 1)

Flowchart:

Flowchart: Rearrange the digits of a number.

Sample Solution-2:

Python Code:

def test(n):
    temp = ''.join(sorted(str(n)))
    return int(temp[::-1]), int(temp) 
                                      
n = 1254
print("Original number:", n)
print("Rearrange the digits of the said number to get Maximum and Minimum Numbers:")
print("Maximum and Minimum Numbers:",test(n))
n = 6
print("\nOriginal number:", n)
print("Rearrange the digits of the said number to get Maximum and Minimum Numbers:")
print("Maximum and Minimum Numbers:",test(n))
n = 1000
print("\nOriginal number:", n)
print("Rearrange the digits of the said number to get Maximum and Minimum Numbers:")
print("Maximum and Minimum Numbers:",test(n))

Sample Output:

Original number: 1254
Rearrange the digits of the said number to get Maximum and Minimum Numbers:
Maximum and Minimum Numbers: (5421, 1245)

Original number: 6
Rearrange the digits of the said number to get Maximum and Minimum Numbers:
Maximum and Minimum Numbers: (6, 6)

Original number: 1000
Rearrange the digits of the said number to get Maximum and Minimum Numbers:
Maximum and Minimum Numbers: (1000, 1)

Flowchart:

Flowchart: Rearrange the digits of a number.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous Python Exercise: Absolute difference between two consecutive digits.
Next Python Exercise: Sum of all prime numbers in a list of integers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/math/python-math-exercise-93.php