9. This XOR That? - Alternative Solution!


#1


https://www.codecademy.com/en/courses/python-intermediate-en-KE1UJ/1/4?curriculum_id=4f89dab3d788890003000096


I coded my own XOR function. If anyone has any suggestions on how to improve this, i would appreciate the feedback.


#print bin(0b1110 ^ 0b101)


X = "1110"
Y  = "101"


combined = [X, Y]

def bitwise_XOR(X, Y):
    one = "1"
    new_x = X
    new_y = Y
    list_x = []
    list_y = []
    together = []
    different_length = abs(len(X) - len(Y))
    final = ""
    
    if len(X) > len(Y):
        for i in X:
            together.append("0")
        new_y = ("0" * different_length) + Y
    elif len(Y) > len(X):
        for i in Y:
            together.append("0")
        new_x = ("0" * different_length) + X
    
    for i in new_x:
        list_x.append(i)
    for i in new_y:
        list_y.append(i)

    for i, c in enumerate(new_x):
        if c != list_y[i]:
            together[i] = "1"

    for i, c in enumerate(new_y):
        if c != list_x[i]:
            together[i] = "1" 

   
    final = "0b" + final.join(together)
    return final
    
print bitwise_XOR(X, Y)


#2

I am not in any way entitled to comment on your solution or to suggest improvements as I'm just learning myself but I can post my version so you can see a different approach to it
I know that some of the stuff I did in my code is considered really bad programming so be cautious :smile:
And yes, for anyone who might wonder, I know that this thread is 223 days old...

X  = 0b1110
Y  = 0b101

def xor(X, Y):
    x = str(bin(X)).replace("0b", "")
    y = str(bin(Y)).replace("0b", "")
    l = len(x)
    dif = l - len(y)

    if len(x) > len(y) or len(x) < len(y):
        if dif > 0:
            y = ("0" * dif) + y
        else:
            x = ("0" * abs(dif)) + x
            l = len(y)

    res_string = "0b" + str([1 if x[i] != y[i] else 0 for i in range(0, l)])
    res_string = res_string.replace(", ", "").replace("[", "").replace("]", "")
    result = bin(int(res_string, 2))
    return result
    
print xor(X,Y)

#3

Here's another solution:

def bitwise_xor(x,y):
    if len(x) < len (y):
        x,y = y,x

    a = x[:1:-1]
    b = y[:1:-1]
    result = ""

    for i in range(len(b)):
        if a[i] == b[i]:
            result += "0"
        else:
            result += "1"
			
    result += a[len(b)::]
    return "0b" + result[::-1]

print bitwise_xor("0b1110","0b101")

#4

Not any offense. Seriously not. But this annoys me out. Please guys, PLEASE stop posting these direct solutions to threads to earn better help desk levels.


#5

Boa noite, nesse exercício assim como os demais temos que prestar atenção às instruções.

Exiba (print) o resultado de usar ^ em 0b1110 e 0b101 como uma string binária.

print bin(0b1110 ^ 0b101)


#6

i've shortened this not sure if correct:

def xor(x,y):
a = x[:1:-1]
b = y[:1:-1]
new = ['1' if a[n] != b[n] else '0' for n in range(len(b))]
new += '1' * abs(len(a)-len(b))
new = '0b' + "".join(new[::-1])
print new

xor('0b1110','0b101')


#7

Not very elegant but it doesn't convert the inputs, only shifts them out of being.

>>> def xor(a, b):
    x = ''
    if b > a:
        a, b = b, a
    while a > 0:
        m, n = a & 1, b & 1
        y = 1 if (m + n) % 2 else 0
        x = str(y) + x
        a = a >> 1
        b = b >> 1
    return '0b' + x

>>> print (xor(0b1110, 0b101))
0b1011
>>>

#9