Let’s say the number is 123.
Then the statement:
[int(''.join(x)) for x in permutations(list(str(123)))]
What it is doing¶
The part, permutations(list(str('123' is creating a permutated tuples’ list of splitted string ‘123’.
And the int(''.join(x)) is converting each tuple to Integer.
However, of course, you need to import permutations from itertools.
so, the generalized version would be:
from itertools import permutations
[int(''.join(x)) for x in list(permutations(list(str(n))))]
Python is fun. Isn’t it?
WARNING
Although powerful & “fun looking”, try not to use it in some production environment or coding interview unless the length of the input string is extremely low, like <= 10. Over 12, you might crash the runtime, or get an timedOut error.
Because, the time-complexity of this function is O(N!), meaning if N=10, it gonna generate more than 3.6 million items. However, calling the permutations function will return a generator object, not a list of all possible pemutations.
