fork download
  1. from collections import deque
  2.  
  3. def get_card_value(val_str):
  4. """Convert card symbols to numeric values."""
  5. if val_str == 'A': return 1
  6. if val_str == 'J': return 11
  7. if val_str == 'Q': return 12
  8. if val_str == 'K': return 13
  9. return int(val_str)
  10.  
  11. def solve_game():
  12. try:
  13. n = int(input().strip())
  14. p1 = deque()
  15. p2 = deque()
  16.  
  17. # Read Player 1 cards
  18. for _ in range(n):
  19. val, suit = input().split()
  20. p1.append((get_card_value(val), int(suit)))
  21.  
  22. # Read Player 2 cards
  23. for _ in range(n):
  24. val, suit = input().split()
  25. p2.append((get_card_value(val), int(suit)))
  26.  
  27. pile = deque()
  28. turn = 0 # 0 = player1, 1 = player2
  29.  
  30. while p1 and p2:
  31. if turn == 0:
  32. card = p1.popleft()
  33. else:
  34. card = p2.popleft()
  35.  
  36. pile.append(card)
  37.  
  38. # Check if top two cards have the same value
  39. if len(pile) >= 2 and pile[-1][0] == pile[-2][0]:
  40. if pile[-1][1] < pile[-2][1]:
  41. winner = 0 if turn == 0 else 1
  42. else:
  43. winner = 1 if turn == 0 else 0
  44.  
  45. if winner == 0:
  46. p1.extend(pile)
  47. else:
  48. p2.extend(pile)
  49. pile.clear()
  50.  
  51. turn = 1 - turn # alternate turns
  52.  
  53. if len(p1) > len(p2):
  54. print("WINNER")
  55. elif len(p2) > len(p1):
  56. print("LOSER")
  57. else:
  58. print("TIE")
  59.  
  60. except Exception as e:
  61. # For debugging in Codetantra, print this line (comment it out after)
  62. # print("Error:", e)
  63. pass # avoid crashing on empty input
  64.  
  65. if __name__ == "__main__":
  66. solve_game()
  67.  
Success #stdin #stdout 0.11s 14008KB
stdin
5

K 2 Q 4

A 4 4 1

6 2 Q 1

4 2 3 1

5 1 5 2

2 4 3 1
stdout
Standard output is empty