Python Exercises: Next number containing only distinct digits
91. Next Distinct-Digit Number Finder
Write a Python program that accepts an integer number with distinct digits and displays the next number containing only distinct digits.
Sample Data:
12345) -> 12346
(99999) -> 102345
(100) -> 102
Sample Solution-1:
Python Code:
def test(n):
n += 1
if len(set(str(n))) == len(str(n)):
return n
return test(n)
n = 12345
print("Original number:", n)
print("Next number containing only distinct digits!")
print(test(n))
n = 99999
print("Original number:", n)
print("Next number containing only distinct digits!")
print(test(n))
n = 1
print("Original number:", n)
print("Next number containing only distinct digits!")
print(test(n))
n = 100
print("Original number:", n)
print("Next number containing only distinct digits!")
print(test(n))
Sample Output:
Original number: 12345 Next number containing only distinct digits! 12346 Original number: 99999 Next number containing only distinct digits!
Flowchart:

Sample Solution-2:
Python Code:
def test(n):
while True:
n += 1
x = sorted(str(n))
temp = sorted(set(str(n)))
if x == temp:
return n
n = 12345
print("Original number:", n)
print("Next number containing only distinct digits!")
print(test(n))
n = 99999
print("Original number:", n)
print("Next number containing only distinct digits!")
print(test(n))
n = 1
print("Original number:", n)
print("Next number containing only distinct digits!")
print(test(n))
n = 100
print("Original number:", n)
print("Next number containing only distinct digits!")
print(test(n))
Sample Output:
Original number: 12345 Next number containing only distinct digits! 12346 Original number: 99999 Next number containing only distinct digits! 102345 Original number: 1 Next number containing only distinct digits! 2 Original number: 100 Next number containing only distinct digits! 102
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to accept an integer with distinct digits and compute the next number that contains only distinct digits, then print the result.
- Write a Python function that, given a number, iteratively increments it until a number with all distinct digits is found, and returns that number.
- Write a Python script to test the function with several numbers and print the next distinct-digit number for each input.
- Write a Python program to prompt the user for an integer with distinct digits and output the next number with distinct digits, with error handling.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous Python Exercise: Check if a number is a Harshad number or not.
Next Python Exercise: Absolute difference between two consecutive digits.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.