Coordinate order - not sure which comparison is the "correct" one

This commit is contained in:
Stefan Harmuth 2021-12-22 08:44:15 +01:00
parent 1e33dd6210
commit 7656e90984
2 changed files with 68 additions and 0 deletions

View File

@ -144,6 +144,35 @@ class Coordinate:
else: else:
return Coordinate(self.x - other.x, self.y - other.y, self.z - other.z) return Coordinate(self.x - other.x, self.y - other.y, self.z - other.z)
"""
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z
def __gt__(self, other):
if self.z is None:
return self.x > other.x and self.y > other.y
else:
return self.x > other.x and self.y > other.y and self.z > other.z
def __ge__(self, other):
if self.z is None:
return self.x >= other.x and self.y >= other.y
else:
return self.x >= other.x and self.y >= other.y and self.z >= other.z
def __lt__(self, other):
if self.z is None:
return self.x < other.x and self.y < other.y
else:
return self.x < other.x and self.y < other.y and self.z < other.z
def __le__(self, other):
if self.z is None:
return self.x <= other.x and self.y <= other.y
else:
return self.x <= other.x and self.y <= other.y and self.z <= other.z
"""
@staticmethod @staticmethod
def generate(from_x: int, to_x: int, from_y: int, to_y: int, def generate(from_x: int, to_x: int, from_y: int, to_y: int,
from_z: int = None, to_z: int = None) -> List[Coordinate]: from_z: int = None, to_z: int = None) -> List[Coordinate]:

39
tools/trees.py Normal file
View File

@ -0,0 +1,39 @@
from typing import Any, Union
class BinaryTreeNode:
data: Any
left: Union['BinaryTreeNode', None]
right: Union['BinaryTreeNode', None]
def __init__(self, data: Any):
self.data = data
self.left = None
self.right = None
def traverse_inorder(self):
if self.left:
self.left.traverse_inorder()
yield self.data
if self.right:
self.right.traverse_inorder()
def traverse_preorder(self):
yield self.data
if self.left:
self.left.traverse_preorder()
if self.right:
self.right.traverse_preorder()
def traverse_postorder(self):
if self.left:
self.left.traverse_preorder()
if self.right:
self.right.traverse_postorder()
yield self.data