Skip to content

Instantly share code, notes, and snippets.

@eamonnbell
Last active August 29, 2015 14:06
Show Gist options
  • Save eamonnbell/2c900f9f158226d2164c to your computer and use it in GitHub Desktop.
Save eamonnbell/2c900f9f158226d2164c to your computer and use it in GitHub Desktop.
Time finder
import itertools
import operator
# Student availability represented as a vector of truth values
# aka a binary number. First entry is 9am etc.
time_vector_list = [0b11101001110111101100,
0b11100011110111101000,
0b11110011110111100001,
0b11110011110111100101,
0b11111010100011100101,
0b00100010100011100111,
0b00101010000011100110,
0b00111010000011110110,
0b01101100000011110110,
0b01101100010011110110,
0b01101100011111110110]
def compute_binary_or(a, b):
return bin(a|b)
pairings = list(itertools.combinations(time_vector_list,2))
results = {}
for pair in pairings:
# Do the binary or and cast to string
result = str(compute_binary_or(*pair))
# First item in list corresponds to 9am hence offset
first_time = time_vector_list.index(pair[0]) + 9
second_time = time_vector_list.index(pair[1]) + 9
# Set the value for each time pair to Hamming weight of the OR result
results[(first_time, second_time)] = result.count('1')
# Output the dictionary sorted by value
print sorted(results.iteritems(), key=operator.itemgetter(1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment