# -*- coding: utf-8 -*- from collections import namedtuple class Coordinate(namedtuple("Coordinate", ["x", "y"])): def __sub__(self, other: "Coordinate") -> "Coordinate": return Coordinate(self.x - other.x, self.y - other.y) def __add__(self, other: "Coordinate") -> "Coordinate": return Coordinate(self.x + other.x, self.y + other.y) def manhattan_distance(self, other: "Coordinate") -> int: return abs(self.x - other.x) + abs(self.y - other.y) @property def polarity(self) -> "Coordinate": try: px = abs(self.x) / self.x except ZeroDivisionError: px = 0 try: py = abs(self.y) / self.y except ZeroDivisionError: py = 0 return Coordinate( px, py, )