def clean(s: str) -> str:
s = s.replace(" ", "").replace("*", "")
return s
def str2lst(s: str) -> list:
s = s[:-1]
if s[0] == "-":
teken1 = -1
s = s[1:]
else:
teken1 = 1
if "+" in s:
teken2 = 1
s_list = s.split("+")
else: # '-'
teken2 = -1
s_list = s.split("-")
s_list[0] = int(s_list[0]) * teken1
s_list[-1] = int(s_list[-1]) * teken2
return s_list
def lst2str(s_list: list) -> str:
return f"{s_list[0]} {"-" if s_list[-1] < 0 else "+"} {abs(s_list[-1])} i"
def som_aid(c1: list, c2: list) -> list:
return [c1[0] + c2[0], c1[-1] + c2[-1]]
def vermenigvuldig_aid(c1: list, c2: list) -> list:
x1 = c1[0]*c2[0] - c1[-1]*c2[-1]
x2 = c1[0]*c2[-1] + c1[-1] * c2[0]
return [x1, x2]
def som(c1, c2):
c1, c2 = clean(c1), clean(c2)
c1, c2 = str2lst(c1), str2lst(c2)
s = som_aid(c1, c2)
return lst2str(s)
def vermenigvuldig(c1, c2):
c1, c2 = clean(c1), clean(c2)
c1, c2 = str2lst(c1), str2lst(c2)
s = vermenigvuldig_aid(c1, c2)
return lst2str(s)