Title: a deck of playing cards is divided into 13 kinds of cards, each of which has 4 suits, with a total of 52 cards. It is stipulated that the order from small to large is A23456789TJQK. Now 20 cards are selected from a deck. One of the following rules is selected for each round: 1. Single card - any card. 2. Pair - two cards of the same size. 3. Three belts - three cards of the same size, with at most one free card attached. 4. Four belts -- four cards of the same size, with up to two free cards attached. 5. Shunzi -- at least five consecutive cards. How many rounds will it take to finish 20 cards.
# 8K67A65K27T59K346AK2 # 789TJQK789TJQK789TJQ # 12345123451234512345 # 12345678912345678912 # 56789567891234523456 #classList = input() classList = '789TJQK789TJQK789TJQ' def pai_num(classList): # Returns the element and the number of occurrences classCount = {} # 1 count the number of occurrences of elements in the list and store them in the dictionary for vote in classList: # Code ATJQK to integer range if vote == 'A': vote = '1' if vote == 'T': vote = '10' if vote == 'J': vote = '11' if vote == 'Q': vote = '12' if vote == 'K': vote = '13' classCount[int(vote)] = classCount.get(int(vote), 0) + 1 # Instead of the following three lines of code # classCount={'c': 1, 'b': 3, 'a': 2} # 2 sort according to the key or value in the dictionary # e[0] sorts by key, e[1] sorts by value; default is ascending sort, reverse=Fault # Return to the form of tuple list [('b ', 3), ('a', 2), ('c ', 1]] sortedClassCount1 = sorted(classCount.items(), key=lambda e: e[0], reverse=True) # Sort by key value (drop) return sortedClassCount1 # Consequent checking function # Parameter: a1 ---- list tuple element to be searched, format: a1 = [('7 ', 2), ('6', 3), ('5 ', 2), ('4', 1), ('3 ', 1]] # Parameter: num ﹣ Shun ---- the number of consecutive CIS # Return ---- if the matching sequence is found, return (True, remove the matching list tuple). If the matching sequence is not found, return False def shun(a1): # a1 the object to be looked up as a child, num1 = len(a1) # Give the length of the dictionary #print(num1) Num = 0 # Record the number of sequential children i1 = 0 # Next index location i2 = 0 # Index position of the last sequence selected by i2 record i22 = 0 A ={} # Create a new dictionary B={} C=[] B_num =0 for i in range(num1-1): # Given the length, find the sequence, and assign the last index value of the target quantity sequence to i2 q = a1[i][0] i1 = i + 1 q1 = a1[i1][0] if (int(q) - int(q1)) == 1: # Judge whether the front and back are in order Num += 1 if Num == 1: # Index of record sequence start position i22 = i if Num >= 4: B[i22] = B.get(i22,0) B[i22] =Num+1 # B (first index of sequential sub: number of sequential sub) - Dictionary else: i22 = 0 Num = 0 #print('B',B) for i in range(num1): A[int(a1[i][0])] = A.get(int(a1[i][0]), a1[i][1]) for key, value in B.items(): #print(key, value) for i in range(num1): if i >= key and i <= (key+value-1): # Deal with shunzi A[int(a1[i][0])] -= 1 if A[int(a1[i][0])] == 0: del A[int(a1[i][0])] sortedClassCount1 = sorted(A.items(), key=lambda e: e[0], reverse=True) # How to convert to tuple list B_num = len(B) return B_num,sortedClassCount1 def pai_duizi(yu_pai): # Four generation two (two different cards are preferred), three generation one, one num_1 = 0 num_2 = 0 num_3 = 0 num_4 = 0 num = len(yu_pai) for i in yu_pai: if i[1] == 1: num_1 += 1 if i[1] == 2: num_2 += 1 if i[1] == 3: num_3 += 1 if i[1] == 4: num_4 += 1 #if (num_1+num_2*2) >= (num_3+num_4*2): if num_1-num_3 >= 0: num_1 = num_1 -num_3 if num_1 >= num_4 * 2: num_1 = num_1 - num_4 * 2 return num_1 + num_2 + num_3 + num_4 else: num_1 = num_1 - (num_1//2)*2 num_44 = num_4 - (num_1//2) if num_2-num_44 >= 0: num_2 = num_2-num_4 return num_1 + num_2 + num_3 + num_4 else: return num_1 + num_3 + num_4 else: if num_2-num_4 >= 0: num_2 = num_2 - num_4 return num_2 + num_3 + num_4 else: return num_3 + num_4 def chu_pai(a1): p_num, yu_pa = shun(a1) if p_num > 0: p_num1, yu_pa1 = shun(yu_pa) if p_num1 > 0: p_num2, yu_pa2 = shun(yu_pa1) if p_num2 > 0: p_num3, yu_pa3 = shun(yu_pa2) if p_num3 > 0: p_num4, yu_pa4 = shun(yu_pa3) if p_num4 > 0: p_num4, yu_pa4 = shun(yu_pa3) else: return pai_duizi(yu_pa3) + p_num + p_num1 + p_num2+ p_num3 else: return pai_duizi(yu_pa3) + p_num + p_num1 + p_num2 else: return pai_duizi(yu_pa2) + p_num + p_num1 else: return pai_duizi(yu_pa1) + p_num else: return pai_duizi(yu_pa) a1 = pai_num(classList) # a1 sorts (drops) by key value, a0 sorts (drops) by value print(a1) print(chu_pai(a1)) # Minimum number of rounds required to output 20 cards ''' //Output: [(13, 2), (12, 3), (11, 3), (10, 3), (9, 3), (8, 3), (7, 3)] 3 '''