A BIT of This OR That_Alternative solution


#1



Error: string index out of range


I've been trying to solve this exercise not using '|' .
Who know where is mistake? Please help


def bin_or(bitwise_a, bitwise_b):
    type(bitwise_a) is str
    type(bitwise_b) is str
    result_or = ''
    a = bitwise_a[2:]
    b = bitwise_b[2:]
    if len(a) > len(b):
        b.zfill(len(a))
    elif len(a) < len(b):
        a.zfill(len(b))  
    for i, bin_a in enumerate(a):
        bin_b = b[i]
        if bin_a == bin_b:
            result_or += bin_a
        elif bin_a > bin_b:
            result_or += bin_a
        else:
            result_or += bin_b
    print '0b' + result_or
    return result_or    
bin_or('0b1110', '0b101')


#2

i fiddled a bit with your code:

def bin_or(bitwise_a, bitwise_b):
    type(bitwise_a) is str
    type(bitwise_b) is str
    result_or = ''
    a = bitwise_a[2:]
    b = bitwise_b[2:]
    if len(a) > len(b):
        b.zfill(len(a))
    elif len(a) < len(b):
        a.zfill(len(b)) 
    print list(enumerate(a))
    print b
    for i, bin_a in enumerate(a):
        bin_b = b[i]
        if bin_a == bin_b:
            result_or += bin_a
        elif bin_a > bin_b:
            result_or += bin_a
        else:
            result_or += bin_b
    print '0b' + result_or
    return result_or    
bin_or('0b1110', '0b101')

i added some print statements, as you can a has 4 numbers, where b has 3. so b[3] is going to give an index out of range error since b and a are not of equal length


Problem in "How is Everybody Doing?"
#3

I've solved my problem. I've just added the names of variables:

if len(a) > len(b):
b = b.zfill(4)
elif len(a) < len(b):
a = a.zfill(len(b))

And everything works


#4

if len(a) > len(b):
b = b.zfill(len(a))
elif len(a) < len(b):
a = a.zfill(len(b))


#5