Last active
November 12, 2015 13:36
-
-
Save kazufusa/c205bc828ffcc28362af to your computer and use it in GitHub Desktop.
Shape class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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