Last active
May 11, 2022 18:07
-
-
Save Lubba-64/6c89c9a38d4cc2fe4610cf776fd2fc00 to your computer and use it in GitHub Desktop.
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
import math | |
class OperatedList: | |
def __init__(self, *args: int): | |
self.list = list(args) | |
def __len__(self): | |
return len(self.list) | |
# Operation Functions | |
def _apply_op_between(self, numbers1: list, numbers2: list, func) -> list: | |
""" | |
Applies the given operation between two equally sized collections. | |
""" | |
return [func(n1, n2) for n1, n2 in zip(numbers1, numbers2)] | |
def _apply_op_on(self, numbers: list, value, func) -> list: | |
""" | |
Applies the value and the operation to each element of this collection. | |
""" | |
return [func(num, value) for num in numbers] | |
def operate(self, other, operation): | |
""" | |
Perform an operation on this OperatedList. | |
""" | |
if isinstance(other, OperatedList): | |
return OperatedList(*self._apply_op_between(other.list, self.list, operation)) | |
if isinstance(other, list): | |
return OperatedList(*self._apply_op_between(other, self.list, operation)) | |
return OperatedList(*self._apply_op_on(self.list, other, operation)) | |
# Operators | |
def __add__(self, other): | |
return self.operate(other, lambda x, y: x + y) | |
def __mul__(self, other): | |
return self.operate(other, lambda x, y: x * y) | |
def __pow__(self, other): | |
return self.operate(other, lambda x, y: x**y) | |
def __sub__(self, other): | |
return self.operate(other, lambda x, y: x - y) | |
def __truediv__(self, other): | |
return self.operate(other, lambda x, y: x / y) | |
def __floordiv__(self, other): | |
return self.operate(other, lambda x, y: x // y) | |
def __mod__(self, other): | |
return self.operate(other, lambda x, y: x % y) | |
# Other Operators | |
def st_dev(self, issample: bool): | |
""" | |
The standard deviation of this collection. | |
""" | |
return math.sqrt(self.variance(issample)) | |
def variance(self, issample: bool): | |
""" | |
The variance of this collection. | |
""" | |
return ((self**2).sum() - (self.sum() ** 2 / len(self))) / len(self) - (1 if issample else 0) | |
def sum(self): | |
""" | |
The sum of all vals in this collection. | |
""" | |
return self.summate(lambda x, y: x + y) | |
def summate(self, func): | |
""" | |
Do a consecutive operation on all elements in the list. | |
""" | |
returned = 0 | |
for i in self.list: | |
returned = func(returned, i) | |
return returned | |
def mag(self): | |
""" | |
The magnitude of this vector | |
""" | |
return math.sqrt((self**2).sum() / len(self)) | |
def normalize(self): | |
""" | |
Normalizes this vector | |
""" | |
if self.mag() > 0: | |
self.list = (self / self.mag()).list | |
return self |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so cool and epic B)