fork(6) download
  1. from collections import defaultdict
  2. def cardflip( cards ):
  3. # 'tops' maps the value to the count of cards
  4. # showing that value on top
  5. tops = defaultdict(int)
  6. # 'bottoms' maps the value to the count of cards
  7. # showing that value on bottom
  8. bottoms = defaultdict(int)
  9. for c in cards:
  10. topvalue = c[0]
  11. tops[topvalue] += 1
  12. bottomvalue = c[1]
  13. if bottomvalue != topvalue:
  14. # if it's the same on both side we ignore the bottom
  15. bottoms[bottomvalue] += 1
  16. tops[bottomvalue] += 0
  17. # topcounts is a list of pairs (value, count)
  18. topcounts = list(tops.items())
  19. # sort it by count, the biggest first
  20. topcounts.sort( key = lambda x : -x[1] )
  21. for (v, c) in topcounts:
  22. if (c + bottoms[v]) * 2 >= len(cards):
  23. final = v
  24. break
  25. else:
  26. print( "there is no result" )
  27. return
  28.  
  29. print( "value=", final, "moves=", (len(cards)+1)//2 - tops[final] )
  30.  
  31.  
  32. cardflip( [ (3,10), (10,3), (5,4) ] )
  33. cardflip( [ (1,3), (2,3), (4, 5), (6, 7), (9,3), (11,2)] )
Success #stdin #stdout 0.11s 10096KB
stdin
Standard input is empty
stdout
value= 10 moves= 1
value= 3 moves= 3