49 lines
1.0 KiB
Python
49 lines
1.0 KiB
Python
|
bits = []
|
||
|
|
||
|
gamma = 0
|
||
|
epsilon = 0
|
||
|
|
||
|
gamma_str = ''
|
||
|
epsilon_str = ''
|
||
|
|
||
|
with open('data', 'r') as fp:
|
||
|
|
||
|
# NOTE(dev): We assume all lines are the same length
|
||
|
for i in range(0, len(fp.readline().strip())):
|
||
|
bits.append(0)
|
||
|
|
||
|
# # DEBUG(dev):
|
||
|
# test = fp.readline()
|
||
|
# for i in range(0, len(test)):
|
||
|
# print test[i]
|
||
|
|
||
|
for line in fp:
|
||
|
for i in range(0, len(line.strip())):
|
||
|
if(line[i] == '1'):
|
||
|
bits[i] = bits[i] + 1
|
||
|
else:
|
||
|
bits[i] = bits[i] - 1
|
||
|
|
||
|
for i in range(0, len(bits)):
|
||
|
ch = '1'
|
||
|
if(bits[i] < 0):
|
||
|
ch = '0'
|
||
|
gamma_str = gamma_str + ch
|
||
|
|
||
|
# # DEBUG(dev):
|
||
|
# for i in range(1, len(gamma_str)+1):
|
||
|
# print gamma_str[-i]
|
||
|
|
||
|
# NOTE(dev): Have to do (i+1) because we're going backwards, which starts at -1
|
||
|
for i in range(0, len(gamma_str)):
|
||
|
if(gamma_str[-(i+1)] == '1'):
|
||
|
gamma = gamma + pow(2,i)
|
||
|
else:
|
||
|
epsilon = epsilon + pow(2,i)
|
||
|
|
||
|
|
||
|
# print " bits: ", bits
|
||
|
print " gamma: ", gamma
|
||
|
print "epsilon: ", epsilon
|
||
|
print " POWER: ", gamma * epsilon
|