from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc def code(s): return ord(s)-ord("A") def codeR(n): return chr(ord("A")+n) @bootstrap def dfs(n): while G[n]: v, s = G[n].pop() yield dfs(v) ans.append(s) yield G = [[] for _ in range(26)] for i in range(26): for j in range(26): G[i].append((j, codeR(j)+codeR(i))) ans = [] dfs(0) for a in ans: print(a)