mirror of
https://github.com/iritmaximus/tira.git
synced 2026-10-12 19:13:47 +03:00
34 lines
930 B
Python
34 lines
930 B
Python
import itertools
|
|
|
|
def create_words(word=''):
|
|
perms = set()
|
|
for permutation in itertools.permutations(word):
|
|
prev = ""
|
|
correct = True
|
|
for char in permutation:
|
|
if char == prev:
|
|
correct = False
|
|
break
|
|
prev = char
|
|
|
|
if correct:
|
|
perms.add("".join(permutation))
|
|
|
|
return sorted(list(perms))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(create_words("abc")) # ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
|
|
print(create_words("aab")) # ['aba']
|
|
print(create_words("aaab")) # []
|
|
|
|
print(create_words("kala"))
|
|
# ['akal', 'akla', 'alak', 'alka', 'kala', 'laka']
|
|
|
|
print(create_words("syksy"))
|
|
# ['ksysy', 'kysys', 'skysy', 'syksy', 'sykys', 'sysky',
|
|
# 'sysyk', 'yksys', 'ysksy', 'yskys', 'ysyks', 'ysysk']
|
|
|
|
print(len(create_words("aybabtu"))) # 660
|
|
print(len(create_words("abcdefgh"))) # 40320
|