Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear and readable (using bitwise operations) solution in Clear category for Multiplication Table by Sebastian.M
def checkio(first, second):
bin1, bin2 = list(bin(first)), list(bin(second))
bin1 = bin1[2:]
bin2 = bin2[2:]
bin1 = list(map(int, bin1))
bin2 = list(map(int, bin2))
sum = 0
for op in ['and','or','xor']:
for x in bin1:
tab=[]
for y in bin2:
if op=='and':
tab.append(x & y)
elif op=='or':
tab.append(x | y)
else:
tab.append(x ^ y)
sum+=int(''.join(str(t) for t in tab),2)
return sum
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(4, 6) == 38
assert checkio(2, 7) == 28
assert checkio(7, 2) == 18
Oct. 28, 2016