Files
2023-11-07 01:27:43 +02:00

71 lines
1.5 KiB
Python

abc = ["A", "B", "C"]
def create(n):
print()
list = []
create_rec(0, n, list)
return remove_duplicates(list)
def remove_duplicates(list):
no_dups_list = []
for x in list:
no_dups = True
if x.find("AA") != -1:
no_dups = False
elif x.find("BB") != -1:
no_dups = False
elif x.find("CC") != -1:
no_dups = False
if no_dups:
no_dups_list.append(x)
return sorted(no_dups_list)
def create_rec(k, n, list):
if k == n:
return
elif k == 0:
for x in range(3**n):
if x < (1 / 3) * 3**n:
list.append("A")
elif x < (2 / 3) * 3**n:
list.append("B")
else:
list.append("C")
create_rec(1, n, list)
else:
repeating = 3**n / (3 ** (k + 1))
round = 0
i = repeating
# print("repeat amount:", repeating)
for x in range(len(list)):
if round % 3 == 0:
char = "A"
elif round % 3 == 1:
char = "B"
else:
char = "C"
if i % repeating == 0:
round += 1
# print("round:", round, "- i", i, "--- char:", char)
i += 1
list[x] += char
create_rec(k + 1, n, list)
if __name__ == "__main__":
print(create(1)) # [A,B,C]
print(create(2)) # [AB,AC,BA,BC,CA,CB]
print(len(create(5))) # 48