﻿ Python - Next number containing only distinct digits

# Python Exercises: Next number containing only distinct digits

## Python Math: Exercise-91 with Solution

Write a Python program that accepts an integer number with distinct digits and display 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: ## 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):
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: ## 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: 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.

﻿

## Python: Tips of the Day

Enum Class:

Here is another classy approach from Python, no pun intended. Python's enum class will let you create enum object members with constant and unique values which then can be listed, compared or used for identification.

Enum classes are also iterable so they can be iterated as well:

```from enum import Enum
class sports(Enum):
volleyball = 1
soccer = 2
running = 3
football = 4
baseball = 5
boxing = 6
print(sports.baseball)
print(repr(sports.baseball))
```

Output:

```sports.baseball
<sports.baseball: 5>```