-
-
Save cesarkawakami/4505062 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Number(object): | |
def __init__(self, value): | |
self.value = value | |
def __add__(self, other): | |
return Average(self.value, 1) + other | |
def __radd__(self, other): | |
return Average(self.value, 1) + other | |
class Average(object): | |
def __init__(self, value, n): | |
self.value = value | |
self.n = n | |
def __add__(self, other): | |
if not isinstance(other, Average): | |
return NotImplemented | |
new_n = self.n + other.n | |
new_value = self.n * self.value + other.n * other.value | |
new_value /= new_n | |
return Average(new_value, new_n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment