Пример #1
0
    for i in range(4):
        ones += table[num & 0xffffL]
        num >>= 16
    return ones % 2


def fancy_bit_manipulation(num):
    # Get the lowest set bit of num using num & ~(x - 1). Eliminate the lowest set bit
    ones = 0
    while num:
        ones += 1
        lowest = num & ~(num - 1)
        num ^= lowest  # Get rid of the lowest set bit
    return ones % 2

set_fns([naive, lookup_table, fancy_bit_manipulation])
test_solutions([0x8000000000000000L], 1)
test_solutions([1], 1)
test_solutions([0], 0)
test_solutions([0xffffffffffffffffL], 0)
test_solutions([0b0010L], 1)
test_solutions([0b0110L], 0)

start = time.clock()
for i in xrange(0xffffL):
    naive(i)
end = time.clock()
print 'Naive takes:', end - start, ' seconds'

start = time.clock()
for i in xrange(0xffffL):
Пример #2
0
        new_num |= mask
    return new_num


def naive16(n):
    # Iteratively mask a new number 32 times
    new_num = 0
    for i in range(8):
        l = (1 << 15 - i) & n
        r = (1 << i) & n
        mask = (r << 15 - 2 * i) | (l >> 15 - 2 * i)  # l and r are swapped
        new_num |= mask
    return new_num


table = []
def precomputed(n):
    if not table:
        for i in range(2 ** 16):
            table.append(naive16(i))
    mask = 2 ** 16 - 1
    return table[n & mask] << 48 | table[n >> 16 & mask] << 32 | table[n >> 32 & mask] << 16 | table[n >> 48 & mask]


set_fns([naive, precomputed])
test_solutions([0x8000000000000000L], 1)
test_solutions([1], 0x8000000000000000L)
test_solutions([0x8000000000000001L], 0x8000000000000001L)
test_solutions([0x0000000000000000L], 0x0000000000000000L)
test_solutions([0b1010000000000000000000000000000000000000000000000000000000000111],
               0b1110000000000000000000000000000000000000000000000000000000000101)
Пример #3
0
import sys
sys.path.append("..")
from test_solutions import test_solutions, set_fns
# Given a 64 bit integer whose weight, w is 1 <= w < 64, return the closest integer with the same weight


def closest(n):
    # Swap the first consecutive bits that differ
    last_bit = n & 1
    for i in range(1, 64):  # iterate from lsb to msb
        current_bit = n >> i & 1
        if last_bit != current_bit:
            swap_mask = 1 << i | 1 << i - 1  # All bits are off, save for the ones we are inverting
            # Invert where swap_mask is 1(1 ^ 1 = 0, 0 ^ 1 = 1). Leave alone where swap_mask is 0 (y ^ 0 = y).
            return n ^ swap_mask
        last_bit = current_bit

set_fns([closest])
test_solutions([10], 9)
test_solutions([9], 10)
test_solutions([12], 10)
test_solutions([3], 5)
test_solutions([5], 6)
test_solutions([6], 5)
Пример #4
0
import sys
sys.path.append("..")
from test_solutions import test_solutions, set_fns
# Power set function

def get_power_set(arg_set):
    # Return a list of all subsets if arg_set
    power_set = [set()]
    for element in arg_set:
        for subset in list(power_set):
            new_subset = subset | {element}
            power_set.append(new_subset)
    return power_set

set_fns([get_power_set])
test_solutions([set()], [set()])
test_solutions([{1,2,3}], [set(), {1}, {2}, {1,2}, {3}, {1,3}, {2,3}, {1,2,3}])