Advertisement
danchaofan

Euler #54

Dec 8th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. def convert(letter):
  2.     if letter in "TJQKA":
  3.         return 10 + "TJQKA".index(letter)
  4.     else:
  5.         return int(letter)
  6.  
  7.  
  8. def straight(n):
  9.     nn = sorted(n)
  10.     for x in range(4):
  11.         if nn[x] + 1 != nn[x+1]:
  12.             return False
  13.     return True
  14.  
  15.  
  16. def rank(hand):
  17.     nums, suits = [], []
  18.     for x in hand:
  19.         nums.append(convert(x[0]))
  20.     for x in hand:
  21.         suits.append(x[1])
  22.     print(sorted(nums), sorted(suits))
  23.     if sorted(nums) == [10, 11, 12, 13, 14] and len(set(suits)) == 1:
  24.         return 9
  25.     elif len(set(suits)) == 1 and straight(nums):
  26.         return 8, sorted(nums)[4]
  27.     elif len(set(sorted(nums)[:4])) == 1 or len(set(sorted(nums)[1:])) == 1:
  28.         if sorted(nums)[0] == sorted(nums)[1]:
  29.             return 7, sorted(nums)[0]
  30.         else:
  31.             return 7, sorted(nums)[1]
  32.     elif (len(set(sorted(nums)[:3])) == 1 or len(set(sorted(nums)[2:])) == 1) and len(set(sorted(nums))) == 2:
  33.         if sorted(nums)[1] != sorted(nums)[2]:
  34.             return 6, sorted(nums)[2]
  35.         else:
  36.             return 6, sorted(nums)[0]
  37.     elif len(set(suits)) == 1:
  38.         return 5, sorted(nums)[4]
  39.     elif straight(nums):
  40.         return 4, sorted(nums)[4]
  41.     elif len(set(sorted(nums)[:3])) == 1 or len(set(sorted(nums)[1:4])) == 1 or len(set(sorted(nums)[2:])) == 1:
  42.         for x in range(2, 15):
  43.             count = 0
  44.             for y in sorted(nums):
  45.                 if x == y:
  46.                     count += 1
  47.                 if count == 3:
  48.                     return 3, x
  49.     elif len(set(sorted(nums))) == 3:
  50.         return 2, sorted(nums)[3]
  51.     elif len(set(sorted(nums))) == 4:
  52.         for x in range(2, 15):
  53.             count = 0
  54.             for y in sorted(nums):
  55.                 if x == y:
  56.                     count += 1
  57.                 if count == 2:
  58.                     return 1, x
  59.     else:
  60.         return 0, sorted(nums)[4]
  61.  
  62.  
  63. file = list(open("poker.txt"))
  64. newfile = []
  65. for a in file:
  66.     newfile.append(a.rstrip())
  67. file = []
  68. for b in newfile:
  69.     temp = []
  70.     tempstr = ""
  71.     for c in b:
  72.         tempstr += c
  73.         if c == " ":
  74.             temp.append(tempstr)
  75.             tempstr = ""
  76.             continue
  77.     temp.append(tempstr)
  78.     file.append(temp)
  79.  
  80. answer, index = 0, 0
  81. for d in file:
  82.     index += 1
  83.     print(index)
  84.     print(d[:5], d[5:])
  85.     if list(rank(d[:5]))[0] > list(rank(d[5:]))[0]:
  86.         answer += 1
  87.         print("Won")
  88.     elif list(rank(d[:5]))[0] == list(rank(d[5:]))[0]:
  89.         if list(rank(d[:5]))[1] > list(rank(d[5:]))[1]:
  90.             answer += 1
  91.             print("Won")
  92.     print(list(rank(d[:5])), list(rank(d[5:])))
  93. print(answer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement