Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. #-*- coding: utf-8 -*-  
  2. #-*- coding: cp950 -*- 
  3. # (subtask1)Check whether a input is valid
  4. # ans_ok(x)
  5. from itertools import combinations
  6. def valid(s):
  7.     if len(s)!=4:
  8.         return False
  9.     if any(x not in '0123456789' for x in s):
  10.         return False
  11.     if any(x==y for x, y in combinations(s,2)):
  12.         return False
  13.     return True
  14.  
  15.  
  16. #(subtask2)int變成長度為4的字串
  17. def int2ans(n):
  18.    
  19.     stk = str(n);
  20.     four = stk.rjust(4, "0")
  21.    
  22.     '''
  23.    這個 function 將數字 n 轉成長度為 4 的字串
  24.    目前的版本在 n == 123 時會輸出 123,但我們要的是 0123
  25.    你可以假設 n『都是介於 0~9999 的整數』
  26.    唯之後使用這個 function 時記得留意是否傳進來的 n 都符合該假設
  27.    '''
  28.     return four
  29.  
  30.    
  31. #(subtask 3) 產生所有合法的可能的答案  
  32. possible_ans = []
  33. for i in range(10000):
  34.     a = int2ans(i)
  35.     if valid(a):
  36.         possible_ans.append(a)
  37.  
  38.  
  39. #(subtask4) 從可能的答案中選一個輸出
  40. import random
  41. def pick_ans(possible_ans):
  42.     picka = random.choice(possible_ans)
  43.  
  44.  
  45.     '''
  46.    possible_ans 是目前所有可能的答案
  47.    隨便選一個輸出吧,不過要小心不要選到超過 list 範圍的答案喔!!!
  48.    不然一分都拿不到
  49.    
  50.    你可以假設『possible_ans 裡面至少有一個答案』
  51.    唯之後使用這個 function 時記得留意是否傳進來的 possible_ans 都符合該假設
  52.    等等,如果這個假設不成立是不是哪裡怪怪的??
  53.    '''
  54.     return picka
  55. print 'pick_ans:',pick_ans(possible_ans)
  56.  
  57.  
  58. #(subtask5) 對於A猜的數字,根據Q自己的答案,回答幾A幾B
  59. from random import sample
  60. a_guess = pick_ans(possible_ans)
  61. q_ans =''.join(sample('0123456789',4))
  62. def response(guess, ans):
  63.     if guess != ans:
  64.         A = 0
  65.         B = 0
  66.         for x,y in zip(guess, ans):
  67.             if x == y: A += 1
  68.         for x in guess:
  69.             if x in ans: B += 1
  70.         B -= A
  71.         print guess,'is {}A{}B.'.format(A,B)
  72.  
  73.     else:
  74.         print 'Congrats! The answer is', guess
  75.     return [A,'A',B,'B']
  76.  
  77. response(a_guess, q_ans)
  78. '''
  79.    while possible_ans != my_guess
  80.        sum(1 for x,y in zip(my_guess,possible_ans) if x==y) = nA
  81.        sum(1 for x in my_guess if x in possible_ans)- nA = nB
  82.  
  83.    my_guess 是我這次猜的答案
  84.    nAnB 是 Q 君針對我這次的猜測進行的回答
  85.    possible_ans 是目前可能的答案,但是經過這次的猜測與回答後,有一些變得不可能了
  86.    
  87.    提示:借用一下 subtask 5 的 function 這題就很簡單咯
  88. '''
  89.  
  90. #(subtask6) 根據自己猜的答案與 Q.py 的回覆刪掉不可能的答案
  91. def refine(my_guess, possible_ans):
  92.     nAnB = response(a_guess, q_ans)
  93.     nA   = nAnB[0]
  94.     nB   = nAnB[2]
  95.     still_possible_ans = possible_ans
  96.     for ans in still_possible_ans:
  97.         if sum(1 for x,y in zip(my_guess, ans)if x == y) != nA and sum(1 for x in my_guess if x in ans) - nA != nB:
  98.             still_possible_ans.remove(ans)
  99.     return still_possible_ans
  100.  
  101. while a_guess != q_ans:
  102.     possible_ans = refine(a_guess, possible_ans)
  103.     for index in possible_ans:
  104.         print index,
  105.     print ' '
  106.     a_guess = pick_ans(possible_ans)
  107.     print 'My new guess is', a_guess
  108.  
  109. print 'Ans is', q_ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement