2012-02-24 4 views
-2

を検索することはできません私は、これらの問題練習しています:私は、(でも、入力と出力の手動検査後)正しい解決策を持っていると信じてい http://code.google.com/codejam/contest/32016/dashboard#s=p1&a=1GoogleのCodejamミルクセーキ例:間違い

を。 まだまだ私はつづき続けるIncorrect Output

誰かが間違っているかもしれないと指摘できますか?

def process_file(file): 
    fsock = open(file) 
    text = fsock.read() 
    fsock.close() 
    lines = text.split('\n') 
    return lines 


def process_lines(lines): 
    cur = 1 
    ans = [] 
    while cur < len(lines) - 1: 
     N = int(lines[cur]) 
     cur += 1 
     M = int(lines[cur]) 
     cur += 1 
     struct = [] 
     for i in range(0, M): 
      cust_pref = [int(n) for n in lines[cur].split(' ')] 
      cust_drinks = [a-1 for a in cust_pref[1::2]] 
      cust_drinks_malt_pref = cust_pref[2::2] 
      cust_choice = [(cust_drinks[i], cust_drinks_malt_pref[i]) for i in range(0, len(cust_drinks))] 
      cur += 1 
      struct.append(cust_choice) 
     ans.append((N, struct)) 
    return ans 


def process_case(case): 
    milkshake_menu = [0] * case[0] # our default menu 
    i = 0 
    impossible = False 
    while i < len(case[1]): # i represents the customer number, case[1] represents customers 
     acceptable = False 
     customer = case[1][i] 
     for drink_preferred in customer: 
      if milkshake_menu[drink_preferred[0]] == drink_preferred[1]: 
       acceptable = True 
       i += 1 # ok, next customer 
       break 
     if not acceptable: 
      for drink_preferred in customer: 
       # find a malted preference 
       if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1: 
        # he needs a malted one 
        milkshake_menu[drink_preferred[0]] = 1 
        # but then we have to test previous customers, reset i 
        i = 0 
        break 
       #if you have come here, the customer does not have a malted preference, or has a unmalted preference that conflicts with other customer 
       impossible = True 
      #impossible is True, break outer loop 
      if impossible: 
       break 
    if impossible: 
     return "IMPOSSIBLE" 
    else: 
     return " ".join([str(n) for n in milkshake_menu]) 



if __name__ == "__main__": 
    import sys 
    filename = sys.argv[1] 
    lines = process_file(filename) 
    inp = process_lines(lines) 
    for k, v in enumerate(inp): 
     a = process_case(v) 
     print "Case #%d: %s" % (k + 1, a) 

ペーストビン出力:http://pastebin.com/uXJQKbSr奇妙に見える

+0

これはSOの仕組みではありません。 [faq]と[ask]をお読みください。 –

+0

サンプルデータで試しましたか?それは動作しますか? – katrielalex

+0

はい、有効な回答を出力します。私もそれを投稿すべきですか? –

答えて

2

ことの一つは、私はこれは私が今、あなたの考える

 for drink_preferred in customer: 
      # find a malted preference 
      if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1: 
       # he needs a malted one 
       milkshake_menu[drink_preferred[0]] = 1 
       # but then we have to test previous customers, reset i 
       i = 0 
       break 
     else: 
      impossible = True 

ことを期待する

 for drink_preferred in customer: 
      # find a malted preference 
      if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1: 
       # he needs a malted one 
       milkshake_menu[drink_preferred[0]] = 1 
       # but then we have to test previous customers, reset i 
       i = 0 
       break 
      impossible = True 

ループ内にありすべての可能性をチェックする前にタスクが不可能であると判断します。

+0

ありがとうございました。私はいつも...と思っていましたが、いつも同じくぼみのレベルを持っていました。ドキュメントを参照する必要があります。 –

+4

もしあなたと同じインデントがあるなら、あなたは大丈夫です。この例では、elseはforループにリンクされています。意味は、forループが最後に達した場合(つまり、途切れることなく)、else節を実行するということです。 –