diff 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
line wrap: on
line diff
--- a/nono.py	Wed Mar 11 19:52:22 2020 +0000
+++ b/nono.py	Wed Mar 11 22:06:04 2020 +0000
@@ -14,7 +14,7 @@
 
 Red=''
 eRed=''
-RedFmt=Red+'%s '+eRed
+RedFmt=Red+'%s'+eRed
 
 def interleave(*args):
   for vals in zip(*args):
@@ -148,10 +148,10 @@
       if i<self.n and self[i].val is True:
         dprint('x4',i)
         return False
-      elif gapsFilled==0:
-        # We must have crossed at least one gap...
-        print("Shouldn't happen: no gap! me:%s i:%s j:%s rr:%s"%(self,i, j, rr),file=sys.stderr)
-        raise Exception
+#       elif gapsFilled==0:
+#         # We must have crossed at least one gap...
+#         print("Shouldn't happen: no gap! me:%s i:%s j:%s rr:%s"%(self,i, j, rr),file=sys.stderr)
+#         raise Exception
       # Victory!
       dprint('c6',r,j,i)
       for k in range(j,i):
@@ -180,13 +180,13 @@
     while f<self.marginN and self[f+1].val is True:
       f+=1
     l=(f-s)+1 # our length
+    print('%s:%s,%s,%s,%s:<%s>'%(str(self.__class__.__name__)[0],s,f,l,self.runs,self))
     c=self.runs.count(l)
     if c==0:
       # not big enough yet
       dprint('n0')
       return
-    j=self.runs.index(l)
-    if j==0:
+    if self.runs[0]==l:
       # is it safely left marginal, i.e. no blobs or big enough gaps before us?
       if self.marginal(range(self.margin0,s),l):
         changed=True
@@ -201,21 +201,21 @@
               dprint('n1')
               self.checkNew(f+2) # I think@@
         self.found0(f) # pull in the start margin at least to f+2
-      elif j==self.rn-1:
-        # is it safely _right_ marginal, i.e. no blobs or big enough gaps _after_ us?
-        if self.marginal(range(self.marginN-1,r,-1),l):
-          changed=True
-          # mark our margins
-          for i in range(self.marginN-1,f,-1):
-            if self[i].val is None:
-              self[i].setVal(False)
-          if s>self.margin0+1:
-            if self[s-1].val is None:
-              self[s-1].setVal(False)
-              if s>self.margin0+2 and self[s-2].val is True:
-                dprint('n2')
-                self.checkNew(s-2) # I think@@
-          self.foundN(s) # pull in the finish margin at least to s-2
+    if self.runs[-1]==l:
+      # is it safely _right_ marginal, i.e. no blobs or big enough gaps _after_ us?
+      if self.marginal(range(self.marginN-1,f,-1),l):
+        changed=True
+        # mark our margins
+        for i in range(self.marginN-1,f,-1):
+          if self[i].val is None:
+            self[i].setVal(False)
+        if s>self.margin0+1:
+          if self[s-1].val is None:
+            self[s-1].setVal(False)
+            if s>self.margin0+2 and self[s-2].val is True:
+              dprint('n2')
+              self.checkNew(s-2) # I think@@
+        self.foundN(s) # pull in the finish margin at least to s-2
     if changed:
       self.resetAllRuns()
 
@@ -234,7 +234,7 @@
     return True
 
   def found0(self,i):
-    dprint('found0 called on %s at %s'%(self,i))
+    print('found0 called on %s at %s'%(self,i))
     i=self.margin0
     while self[i].val is False:
       i+=1
@@ -245,7 +245,16 @@
       self.updateHeader(r=r)
 
   def foundN(self,i):
-    print('foundN called on %s at %s'%(self,i),file=sys.stderr)
+    print('foundN called on %s at %s'%(self,i))
+    i=self.marginN
+    while self[i].val is False:
+      i-=1
+    if self[i].val is True:
+      r=self.runs.pop()
+      self.finalComplete=[r]+self.finalComplete
+      self.marginN-=r+1
+      self.updateHeader(r=r)
+    
 
 class Row(Vector):
   def __init__(self,n,m,runs,pos):
@@ -257,17 +266,22 @@
     return ((self.fmt%(' '.join(str(r) for r in self.runs)))+
             Vector.__str__(self))
 
-  def updateHeader(self,maxWidth=None,r=None):
+  def updateHeader(self,maxWidth=None,r=None,pre=None):
     if maxWidth is None:
       # update
-      self.infix+=RedFmt%r
-      self.fmt="%s%s%%s|"%(self.prespace,self.infix)
+      if pre:
+        self.infix+=(RedFmt%r)+" "
+      else:
+        # post
+        self.suffix=" "+RedFmt%r+self.suffix
+      self.fmt="%s%s%%s%s|"%(self.prespace,self.infix,self.suffix)
     else:
       # init
       self.maxWidth=maxWidth
       self.prespace=' '*(maxWidth-self.width)
       self.fmt="%s%%s|"%self.prespace
       self.infix=""
+      self.suffix=""
 
   def newBlob(self,x,newSeq):
     self[x].setVal(True)
@@ -335,17 +349,17 @@
     self.rc=runsPerRow
     self.cc=runsPerCol
     # print col nums>9 vertically :-(
-    self.columns=cc=[Column(n,self,runsPerCol[i],i) for i in range(20)]
+    self.columns=cc=[Column(n,self,runsPerCol[i],i) for i in range(n)]
     self.maxCRheight=maxCRheight=max(col.height for col in cc)
     for c in cc:
       c.updateHeader(maxCRheight)
-    self.rows=rr=[Row(n,self,runsPerRow[i],i) for i in range(20)]
+    self.rows=rr=[Row(n,self,runsPerRow[i],i) for i in range(n)]
     maxRRwidth=max(row.width for row in rr)
     for r in rr:
       r.updateHeader(maxRRwidth)
     self.rowfmt="%s|%%s|"%(' '*maxRRwidth)
-    for x in range(20):
-      for y in range(20):
+    for x in range(n):
+      for y in range(n):
         self[(x,y)]=Cell(rr[y],y,cc[x],x)
 
   def __str__(self):
@@ -355,7 +369,7 @@
     return "\n".join(lines)
 
 def dprint(*args):
-  pass #print(*args)
+  print(*args)
 
 if __name__ == '__main__':
   if len(sys.argv)>1: