Module 14: Find Spans in the Grid (Python code)
This commit is contained in:
parent
0f7020f74c
commit
d9795f1ea1
84
a.py
84
a.py
@ -1,3 +1,5 @@
|
||||
from copy import copy
|
||||
|
||||
def ToUpper(s):
|
||||
return s.upper()
|
||||
|
||||
@ -14,7 +16,10 @@ class Point:
|
||||
|
||||
## ----------------------------------------------------------------------------
|
||||
##-Span
|
||||
class Span:
|
||||
class Spans:
|
||||
|
||||
spans = []
|
||||
|
||||
def __init__(self, p, l, v):
|
||||
self.point = p
|
||||
self.len = l
|
||||
@ -103,6 +108,7 @@ class Grid:
|
||||
def __init__(self, n):
|
||||
self.name = n
|
||||
self.lines = []
|
||||
self.sp = Spans.spans
|
||||
|
||||
def rows(self):
|
||||
return len(self.lines)
|
||||
@ -113,6 +119,63 @@ class Grid:
|
||||
else:
|
||||
return len(self.lines[0])
|
||||
|
||||
# Returns character value of the box at point 'p'
|
||||
# 'p' must be in bounds
|
||||
def box(self, p):
|
||||
assert self.in_bounds(p), p
|
||||
return self.lines[p.row][p.col]
|
||||
|
||||
# Returns True if the point p is a '.' "block" in the grid
|
||||
# 'p' must be in bounds
|
||||
def is_block(self, p):
|
||||
return self.box(p) == '.'
|
||||
|
||||
def is_blank(self, p):
|
||||
return self.box(p) == '-'
|
||||
|
||||
def is_letter(self, p):
|
||||
c = self.box(p)
|
||||
return c >= 'A' and c <= 'Z'
|
||||
|
||||
def in_bounds(self, p):
|
||||
return p.row >= 0 and p.row < self.rows() and p.col >= 0 and p.col < self.cols()
|
||||
|
||||
# Next increments the point across the grid, one box at a time
|
||||
# Returns True if point is still in bounds
|
||||
def Next(self, p, vert):
|
||||
if vert:
|
||||
p.row += 1
|
||||
if p.row >= self.rows():
|
||||
p.row = 0
|
||||
p.col += 1
|
||||
else:
|
||||
p.col += 1
|
||||
if p.col >= self.cols():
|
||||
p.col = 0
|
||||
p.row += 1
|
||||
return self.in_bounds(p)
|
||||
|
||||
def FillSpans_(self, vert):
|
||||
p = Point()
|
||||
while (self.in_bounds(p)):
|
||||
while (self.in_bounds(p) and self.is_block(p)):
|
||||
self.Next(p, vert)
|
||||
if not self.in_bounds(p):
|
||||
return
|
||||
startp = copy(p)
|
||||
#print(f"SPAN START: {startp}")
|
||||
|
||||
len_ = 0
|
||||
while (self.in_bounds(p) and not self.is_block(p)):
|
||||
self.Next(p, vert)
|
||||
len_ += 1
|
||||
#print(f"END OF SPAN!!! len={len_}")
|
||||
self.sp.append(Spans(startp, len_, vert))
|
||||
|
||||
def FillSpans(self):
|
||||
self.FillSpans_(vert=False) # horiz
|
||||
self.FillSpans_(vert=True) # vert
|
||||
|
||||
def LoadFromFile(self, filename):
|
||||
with open(filename, 'r') as f:
|
||||
for line in f:
|
||||
@ -131,6 +194,11 @@ class Grid:
|
||||
for s in self.lines:
|
||||
print(f" {''.join(s)}")
|
||||
|
||||
def PrintSpans(self):
|
||||
print(f"Spans:")
|
||||
for span in self.sp:
|
||||
print(f" {span}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
lib = Library()
|
||||
lib.ReadFromFile("top_12000.txt")
|
||||
@ -139,15 +207,5 @@ if __name__ == "__main__":
|
||||
grid.LoadFromFile("test")
|
||||
grid.Check()
|
||||
grid.Print()
|
||||
|
||||
p1 = Point()
|
||||
p2 = Point(2, 1)
|
||||
|
||||
print(f"Point1 is {p1}")
|
||||
print(f"Point2 is {p2}")
|
||||
|
||||
s1 = Span(p1, 3, True)
|
||||
s2 = Span(p2, 5, False)
|
||||
|
||||
print(f"Span1 is {s1}")
|
||||
print(f"Span2 is {s2}")
|
||||
grid.FillSpans()
|
||||
grid.PrintSpans()
|
Loading…
x
Reference in New Issue
Block a user