Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hyper-neutrino/fbf7eed7f6b8bf5ee980740c2923434a to your computer and use it in GitHub Desktop.
Save hyper-neutrino/fbf7eed7f6b8bf5ee980740c2923434a to your computer and use it in GitHub Desktop.
def add(x, y):
ml = max(len(str(x)), len(str(y)))
x, y = str(x).zfill(ml + 1), str(y).zfill(ml + 1)
carries = 0
z = ""
carry = 0
for a, b in zip(map(int, x[::-1]), map(int, y[::-1])):
c = a + b + carry
if c >= 10:
carries += 1
carry = 1
else:
carry = 0
z = str(c % 10) + z
return (carries, int(z))
def verify(start, sums):
start = start[:]
carries = 0
for x, y in sums:
if x == y:
if start.count(x) < 2:
print(start, x, y)
return False
else:
if x not in start or y not in start:
print(start, x, y)
return False
c, t = add(x, y)
carries += c
start.remove(x)
start.remove(y)
start.append(t)
return carries
def summations(a):
if len(a) == 2: return [[(a[0], a[1])]]
sums = []
for i in range(len(a)):
x = a[i]
for j in range(i + 1, len(a)):
y = a[j]
for ss in summations(a[:i] + a[i + 1:j] + a[j + 1:] + [x + y]):
sums.append([(x, y)] + ss)
return sums
import random
a = [random.randint(1, 100) for _ in range(7)]
ss = summations(a)
t = set()
for s in summations(a):
v = verify(a, s)
t.add(v)
if v is False:
print(a, s)
print(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment