Annika had some interesting extra-credit math problems for homework tonight:

Given ABC+ABC+ABC=BBB, find which values [0-9] must A,B,C have to solve the problem. Value must be unique to each letter (ie A not equal to B, B not equal to C, and C not equal to A

This is what I really love about Python – 20min or so later with some rough hackery (didn’t even import numpy), we had a solution to that and the rest of the problems:

XYZ*Z=WYZ

and MN-DM = NM

My kids learn some maths, looping, and type casting and that Python really does quickly solve any problem you throw at it. Maybe I’ll teach them some recursion tomorrow…

And now the unoptimized code (yes, it was a quick hack):

```
# Solve problems where characters can have a value of 0-9, but no characters can have same value
# First do a loop
for a in range(0,9):
# then b loop
for b in range (0,9):
# then c loop
for c in range (0,9):
# a,b,c cannot be the same value
if not (a==b or b==c or c==a):
# check if adding abc three times = bbb
abc = int(str(a) + str(b) + str(c))
bbb = int(str(b) + str(b) + str(b))
if (abc * 3) == bbb and (a != b) and (b != c) and (c != a):
print("We solved abc + abc + abc = bbb. The answer is a = {}, b = {}, c = {}".format(a,b,c))
print("-"*80)
# First do w loop
for w in range(0,9):
# then x loop
for x in range(0,9):
# then y loop
for y in range(0,9):
# then z loop
for z in range(0,9):
# w,x,y,z cannot be same value
if not((w==x or w==y or w==z) or (x==y or x==z) or (y==z)):
# Check if xyz * z = wyz
xyz = int(str(x) + str(y) + str(z))
wyz = int(str(w) + str(y) + str(z))
if (xyz * z == wyz):
print("We solved xyz * z = wyz. The answer is w = {}, x = {}, y = {}, z = {}".format(w, x, y, z))
print("-"*80)
# First do m loop
for m in range(0,9):
# then n loop
for n in range(0,9):
# then d loop
for d in range(0,9):
# m,n,d cannot be same value
if not (m==n or n==d or m==d):
# Check if mn - dm = nm
mn = int(str(m) + str(n))
dm = int(str(d) + str(m))
nm = int(str(n) + str(m))
if (mn - dm == nm):
print("We solved mn - dm = nm. The answer is m = {}, n = {}, d = {}".format(m,n,d))
```

And finally the solution output:

```
We solved abc + abc + abc = bbb. The answer is a = 1, b = 4, c = 8
--------------------------------------------------------------------------------
We solved xyz * z = wyz. The answer is w = 1, x = 0, y = 2, z = 5
We solved xyz * z = wyz. The answer is w = 3, x = 0, y = 7, z = 5
We solved xyz * z = wyz. The answer is w = 6, x = 1, y = 2, z = 5
We solved xyz * z = wyz. The answer is w = 8, x = 1, y = 7, z = 5
--------------------------------------------------------------------------------
We solved mn - dm = nm. The answer is m = 5, n = 0, d = 4
We solved mn - dm = nm. The answer is m = 6, n = 2, d = 3
We solved mn - dm = nm. The answer is m = 7, n = 4, d = 2
We solved mn - dm = nm. The answer is m = 8, n = 6, d = 1
```