fork download
  1. import re
  2.  
  3. P = ['abcde', 'baedc', 'cdaeb', 'dbaec', 'edbca']
  4. N = len(P)
  5.  
  6. next = lambda x: chr(ord(x)+1)
  7. h2h_winner = lambda x, y, w: x if sum([w[i] for i in range(N) if re.match('.*' + x + '.*' + y + '.*', P[i])]) > N/2 else y
  8. winner = lambda x, y, w: h2h_winner(x, y, w) if y == chr(ord('a')+N-1) else h2h_winner(winner(x, next(y), w), winner(y, next(y), w), w)
  9.  
  10. w = [1] * N
  11. print "Winner:", winner('a', 'b', w)
  12. w[0] = 0
  13. for i in range(1, N):
  14. w[i] = 2
  15. print "Winner if a's vote is transferred to", chr(ord('a')+i) + ":", winner('a', 'b', w)
  16. w[i] = 1
  17.  
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
Winner: d
Winner if a's vote is transferred to b: b
Winner if a's vote is transferred to c: d
Winner if a's vote is transferred to d: d
Winner if a's vote is transferred to e: a