Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Multiplication Table by Rounin
def popcount(n, digit):
return len(bin(n)[2:].replace(('0' if digit == '1' else '1'),''))
def _and(a, b):
return popcount(a, '1')*b
def _or(a, b):
return popcount(a, '0')*b + popcount(a, '1')*int((bin(b)[2:].replace('0', '1')), 2)
def _not(n):
return int(bin(n)[2:].replace('0','x').replace('1','0').replace('x','1'), 2)
def _xor(a, b):
return popcount(a, '1')*_not(b) + popcount(a, '0')*b
def checkio(first, second):
return _and(first, second) + _or(first, second) + _xor(first, second)
#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
Aug. 5, 2017