# mirror.py

a guest
Aug 12th, 2014
354
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class OffsetMirrorSet(object):
2.     """
3.     A class to compute the grammar description of mirrors.
4.
5.     c.f. http://puzzling.stackexchange.com/a/2059/1779
6.     """
7.     def __init__(self, f, u, v):
8.         terminal = []
9.         non_terminal = []
10.         #First we look for 1 digit terminals
11.         for i in xrange(10):
12.             if i * f + v - u * 10 == i:
13.                 terminal.append(str(i))
14.
15.         #If it's not a 1 digit number then it has at least 2:
16.         for a0 in xrange(10):
17.             for an in xrange(10):
18.                 right = a0 * f + v - an
19.                 left = u *10 + a0 - an * f
20.                 if right % 10 == 0 and left >= 0 and left < f:
21.                     new_u = left
22.                     new_v = right / 10
23.
24.                     #can it be a 2 digit terminal ?
25.                     if new_u == new_v:
26.                         terminal.append(str(an) + str(a0))
27.
28.                     non_terminal.append( (str(an), (new_u, new_v), str(a0)) )
29.
30.         self.info = (f, u, v)
31.         self.terminal = terminal
32.         self.non_terminal = non_terminal
33.
34.     def __repr__(self):
35.         f = self.info[0]
36.         return (("S_%d(%d, %d) = " %self.info)
37.             + " + ".join(self.terminal) + " + "
38.             + " + ".join("%s.S_%d(%d, %d).%s" %(a,f,u,v,b) for (a,(u,v),b) in self.non_terminal))
RAW Paste Data