Skip to content

Instantly share code, notes, and snippets.

@erikbgithub
Created August 12, 2016 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikbgithub/855aeb8be5c817d42ec0812598104184 to your computer and use it in GitHub Desktop.
Save erikbgithub/855aeb8be5c817d42ec0812598104184 to your computer and use it in GitHub Desktop.
how I would do it
def generate_area(x,y, ignore):
""" generates a one dimensional representation of boolean values
the key part is that ignore contains a list of points that is set to false
"""
return [i not in ignore for i in range(x*y)]
def ignore_square(start, linelength):
""" generate a square of elements to ignore """
end = linelength - start
top_left_point = start*linelength+start
top_right_point = start*linelength+end
bottom_left_point = end*linelength+start
bottom_right_point = end*linelength+end
# +1 because that's how range works
line_top=range(top_left_point, top_right_point+1)
line_bottom=range(bottom_left_point, bottom_right_point+1)
# - the last parameter of range is a step, we always step one line further
# - here we add an additional line to the start and remove the +1 since the corner points are already generated
line_left=range(top_left_point+linelength, bottom_left_point, linelength)
line_right=range(top_right_point+linelength, bottom_right_point, linelength)
return line_top + line_bottom + line_left + line_right
class Chip(object):
""" generates a chip layout; adapt str() method to change output """
def __init__(self, size, symbol_true = "o", symbol_false = " "):
"""
:param size: the size of the whole square
:param symbol_true: (opt) change how the pins should be represented
:param symbol_false: (opt) change how the empty places should be represented
"""
self.size_x = size
self.size_y = size
self.symbol_true = symbol_true
self.symbol_false = symbol_false
self.ignores = []
self.regenerate()
def regenerate(self):
""" recreate whole data after changing some of the datapoints """
self.area = generate_area(self.size_x, self.size_y, self.ignores)
def ignore(self, distance):
""" ignore a square inside """
self.ignores += ignore_square(distance, self.size_x)
self.regenerate()
return self
def __str__(self):
out = ""
for py in range(self.size_y):
for px in range(self.size_x):
out += self.symbol_true if self.area[px+py*self.size_x] else self.symbol_false
out += "\n"
return out
def main():
print "complete:"
print Chip(15)
print
print "your example:"
print Chip(15).ignore(5)
print
print "another example:"
print Chip(15).ignore(3)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment