# Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 3.10 KB  |  hits: 16  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. Google Codejam Milkshake Example: Cannot Find The Mistake [closed]
2. def process_file(file):
3.     fsock = open(file)
5.     fsock.close()
6.     lines = text.split('n')
7.     return lines
8.
9.
10. def process_lines(lines):
11.     cur = 1
12.     ans = []
13.     while cur < len(lines) - 1:
14.         N = int(lines[cur])
15.         cur += 1
16.         M = int(lines[cur])
17.         cur += 1
18.         struct = []
19.         for i in range(0, M):
20.             cust_pref = [int(n) for n in lines[cur].split(' ')]
21.             cust_drinks = [a-1 for a in cust_pref[1::2]]
22.             cust_drinks_malt_pref = cust_pref[2::2]
23.             cust_choice = [(cust_drinks[i], cust_drinks_malt_pref[i]) for i in range(0, len(cust_drinks))]
24.             cur += 1
25.             struct.append(cust_choice)
26.         ans.append((N, struct))
27.     return ans
28.
29.
30. def process_case(case):
32.     i = 0
33.     impossible = False
34.     while i < len(case[1]): # i represents the customer number, case[1] represents customers
35.         acceptable = False
36.         customer = case[1][i]
37.         for drink_preferred in customer:
39.                 acceptable = True
40.                 i += 1 # ok, next customer
41.                 break
42.         if not acceptable:
43.             for drink_preferred in customer:
44.                 # find a malted preference
45.                 if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1:
46.                     # he needs a malted one
48.                     # but then we have to test previous customers, reset i
49.                     i = 0
50.                     break
51.                 #if you have come here, the customer does not have a malted preference, or has a unmalted preference that conflicts with other customer
52.                 impossible = True
53.             #impossible is True, break outer loop
54.             if impossible:
55.                 break
56.     if impossible:
57.        return "IMPOSSIBLE"
58.     else:
59.        return " ".join([str(n) for n in milkshake_menu])
60.
61.
62.
63. if __name__ == "__main__":
64.     import sys
65.     filename = sys.argv[1]
66.     lines = process_file(filename)
67.     inp = process_lines(lines)
68.     for k, v in enumerate(inp):
69.         a = process_case(v)
70.         print "Case #%d: %s" % (k + 1, a)
71.
72. for drink_preferred in customer:
73.             # find a malted preference
74.             if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1:
75.                 # he needs a malted one
77.                 # but then we have to test previous customers, reset i
78.                 i = 0
79.                 break
80.             impossible = True
81.
82. for drink_preferred in customer:
83.             # find a malted preference
84.             if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1:
85.                 # he needs a malted one