<filename>_tasks/add_strings_as_binary.py<gh_stars>1-10
# add strings as binary, without using numbers

invert = lambda digit: '0' if digit == '1' else '1'

def add(a: str, b: str) -> str:
    res = []
    carry_flag = '0'
    max_len = max(len(a), len(b))
    a = reversed(a.zfill(max_len)) # pad with zeroes and invert
    b = reversed(b.zfill(max_len))
    for n1, n2 in zip(a, b): # iterate from lowest digits
        if n1 == n2: # 00 or 11 cases
            res.append(carry_flag)
            carry_flag = n1
        else: # 10 or 10 cases
            res.append(invert(carry_flag))
    if carry_flag == '1': # add carry if it is set
        res.append(carry_flag)
    return ''.join(reversed(res)) # reverse and join to str

assert add('0', '0') == '0'
assert add('0', '1') == '1'
assert add('1', '1') == '10'
assert add('0', '10') == '10'
assert add('1', '10') == '11'
assert add('1', '11')  == '100'
assert add('10', '10') == '100'
assert add('11', '11') == '110'
assert add('1'+'0'*1000, '1') == '1'+'0'*999 + '1'
assert add('1'+'0'*1000, '1'+'0'*1000) == '1'+'0'*1001
print('OK')
<filename>_tasks/add_strings_as_binary.py<gh_stars>1-10
# add strings as binary, without using numbers

invert = lambda digit: '0' if digit == '1' else '1'

def add(a: str, b: str) -> str:
    res = []
    carry_flag = '0'
    max_len = max(len(a), len(b))
    a = reversed(a.zfill(max_len)) # pad with zeroes and invert
    b = reversed(b.zfill(max_len))
    for n1, n2 in zip(a, b): # iterate from lowest digits
        if n1 == n2: # 00 or 11 cases
            res.append(carry_flag)
            carry_flag = n1
        else: # 10 or 10 cases
            res.append(invert(carry_flag))
    if carry_flag == '1': # add carry if it is set
        res.append(carry_flag)
    return ''.join(reversed(res)) # reverse and join to str

assert add('0', '0') == '0'
assert add('0', '1') == '1'
assert add('1', '1') == '10'
assert add('0', '10') == '10'
assert add('1', '10') == '11'
assert add('1', '11')  == '100'
assert add('10', '10') == '100'
assert add('11', '11') == '110'
assert add('1'+'0'*1000, '1') == '1'+'0'*999 + '1'
assert add('1'+'0'*1000, '1'+'0'*1000) == '1'+'0'*1001
print('OK')
# add strings as binary, without using numbers # pad with zeroes and invert # iterate from lowest digits # 00 or 11 cases # 10 or 10 cases # add carry if it is set # reverse and join to str