Skip to content

Instantly share code, notes, and snippets.

@xudifsd
Last active November 22, 2020 06:54
Show Gist options
  • Save xudifsd/d1b7d92ce999a63e8164f0f6446247e1 to your computer and use it in GitHub Desktop.
Save xudifsd/d1b7d92ce999a63e8164f0f6446247e1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Sat 21 Nov 2020 10:12:23 PM PST
def permutation(lst):
assert len(lst) > 0
result = []
def helper():
if len(lst) == 0:
yield result[:]
return
for i in range(len(lst)):
val = lst.pop(i)
result.append(val)
yield from helper()
result.pop()
lst.insert(i, val)
yield from helper()
def check(lst):
num = 0
for i in range(len(lst)):
num = num * 10 + lst[i]
return num % (len(lst)) == 0
def merge(lst1, lst2):
assert len(lst1) == len(lst2)
result = []
for i in range(len(lst1)):
result.append(lst1[i])
result.append(lst2[i])
return result
def calculate():
for evens in permutation([0, 2, 4, 6, 8]):
for odds in permutation([1, 3, 5, 7, 9]):
fail = False
lst = merge(odds, evens)
for i in range(1, len(lst) + 1):
if not check(lst[:i]):
fail = True
break
if not fail:
print("".join(map(str, lst)))
if __name__ == '__main__':
calculate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment