comparison nono.py @ 9:28a7b0e992cb

lots of fails in trace of 5a wrt checkNew/found0/foundN???
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Wed, 11 Mar 2020 22:06:04 +0000
parents 3276cf6ee6df
children 0a24625b33fa
comparison
equal deleted inserted replaced
8:3276cf6ee6df 9:28a7b0e992cb
12 12
13 import sys 13 import sys
14 14
15 Red='' 15 Red=''
16 eRed='' 16 eRed=''
17 RedFmt=Red+'%s '+eRed 17 RedFmt=Red+'%s'+eRed
18 18
19 def interleave(*args): 19 def interleave(*args):
20 for vals in zip(*args): 20 for vals in zip(*args):
21 yield from vals 21 yield from vals
22 22
146 # Maybe a win? 146 # Maybe a win?
147 # look ahead, can we stop here? 147 # look ahead, can we stop here?
148 if i<self.n and self[i].val is True: 148 if i<self.n and self[i].val is True:
149 dprint('x4',i) 149 dprint('x4',i)
150 return False 150 return False
151 elif gapsFilled==0: 151 # elif gapsFilled==0:
152 # We must have crossed at least one gap... 152 # # We must have crossed at least one gap...
153 print("Shouldn't happen: no gap! me:%s i:%s j:%s rr:%s"%(self,i, j, rr),file=sys.stderr) 153 # print("Shouldn't happen: no gap! me:%s i:%s j:%s rr:%s"%(self,i, j, rr),file=sys.stderr)
154 raise Exception 154 # raise Exception
155 # Victory! 155 # Victory!
156 dprint('c6',r,j,i) 156 dprint('c6',r,j,i)
157 for k in range(j,i): 157 for k in range(j,i):
158 maybe[k]+=1 158 maybe[k]+=1
159 req-=r 159 req-=r
178 s-=1 178 s-=1
179 f=pos # finish index of our run 179 f=pos # finish index of our run
180 while f<self.marginN and self[f+1].val is True: 180 while f<self.marginN and self[f+1].val is True:
181 f+=1 181 f+=1
182 l=(f-s)+1 # our length 182 l=(f-s)+1 # our length
183 print('%s:%s,%s,%s,%s:<%s>'%(str(self.__class__.__name__)[0],s,f,l,self.runs,self))
183 c=self.runs.count(l) 184 c=self.runs.count(l)
184 if c==0: 185 if c==0:
185 # not big enough yet 186 # not big enough yet
186 dprint('n0') 187 dprint('n0')
187 return 188 return
188 j=self.runs.index(l) 189 if self.runs[0]==l:
189 if j==0:
190 # is it safely left marginal, i.e. no blobs or big enough gaps before us? 190 # is it safely left marginal, i.e. no blobs or big enough gaps before us?
191 if self.marginal(range(self.margin0,s),l): 191 if self.marginal(range(self.margin0,s),l):
192 changed=True 192 changed=True
193 # mark our margins 193 # mark our margins
194 for i in range(self.margin0+1,s): 194 for i in range(self.margin0+1,s):
199 self[f+1].setVal(False) 199 self[f+1].setVal(False)
200 if f<self.marginN-2 and self[f+2].val is True: 200 if f<self.marginN-2 and self[f+2].val is True:
201 dprint('n1') 201 dprint('n1')
202 self.checkNew(f+2) # I think@@ 202 self.checkNew(f+2) # I think@@
203 self.found0(f) # pull in the start margin at least to f+2 203 self.found0(f) # pull in the start margin at least to f+2
204 elif j==self.rn-1: 204 if self.runs[-1]==l:
205 # is it safely _right_ marginal, i.e. no blobs or big enough gaps _after_ us? 205 # is it safely _right_ marginal, i.e. no blobs or big enough gaps _after_ us?
206 if self.marginal(range(self.marginN-1,r,-1),l): 206 if self.marginal(range(self.marginN-1,f,-1),l):
207 changed=True 207 changed=True
208 # mark our margins 208 # mark our margins
209 for i in range(self.marginN-1,f,-1): 209 for i in range(self.marginN-1,f,-1):
210 if self[i].val is None: 210 if self[i].val is None:
211 self[i].setVal(False) 211 self[i].setVal(False)
212 if s>self.margin0+1: 212 if s>self.margin0+1:
213 if self[s-1].val is None: 213 if self[s-1].val is None:
214 self[s-1].setVal(False) 214 self[s-1].setVal(False)
215 if s>self.margin0+2 and self[s-2].val is True: 215 if s>self.margin0+2 and self[s-2].val is True:
216 dprint('n2') 216 dprint('n2')
217 self.checkNew(s-2) # I think@@ 217 self.checkNew(s-2) # I think@@
218 self.foundN(s) # pull in the finish margin at least to s-2 218 self.foundN(s) # pull in the finish margin at least to s-2
219 if changed: 219 if changed:
220 self.resetAllRuns() 220 self.resetAllRuns()
221 221
222 def marginal(self,rng,l): 222 def marginal(self,rng,l):
223 g=0 # length of a gap 223 g=0 # length of a gap
232 if g==l: 232 if g==l:
233 return False 233 return False
234 return True 234 return True
235 235
236 def found0(self,i): 236 def found0(self,i):
237 dprint('found0 called on %s at %s'%(self,i)) 237 print('found0 called on %s at %s'%(self,i))
238 i=self.margin0 238 i=self.margin0
239 while self[i].val is False: 239 while self[i].val is False:
240 i+=1 240 i+=1
241 if self[i].val is True: 241 if self[i].val is True:
242 r=self.runs.pop(0) 242 r=self.runs.pop(0)
243 self.initialComplete.append(r) 243 self.initialComplete.append(r)
244 self.margin0+=r+1 244 self.margin0+=r+1
245 self.updateHeader(r=r) 245 self.updateHeader(r=r)
246 246
247 def foundN(self,i): 247 def foundN(self,i):
248 print('foundN called on %s at %s'%(self,i),file=sys.stderr) 248 print('foundN called on %s at %s'%(self,i))
249 i=self.marginN
250 while self[i].val is False:
251 i-=1
252 if self[i].val is True:
253 r=self.runs.pop()
254 self.finalComplete=[r]+self.finalComplete
255 self.marginN-=r+1
256 self.updateHeader(r=r)
257
249 258
250 class Row(Vector): 259 class Row(Vector):
251 def __init__(self,n,m,runs,pos): 260 def __init__(self,n,m,runs,pos):
252 Vector.__init__(self,n,m,runs) 261 Vector.__init__(self,n,m,runs)
253 self.y=pos 262 self.y=pos
255 264
256 def __str__(self): 265 def __str__(self):
257 return ((self.fmt%(' '.join(str(r) for r in self.runs)))+ 266 return ((self.fmt%(' '.join(str(r) for r in self.runs)))+
258 Vector.__str__(self)) 267 Vector.__str__(self))
259 268
260 def updateHeader(self,maxWidth=None,r=None): 269 def updateHeader(self,maxWidth=None,r=None,pre=None):
261 if maxWidth is None: 270 if maxWidth is None:
262 # update 271 # update
263 self.infix+=RedFmt%r 272 if pre:
264 self.fmt="%s%s%%s|"%(self.prespace,self.infix) 273 self.infix+=(RedFmt%r)+" "
274 else:
275 # post
276 self.suffix=" "+RedFmt%r+self.suffix
277 self.fmt="%s%s%%s%s|"%(self.prespace,self.infix,self.suffix)
265 else: 278 else:
266 # init 279 # init
267 self.maxWidth=maxWidth 280 self.maxWidth=maxWidth
268 self.prespace=' '*(maxWidth-self.width) 281 self.prespace=' '*(maxWidth-self.width)
269 self.fmt="%s%%s|"%self.prespace 282 self.fmt="%s%%s|"%self.prespace
270 self.infix="" 283 self.infix=""
284 self.suffix=""
271 285
272 def newBlob(self,x,newSeq): 286 def newBlob(self,x,newSeq):
273 self[x].setVal(True) 287 self[x].setVal(True)
274 self[x].column.checkNew(self.y) 288 self[x].column.checkNew(self.y)
275 if newSeq: 289 if newSeq:
333 print("losing r:%s x c:%s"%(len(runsPerRow),n),sys.stderr) 347 print("losing r:%s x c:%s"%(len(runsPerRow),n),sys.stderr)
334 exit(1) 348 exit(1)
335 self.rc=runsPerRow 349 self.rc=runsPerRow
336 self.cc=runsPerCol 350 self.cc=runsPerCol
337 # print col nums>9 vertically :-( 351 # print col nums>9 vertically :-(
338 self.columns=cc=[Column(n,self,runsPerCol[i],i) for i in range(20)] 352 self.columns=cc=[Column(n,self,runsPerCol[i],i) for i in range(n)]
339 self.maxCRheight=maxCRheight=max(col.height for col in cc) 353 self.maxCRheight=maxCRheight=max(col.height for col in cc)
340 for c in cc: 354 for c in cc:
341 c.updateHeader(maxCRheight) 355 c.updateHeader(maxCRheight)
342 self.rows=rr=[Row(n,self,runsPerRow[i],i) for i in range(20)] 356 self.rows=rr=[Row(n,self,runsPerRow[i],i) for i in range(n)]
343 maxRRwidth=max(row.width for row in rr) 357 maxRRwidth=max(row.width for row in rr)
344 for r in rr: 358 for r in rr:
345 r.updateHeader(maxRRwidth) 359 r.updateHeader(maxRRwidth)
346 self.rowfmt="%s|%%s|"%(' '*maxRRwidth) 360 self.rowfmt="%s|%%s|"%(' '*maxRRwidth)
347 for x in range(20): 361 for x in range(n):
348 for y in range(20): 362 for y in range(n):
349 self[(x,y)]=Cell(rr[y],y,cc[x],x) 363 self[(x,y)]=Cell(rr[y],y,cc[x],x)
350 364
351 def __str__(self): 365 def __str__(self):
352 lines=[self.rowfmt%('|'.join([(self.columns[i]).header[j] for i in range(self.n)])) # 'rotate' 366 lines=[self.rowfmt%('|'.join([(self.columns[i]).header[j] for i in range(self.n)])) # 'rotate'
353 for j in range(self.maxCRheight)] 367 for j in range(self.maxCRheight)]
354 lines+=[str(r) for r in self.rows] 368 lines+=[str(r) for r in self.rows]
355 return "\n".join(lines) 369 return "\n".join(lines)
356 370
357 def dprint(*args): 371 def dprint(*args):
358 pass #print(*args) 372 print(*args)
359 373
360 if __name__ == '__main__': 374 if __name__ == '__main__':
361 if len(sys.argv)>1: 375 if len(sys.argv)>1:
362 f=open(sys.argv[1]) 376 f=open(sys.argv[1])
363 else: 377 else: