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:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
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:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
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.
Python: Tips of the Day
Given each iterable to construct a tuple by adding an index:
>>> a = ['Hello', 'world', '!'] >>> list(enumerate(a)) [(0, 'Hello'), (1, 'world'), (2, '!')]
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook