Your program will receive an array of complex numbers represented as strings. Your task is to write the  `complexSum` function which have to return the sum as a string.

Complex numbers can be written in the form of `a+bi`, such as `2-3i` where `2` is the real part, `3` is the imaginary part, and `i` is the "imaginary unit". 

When you add two complex numbers, the real and the imaginary part needs to be added separately,so for example `2+3i + 5-i = (2+5)+(3i-i) = 7+2i`

Both the complex and the imaginary part can be 0, so `123`, `-2i` or `i` are also complex numbers.

Complex numbers must be returned in their shortest form, so e.g. `0+1*i` should be just `i`, and `10+0i` should be `10`. This is also how you will get them!

For simplicity, the coefficients will always be integers. If the array is empty, return `0`.

Have fun! :)

Difficulty: introductory

Starter code:

def complexSum(arr):
	

Solutions:
def complexSum(arr, sub={'1i': 'i', '-1i': '-i', '0i': '0'}):
    s = str(sum(complex(x.replace('i', 'j')) for x in arr)).replace('j', 'i')
    s = s.strip('()')
    s = s.replace('+0i', '')
    return sub.get(s, s)    

def complexSum(arr):
    a = [complex(i.replace("i", "j")) for i in arr]
    s = 0
    for i in a:
        s += i
    if s == 1j:
        return "i"
    elif s == -1j:
        return "-i"
    elif s.imag == 0j:
        s = int(s.real)
    s = str(s).replace("j", "i").lstrip("(").rstrip(")")
    return s

from functools import reduce
from operator import add
import re


P_TO_PYTHON   = re.compile(r'[+-]?\d*i')
P_FROM_PYTHON = re.compile(r'[()]|\b1(?=j)|([+-]|^)0j')

complexify=lambda m: "{}{}1j".format(m.group()[:-1], '*' * (len(m.group()) > 1 and m.group()[-2] not in "-+"))

def complexSum(arr):
    lst = [P_TO_PYTHON.sub(complexify, s) for s in arr]
    cpx = reduce(add, map(eval, lst), 0)
    return P_FROM_PYTHON.sub('', str(cpx)).replace('j','i')

s,complexSum=__import__('re').sub,lambda a:s('[-+]0i?|[()]','',s(r'(\b1)?j','i',str(eval(s('i','j',s(r'\bi','1i','+'.join(a)or'0'))))))

import re
def complexSum(arr):
    real=0
    img=0
    for a in arr:
        for b in re.findall(r'[-+]?\d*i?',a):
            if not b:
                continue
            if 'i' in b:
                if b=='+i' or b=='i':
                    img+=1
                elif b=='-i':
                    img-=1
                else:
                    img+=int(b[:-1])
            else:
                real+=int(b)

    if img==0:
        return str(real)
    elif real==0:
        if img==1:
            return 'i'
        elif img==-1:
            return '-i'
        else:
            return '{}i'.format(img)
    else:
        return '{}{:+}i'.format(real,img).replace('+1i','+i').replace('-1i','-i')

def complexSum(arr):
    s = sum(complex(i.replace('i','j')) for i in arr)
    r,i = map(int,(s.real,s.imag))
    return f'{r}' if not i else f"{'-' if i==-1 else '' if i==1 else i}i" if not r else f"{r}{'+' if i>0 else ''}{i}i"

def complexSum(arr):
    real_sum = 0
    imag_sum = 0
    for string in arr:
        val = ''
        for char in string:
            if char == '-':
                if len(val) > 0:
                    real_sum += int(val)
                val = '-'
            elif char == '+':
                real_sum += int(val)
                val = ''
            elif char == 'i':
                if val == '':
                    val = 1
                elif val[0] == '-':
                    if len(val) != 1:
                        val = -1 * int(val[1::])
                    else: val = -1
                imag_sum += int(val)
                val = 0
            else:   
                val += char
        real_sum += int(val)
    fin_string = ''
    if imag_sum == 1:
        fin_string = 'i'
    elif imag_sum == 0:
        fin_string = ''
    elif imag_sum == -1:
        fin_string = '-i'  
    else:
        if imag_sum > 1 and real_sum != 0:
            fin_string = '+'+str(imag_sum)+'i'
        else: 
            fin_string = str(imag_sum)+'i'
    if real_sum == 0 and imag_sum != 0:
        return fin_string
    return (str(real_sum)+fin_string) if (real_sum+imag_sum !=0) else '0'

complexSum=lambda A:(lambda s:{'1i':'i','-1i':'-i','0i':'0'}.get(s,s))(str(sum(complex(x.replace('i','j'))for x in A)).replace('j','i').strip('()').replace('+0i',''))