Hello everyone,
I'm trying to solve this issue using the strategy described on the analysis
of that problem. I tested locally and the solution is passing all three
test cases. When I sent to the server is failing on the test set 3.
import sys
from sys import stdin
def do_complement():
#print('Complement')
global ans
#print(ans)
for i in range(len(ans)):
if ans[i] == '0':
ans[i] = '1'
elif ans[i] == '1':
ans[i] = '0'
#print(ans)
def do_swap():
#print('Swap')
global ans
#print(ans)
n = len(ans)
for i in range(n//2):
ans[i], ans[n-1-i] = ans[n-1-i], ans[i]
#print(ans[i], ans[n-1-i])
#print(ans)
def query(i):
global ans
val = get_index(i)
validate()
ans[i-1] = val
def find_index():
global ans
for i in range(len(ans)//2):
if ans[i] == '' or ans[len(ans)-1-i] == '':
return i
return len(ans)
'''
Get the value of the bit string on i index
'''
def get_index(i):
print(i)
sys.stdout.flush()
global cont
cont = cont + 1
val = stdin.readline().strip()
return val
'''
Check if the value on index i has changed and differs from the stored value
'''
def value_change(i, old_value):
val = get_index(i)
return val != old_value
def find_equal_pair():
global ans
n = len(ans)
for i in range(n):
if ans[i] != '' and ans[i] == ans[n-1-i]:
return (i+1, ans[i])
return None
def find_diff_pair():
global ans
n = len(ans)
for i in range(n):
if ans[i] != '' and ans[n-1-i] != '' and ans[i]!=ans[n-1-i]:
return (i+1, ans[i])
return None
'''
Validate which change occurs on the bit string after 10 queries
'''
def validate():
# In this moment the bit string has changed so We need to find how it
change?
if cont % 10 == 1:
equal_values = find_equal_pair()
diff_values = find_diff_pair()
global ans
#print('Validate')
#print(ans)
#print(equal_values)
#print(diff_values)
if equal_values != None:
# The string has an pair of 0's or 1's so we need to check what
was
i = equal_values[0]
old_value = equal_values[-1]
if value_change(i, old_value):
# The value change so it could be a complement or
# a swap-complement. We need to validate what was using
# a pair of different numbers
do_complement()
if diff_values != None:
j = diff_values[0]
if value_change(j, diff_values[-1]):
# The value of the pair change so it was a
complement
pass
else:
# The value of the pair didn't change so it was
complement-swap
do_swap()
#do_both()
else:
# The values are the same so it could be a swap or
# the string didn't change. We need to validate what was
using
# a pair of differents numbers
if diff_values != None:
j = diff_values[0]
if value_change(j, diff_values[-1]):
# The value of the pair change so it was a swap
do_swap()
else:
# The value of the pair didn't change so the string
didn't change
pass
else:
# Do nothing for this string.
pass
elif diff_values != None:
# The bit string is composed only for pair of differents numbers
j = diff_values[0]
if not value_change(j, diff_values[-1]):
# Do nothing
pass
else:
# It is a swap or a complement but both produces the same
change
# on the bit string
do_complement()
def main():
cases, n = [int(x) for x in stdin.readline().strip().split()]
for c in range(cases):
global cont, ans
cont = 0
ans = ['' for x in range(n)]
i = 0
while i < len(ans):
# ask for the ith and jth bit
if ans[i] == '':
query(i + 1)
if ans[n-1-i] == '':
query(n - i)
i = find_index()
print(''.join(ans))
sys.stdout.flush()
check_ans = stdin.readline().strip()
if check_ans == 'N':
break
main()
I'm using python 3. Do anyone has an idea of an error on this code?
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/d02a236e-2750-4272-ad04-0a404a38f12a%40googlegroups.com.