annotate nono.py @ 6:a56d5285575b

drafted Vector.checkNew, still need found0 and foundN
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Tue, 10 Mar 2020 19:56:07 +0000
parents fee51ab07d09
children 13f600be3a1b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/python3
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 # Expects e.g. ^A copy from Nonograms dprint preview cols, then blank line, then rows
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 # rows are space-separated
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4 # cols are one-digit-after-another, unless some 2-digit, in which case x is separator
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 # E.g.
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6 # 13x1x2
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 # 19
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 # maps to
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 # 13
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 # 1 1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 # 2 9
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 import sys
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 Red=''
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16 eRed=''
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17 RedFmt=Red+'%s'+eRed
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19 def interleave(*args):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
20 for vals in zip(*args):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
21 yield from vals
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
22
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
23 class Vector(list):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
24 # reads top-to-bottom or left-to-right
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
25 def __init__(self,n,m,runs):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
26 list.__init__(self,list(range(n)))
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
27 self.n=n
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
28 self.margin0=0 # a run can start here, so if >0 then self[m0-1].val must be False
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
29 self.marginN=n-1 # a run can end here, so if <n-1 then self[mN+1].val ditto
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
30 self.runs=self.origRuns=runs
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
31 self.initialComplete=[]
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
32 self.finalComplete=[]
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
33 self.resetAllRuns()
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
34
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
35 def __repr__(self):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
36 return "V@%s%s:%s"%(self.x,self.origRuns,list.__repr__(self))
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
37
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
38 def __str__(self):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
39 return '%s|'%('|'.join(str(c) for c in self))
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
40
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
41 def resetAllRuns(self):
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
42 # compute the set of all possible layouts for runs
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
43 self.rn=len(self.runs)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
44 rtot=sum(self.runs)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
45 self.allRuns=list(self.seedList(0,0,0,
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
46 sum(1+self.runs[k] for k in range(self.rn))-1))
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
47 self.nar=len(self.allRuns)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
48
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
49 def seedList(self,i,j,pos,runLen):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
50 """
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
51 :param i: starting skip before next run
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
52 :type i: 0 if pos==0 else 1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
53 :param j: next run number
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
54 :type j: int
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
55 :param pos: left margin
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
56 :type pos: int
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
57 """
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
58 bound=self.n-(pos+runLen)+1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
59 #dprint('s',i,j,pos,runLen,bound)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
60 if j==self.rn:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
61 yield []
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
62 return
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
63 r=self.runs[j]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
64 for v in range(i,bound):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
65 for sub in self.seedList(1,j+1,pos+v+r,runLen-(r+1)):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
66 yield [-v,r]+sub
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
67
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
68 def step(self):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
69 scratch=[0 if c.val is None else c.val for c in self]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
70 for k,runs in enumerate(self.allRuns):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71 dprint('=====pass %s======'%k)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
72 self.onepass(0,self.n,scratch,runs.copy())
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
73 dprint(scratch)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
74 for i in range(self.n):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75 if scratch[i]==self.nar:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
76 # If blobby in _every_ pass, then must be a blob
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
77 if self[i].val is None:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
78 self[i].setVal(True)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
79 elif self[i].val is True:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
80 # already there
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
81 pass
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
82 else:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
83 print("Shouldn't happen: attempt to blob where x already present! %s at %s"%(self,i),file=sys.stderr)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
84 exit(101)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
85
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
86 def onepass(self,i0,iBound,scratch,stack):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
87 """note that stack is not a simple run, but one with _negative_ numbers between
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
88 and possibly before the positive ones, indicating obligatory skips
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
89 """
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
90 i=i0 # starting index into self/scratch/maybe
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
91 j=-1 # index into run
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
92 maybe=[0]*iBound
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
93 dprint('r: %s'%stack)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
94 req=sum((-r if r<0 else r) for r in stack)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
95 while stack and i<iBound:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
96 r=rr=stack.pop(0)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
97 dprint('pop:',r)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
98 if r<1:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
99 # obligatory skip
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
100 # (Above init of self.allRuns is easier if we allow a 0 to be ignored
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
101 i-=r
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
102 req+=r
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
103 r=rr=stack.pop(0)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
104 # rr is run remaining -- how many we still need
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
105 j+=1 # index of current run in self.runs, we'll need to decorate that eventually
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
106 inOne=-1 # if non-neg, records the start point of a possible run
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
107 gapsFilled=0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
108 # First, check if we can start here: 0 is OK, and n>0 iff n-1 is None or False
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
109 if i>0 and i<iBound:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
110 while self[i-1].val:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
111 i+=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
112 if (iBound-i)<req:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
113 # Can't win, give up altogether
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
114 dprint('c0',i,iBound,req)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
115 return
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
116 while i<iBound:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
117 c=self[i].val
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
118 dprint('top',i,c,inOne,rr)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
119 if c is None:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
120 # we could add a blob here
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
121 dprint('c1')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
122 gapsFilled+=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
123 rr-=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
124 if inOne<0:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
125 dprint('c1a',i)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
126 # starts here
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
127 inOne=i
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
128 # fall through to check for completion
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
129 else:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
130 dprint('c2')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
131 # c is a bool
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
132 if inOne<0:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
133 dprint('c2a')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
134 if c:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
135 dprint('c2a1')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
136 # a *, we can possible start something here
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
137 inOne=i
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
138 rr-=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
139 # fall through to check for completion
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
140 else:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
141 dprint('c2a2')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
142 # an x, can't start here, just move along
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
143 i+=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
144 continue
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
145 else:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
146 dprint('c2b')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
147 if c:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
148 dprint('c2b1')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
149 # a blob, extend or complete a partial
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
150 rr-=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
151 # fall through to check for completion
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
152 else:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
153 # abandon a partial
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
154 dprint('c2b2')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
155 inOne=-1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
156 rr=r
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
157 i+=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
158 continue
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
159 if rr>0:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
160 dprint('c3')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
161 # we're not done, carry on
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
162 i+=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
163 continue
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
164 # Maybe a win?
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
165 # look ahead, can we stop here?
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
166 # NB _self_.n
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
167 if i+1<self.n and self[i+1].val:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
168 dprint('c4')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
169 # Nope
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
170 inOne=-1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
171 rr=r
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
172 gapsFilled=0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
173 i+=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
174 continue
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
175 elif gapsFilled==0:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
176 dprint('c5')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
177 # We must have crossed at least on gap...
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
178 print("Shouldn't happen: no gap! me:%s i:%s j:%s rr:%s inOne:%s"%(self,i, j, rr, inOne),file=sys.stderr)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
179 exit(100)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
180 # Victory!
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
181 dprint('c6',r,inOne,i)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
182 for k in range(inOne,i+1):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
183 maybe[k]+=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
184 i+=1
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
185 req-=r
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
186 break
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
187 # on to the next run
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
188 # end of inner loop, did we win?
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
189 if (not stack) or i==iBound:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
190 # yes
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
191 dprint('win:',maybe)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
192 for k in range(iBound):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
193 scratch[k]+=maybe[k]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
194
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
195 def checkNew(self,pos):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
196 # New blob at pos, can we complete anything?
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
197 # Assuming @@ FTTB that it's never possible to definitively complete a
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
198 # non-marginal run, which is wrong if the length is unique and it's bounded...
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
199 changed=False
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
200 # First, find our boundaries:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
201 s=pos # start index of our run
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
202 while s>self.marginO and self[s-1].val is True:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
203 s-=1
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
204 f=pos # finish index of our run
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
205 while f<self.marginN and self[f+1].val is True:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
206 f+=1
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
207 l=(f-s)+1 # our length
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
208 c=self.runs.count(l)
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
209 if c==0:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
210 # not big enough yet
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
211 dprint('n0')
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
212 return
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
213 j=self.runs.index(l)
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
214 if j==0:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
215 # is it safely left marginal, i.e. no blobs or big enough gaps before us?
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
216 if marginal(range(self.margin0,s)):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
217 changed=True
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
218 # mark our margins
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
219 for i in range(margin0+1,s):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
220 if self[i].val is None:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
221 self[i].setVal(False)
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
222 if f<marginN-1:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
223 if self[f+1].val is None:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
224 self[f+1].setVal(False)
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
225 if f<marginN-2 and self[f+2].val is True:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
226 dprint('n1')
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
227 self.checkNew(f+2) # I think@@
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
228 self.found0(f) # pull in the start margin at least to f+2
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
229 elif j==self.rn-1:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
230 # is it safely _right_ marginal, i.e. no blobs or big enough gaps _after_ us?
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
231 if self.marginal(range(self.marginN-1,r,-1),l):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
232 changed=True
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
233 # mark our margins
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
234 for i in range(marginN-1,f,-1):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
235 if self[i].val is None:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
236 self[i].setVal(False)
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
237 if s>margin0+1:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
238 if self[s-1].val is None:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
239 self[s-1].setVal(False)
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
240 if s>margin0+2 and self[s-2].val is True:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
241 dprint('n2')
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
242 self.checkNew(s-2) # I think@@
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
243 self.foundN(s) # pull in the finish margin at least to s-2
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
244 if changed:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
245 self.resetAllRuns()
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
246
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
247 def marginal(self,rng,l):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
248 g=0 # length of a gap
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
249 for i in rng:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
250 if self[i].val is True:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
251 return False
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
252 if self[i].val is False:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
253 g=0
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
254 else:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
255 # None
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
256 g+=1
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
257 if g==l:
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
258 return False
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
259 return True
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
260
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
261 class Row(Vector):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
262 def __init__(self,n,m,runs,pos,dprintWidth):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
263 Vector.__init__(self,n,m,runs)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
264 self.y=pos
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
265 self.dprintWidth=dprintWidth
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
266 self.fmt="%%%ss|"%dprintWidth
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
267
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
268 def __str__(self):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
269 return ((self.fmt%(' '.join(str(r) for r in self.runs)))+
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
270 Vector.__str__(self))
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
271
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
272 class Column(Vector):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
273 def __init__(self,n,m,runs,pos,dprintHeight):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
274 Vector.__init__(self,n,m,runs)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
275 self.x=pos
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
276 self.dprintHeight=dprintHeight
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
277 self.fmt="%%%ss"%self.dprintHeight
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
278 self.updateHeader()
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
279
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
280 def updateHeader(self):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
281 header=('-'.join(str(c) for c in self.runs))
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
282 self.header=self.fmt%header # pad to same 'height'
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
283
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
284 class Cell:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
285 def __init__(self,row,y,column,x):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
286 # At the intersection of row and column Vectors
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
287 self.row=row
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
288 self.column=column
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
289 self.x=x
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
290 self.y=y
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
291 self.val=None # three valued: None(unknown), True(filled), False(empty)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
292 self.row[x]=self
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
293 self.column[y]=self
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
294
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
295 def __repr__(self):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
296 return "C@(%s,%s):%s"%(self.x,self.y,self.val)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
297
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
298 def __str__(self):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
299 return ' ' if self.val is None else ('\u25A0' if self.val else 'x')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
300
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
301 def setVal(self,v):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
302 if v is True:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
303 if self.val is False:
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
304 print("Warning: x -> * at %s,%s"%(self.x,self.y),file=sys.stderr)
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
305 elif self.val is True:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
306 # No-op
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
307 return
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
308 self.val=v
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
309 self.row.checkNew(self.x)
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
310 self.column.checkNew(self.y)
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
311 # @@ check row/col completed
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
312 else:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
313 if self.val is not None:
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
314 print("Warning: %s -> %s at %s,%s"%(self.val,v,self.x,self.y),file=sys.stderr)
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
315 self.val=v
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
316
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
317 class Nono(dict):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
318 # 0,0 is upper left, so increasing y goes _downwards_, to match the standard layout
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
319 def __init__(self,rows,cols):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
320 n=self.n=len(cols)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
321 if n!=len(rows):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
322 print("losing r:%s x c:%s"%(len(rows),n),sys.stderr)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
323 exit(1)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
324 self.rc=rows
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
325 rowDprintWidth=max(sum(len(str(r)) for r in row)+len(row)-1 for row in rows)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
326 self.rowfmt="%s|%%s"%(' '*rowDprintWidth)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
327 self.cc=cols
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
328 # dprint col nums>9 vertically :-(
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
329 self.colDprintHeight=max(sum(len(str(c)) for c in col)+len(col)-1 for col in cols)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
330 self.columns=cc=[Column(n,self,cols[i],i,self.colDprintHeight) for i in range(20)]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
331 self.rows=rr=[Row(n,self,rows[i],i,rowDprintWidth) for i in range(20)]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
332 for x in range(20):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
333 for y in range(20):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
334 self[(x,y)]=Cell(rr[y],y,cc[x],x)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
335
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
336 def __str__(self):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
337 lines=[self.rowfmt%('|'.join([(self.columns[i]).header[j] for i in range(self.n)])) # 'rotate'
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
338 for j in range(self.colDprintHeight)]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
339 lines+=[str(r) for r in self.rows]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
340 return "\n".join(lines)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
341
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
342 def dprint(*args):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
343 pass
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
344
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
345 if __name__ == '__main__':
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
346 if len(sys.argv)>1:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
347 f=open(sys.argv[1])
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
348 else:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
349 f=sys.stdin
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
350
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
351 cols=[]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
352
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
353 for l in f:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
354 l=l.rstrip()
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
355 if l=='':
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
356 break
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
357 if 'x' in l:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
358 vv=[int(s) for s in l.split('x')]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
359 else:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
360 vv=[int(c) for c in l]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
361 cols.append(vv)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
362
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
363 rows=[[int(s) for s in l.split()] for l in f]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
364
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
365 solver=Nono(rows,cols)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
366 print(solver)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
367 for c in solver.columns:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
368 c.step()
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
369 print()
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
370 print(solver)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
371 for r in solver.rows:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
372 r.step()
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
373 print()
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
374 print(solver)