We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importcollectionsnumbers=['2','3','4','5','6','7','8','9','T','J','Q','K','A']#numbers_2=['A','2','3','4','5','6','7','8','9','T','J','Q','K']defranker(cards):suits=[i[-1]foriincards]iflen(set(suits))==1:same_suit=Trueelse:same_suit=Falsevalues=[numbers.index(i[0])foriincards]# values_2=[numbers_2.index(i[0]) for i in cards]values.sort(reverse=True)ifvalues==[12,3,2,1,0]:ifsame_suit:return(8,3)else:return(4,3)# values_2.sort(reverse=True)differences=[values[i]-values[i+1]foriinrange(4)]groups=[]# I could have grouped by using itertools.groupby but I wanted to write it alorithmicallydifferences.append(-1)#pre-processing to obtain last interval toostart=0foriinrange(1,len(differences)):ifdifferences[i]!=differences[i-1]:groups.append((differences[i-1],i-start))start=iif(1,4)ingroups:consecutive_values=Trueelse:consecutive_values=Falsevalues_same=[i[0]foriincollections.Counter(values).most_common()]#this is cooolifsame_suitandconsecutive_values:return(8,max(values))elif(0,3)ingroups:# if values[0]!=values[1]:# if values[0]!=values[2]:# ans=(values[1],values[0])# else:# ans=(values[0],values[1])# else:# ans=(values[0],int(set(values).difference([values[0]])))# return (7,*ans)return(7,*values_same)elif(0,1)ingroupsand(0,2)ingroups:return(6,*values_same)elifsame_suit:return(5,*values)elifconsecutive_values:return(4,max(values))elif(0,2)ingroups:return(3,*values_same)elifgroups.count((0,1))==2:return(2,*values_same)elif(0,1)ingroups:return(1,*values_same)else:return(0,*values)defsorter(pl1,pl2):size=min(len(pl1),len(pl2))foriinrange(size):ifpl1[i]>pl2[i]:return'Player 1'elifpl1[i]<pl2[i]:return'Player 2'for_inrange(int(input())):cards=input().split()pl1=cards[:5]pl2=cards[5:]print(sorter(ranker(pl1),ranker(pl2)))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #54: Poker hands
You are viewing a single comment's thread. Return to all comments →
100/- points python 3