This is the solution to Code Jam 2014 qualification round problem A. Magic Trick. (Problem Link)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
__author__ = 'Coddicted' def read_row(inf): return inf.readline().strip().split(' ') def main(): with open('data_files/A−small−practice.in', 'r') as inf, open('data_files/A−small−practice.out', 'w') as of: t = int(inf.readline()) case_cnt = 1 while case_cnt <= t: row = int(inf.readline()) i = 1 row1 = [] while i <= 4: if i == row: row1 = read_row(inf) else: inf.readline() i += 1 row = int(inf.readline()) row2 = [] i = 1 while i <= 4: if i == row: row2 = read_row(inf) else: inf.readline() i += 1 row1 = [int(item) for item in row1] row2 = [int(item) for item in row2] comm = [item for item in row1 if item in row2] if len(comm) == 1: of.write('Case #' + str(case_cnt) + ': ' + str(comm[0]) + '\n') print 'Case #' + str(case_cnt) + ': ' + str(comm[0]) elif len(comm) > 1: of.write('Case #' + str(case_cnt) + ': ' + 'Bad magician!' + '\n') print 'Case #' + str(case_cnt) + ': ' + 'Bad magician!' else: of.write('Case #' + str(case_cnt) + ': ' + 'Volunteer cheated!' + '\n') print 'Case #' + str(case_cnt) + ': ' + 'Volunteer cheated!' case_cnt += 1 main() |