def product(ls, start, end): """Multiplies all the numbers in the given range of the list Input data: ls -- The list start -- The starting point of the range end -- The end point of the range Output data: The product of the numbers in the given range """ prod = ComplexNumber.create(1) for i in ls[start:end + 1]: prod = ComplexNumber.multiply(prod, i) return prod
def sums(ls, start, end): """Sums up all the numbers in the given range of the list Input data: ls -- The list start -- The starting point of the range end -- The end point of the range Output data: The sum of the numbers in the given range """ s = ComplexNumber.create(0) for cn in ls[start:end + 1]: s = ComplexNumber.add(s, cn) return s
def replace(ls, args, **kwargs): Validators.replace(ls, args) old = ComplexNumber.create(args[0]) new = ComplexNumber.create(args[2]) Commands.replace(ls, old, new, **kwargs)
def insert(ls, args, **kwargs): Validators.insert(ls, args) Commands.insert(ls, ComplexNumber.create(args[0]), int(args[2]), **kwargs)
def add(ls, args, **kwargs): Validators.add(args) Commands.add(ls, ComplexNumber.create(args[0]), **kwargs)
def create(): assert (ComplexNumber.create("1+2j") == {"real": 1, "imag": 2}) assert (ComplexNumber.create("3-2j") == {"real": 3, "imag": -2}) assert (ComplexNumber.create("-1+12j") == {"real": -1, "imag": 12})