# Python Exercises: Rearrange the digits of a number

## Python Math: Exercise-93 with Solution

Write a Python program to that takes an integer and rearrange 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

**Getting sorted iterable (can sort by "compare" function):**

>>> a = [1, 2, -3] >>> sorted(a) [-3, 1, 2] >>> sorted(a,key=abs) [1, 2, -3]

**Exercises: Weekly Top 16 Most Popular Topics**- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation