Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Blog
Views: 67
Author: Vincent J. Matsko Date: 20 September 2015, Day005 Post: Cryptarithms ︠38db8a7a-5318-4dc9-b5f8-92bd81e350af︠ # Remember to use shift+enter to evaluate. # Also, remember to copy to one of your own projects if you would like to change anything. perm9 = list(Permutations([1,2,3,4,5,6,7,8,9])); # There are 9! = 362,880 different permutations to consider. We can access each one individually. perm9[123]
[1, 2, 3, 5, 4, 6, 8, 9, 7]
def test(perm): # Multiple assignment is done simultaneously. [c,o,l,m,a,t,h,b,g] = perm; # Checking the arithmetic. if (1000*c + 110*o + l + 1000*m + 100*a + 10*t + h) == (1000*b + 100*l + 10*o + g): print c, o, o, l, " ", m, a, t, h, " ", b, l, o, g;
# We use a construct called "list comprehension" here. results = [test(p) for p in perm9];
1 6 6 4 3 7 9 8 5 4 6 2 1 7 7 4 3 6 9 8 5 4 7 2 2 5 5 4 3 8 9 7 6 4 5 1 2 8 8 4 3 5 9 7 6 4 8 1 3 5 5 4 2 8 9 7 6 4 5 1 3 6 6 4 1 7 9 8 5 4 6 2 3 7 7 4 1 6 9 8 5 4 7 2 3 8 8 4 2 5 9 7 6 4 8 1