Skip to content

Instantly share code, notes, and snippets.

@kazufusa
Last active November 12, 2015 13:36
Show Gist options
  • Save kazufusa/c205bc828ffcc28362af to your computer and use it in GitHub Desktop.
Save kazufusa/c205bc828ffcc28362af to your computer and use it in GitHub Desktop.
Shape class
class Shape(object):
__delimiter = "_"
def __init__(self):
"""
>>> shape = Shape()
>>> shape.add([1, 2, 3, 4])
>>> shape.add([1, 2, 5, 6])
>>> shape.add([3, 4, 7, 8])
>>> shape.outlines()
[[1, 4], [1, 6], [2, 3], [2, 5], [3, 8], [4, 7], [5, 6], [7, 8]]
"""
self.lines = {}
def add(self, ids):
for line_hash in self.__line_hashes(ids):
self.lines[line_hash] = self.lines.get(line_hash, 0) + 1
def outlines(self):
return sorted([int(x) for x in k.split(Shape.__delimiter)] for k, v in self.lines.items() if v == 1 )
@staticmethod
def __line_hashes(ids):
"""
>>> Shape._Shape__line_hashes([1, 2, 3, 4])
['1_2', '2_3', '3_4', '1_4']
"""
ids.append(ids[0])
return [Shape.__delimiter.join(sorted([str(ids[i]), str(ids[i+1])])) for i in range(len(ids)-1)]
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment