Write a Python program to that takes an integer and rearrange the digits to create two maximum and minimum numbers
This is a Python program that takes a number as input, converts it to a string, and then generates the minimum and maximum numbers that can be formed by rearranging its digits.
- The program first initializes a variable n to the value of 2014. The print() function is then used to display the original number n to the console.
- An empty list l is then created to store the digits of the number. A for loop is used to iterate over each character in the string representation of n, and the character is appended to the list l.
- The sorted() function is then used to sort the elements of the list l. The join() method is used to concatenate the elements of the list back into a single string, and the resulting string is assigned to the variable min_num. This gives us the minimum number that can be formed by rearranging the digits of the original number.
- The [::-1] slice notation is used to reverse the order of the sorted list before concatenating it into a string, and the resulting string is assigned to the variable max_num. This gives us the maximum number that can be formed by rearranging the digits of the original number.
- Finally, the print() function is used to display the minimum and maximum numbers to the console.
Source Code
n = 2014
print("Given Numbers:", n)
l = []
for e in str(n):
l.append(e)
max_num = "".join(sorted(l)[::-1])
min_num = "".join(sorted(l))
print("Minimum Numbers : ",min_num)
print("Maximum Numbers : ",max_num)
Output
Given Numbers: 2014
Minimum Numbers : 0124
Maximum Numbers : 4210