Renamed all folders

This commit is contained in:
2021-04-16 16:58:48 +02:00
parent dbc6cfef05
commit 197b3eea8a
62 changed files with 0 additions and 0 deletions

270
054/main.py Normal file
View File

@@ -0,0 +1,270 @@
'''
In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
High Card: Highest value card.
One Pair: Two cards of the same value.
Two Pairs: Two different pairs.
Three of a Kind: Three cards of the same value.
Straight: All cards are consecutive values.
Flush: All cards of the same suit.
Full House: Three of a kind and a pair.
Four of a Kind: Four cards of the same value.
Straight Flush: All cards are consecutive values of same suit.
Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.
Consider the following five hands dealt to two players:
Hand Player 1 Player 2 Winner
1 5H 5C 6S 7S KD
Pair of Fives
2C 3S 8S 8D TD
Pair of Eights
Player 2
2 5D 8C 9S JS AC
Highest card Ace
2C 5C 7D 8S QH
Highest card Queen
Player 1
3 2D 9C AS AH AC
Three Aces
3D 6D 7D TD QD
Flush with Diamonds
Player 2
4 4D 6S 9H QH QC
Pair of Queens
Highest card Nine
3D 6D 7H QD QS
Pair of Queens
Highest card Seven
Player 1
5 2H 2D 4C 4D 4S
Full House
With Three Fours
3C 3D 3S 9S 9D
Full House
with Three Threes
Player 1
The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.
How many hands does Player 1 win?
'''
import time
import math
from enum import IntEnum, Enum, auto
class PokerHand(IntEnum):
HIGH = 0
PAIR = 1
TWOPAIR = 2
THREEOFAKIND = 3
STRAIGHT = 4
FLUSH = 5
FULLHOUSE = 6
FOUROFAKIND = 7
STRAIGHTFLUSH = 8
ROYALFLUSH = 9
class Suit(Enum):
HEARTS = auto()
CLUBS = auto()
SPADES = auto()
DIAMONDS = auto()
class Value(IntEnum):
ONE = 1
TWO = 2
THREE = 3
FOUR = 4
FIVE = 5
SIX = 6
SEVEN = 7
EIGHT = 8
NINE = 9
TEN = 10
JACK = 11
QUEEN = 12
KING = 13
ACE = 14
def equal(l):
return all(value == l[0] for value in l)
def freqTable(l):
table = {}
for num in l:
if num in table.keys():
table[num] += 1
else:
table[num] = 1
return table
def textToCard(s):
suit = None
value = None
if s[1] == "H":
suit = Suit.HEARTS
elif s[1] == "C":
suit = Suit.CLUBS
elif s[1] == "S":
suit = Suit.SPADES
else:
suit = Suit.DIAMONDS
if s[0].isnumeric():
value = Value(int(s[0]))
elif s[0] == "T":
value = Value.TEN
elif s[0] == "J":
value = Value.JACK
elif s[0] == "Q":
value = Value.QUEEN
elif s[0] == "K":
value = Value.KING
else:
value = Value.ACE
return suit, value
def classifyHand(hand):
sameSuit = equal([suit for (suit,_) in hand])
values = [value for (_,value) in hand]
values.sort()
if set(values) == set([Value.ACE, Value.KING, Value.QUEEN, Value.JACK, Value.TEN]) and sameSuit:
return PokerHand.ROYALFLUSH
elif all(dif == 1 for dif in [values[i+1] - values[i] for i in range(4)]) and sameSuit:
return PokerHand.STRAIGHTFLUSH
elif equal(values[:4]) or equal(values[1:]):
return PokerHand.FOUROFAKIND
elif (equal(values[:3]) and equal(values[3:])) or (equal(values[2:]) and equal(values[:2])):
return PokerHand.FULLHOUSE
elif sameSuit:
return PokerHand.FLUSH
elif all(dif == 1 for dif in [values[i+1] - values[i] for i in range(4)]):
return PokerHand.STRAIGHT
elif equal(values[:3]) or equal(values[1:4]) or equal(values[2:]):
return PokerHand.THREEOFAKIND
elif (equal(values[:2]) and (equal(values[2:4]) or equal(values[3:]))) or (equal(values[1:3]) and equal(values[3:])):
return PokerHand.TWOPAIR
elif equal(values[:2]) or equal(values[1:3]) or equal(values[2:4]) or equal(values[3:]):
return PokerHand.PAIR
return PokerHand.HIGH
def winner(s1, s2):
hand1 = [textToCard(s) for s in s1]
hand2 = [textToCard(s) for s in s2]
result1 = classifyHand(hand1)
result2 = classifyHand(hand2)
if result1 != result2:
return result1 > result2
values1 = [value for (_,value) in hand1]
values2 = [value for (_,value) in hand2]
values1.sort(reverse=True)
values2.sort(reverse=True)
table1 = freqTable(values1)
table2 = freqTable(values2)
if result1 == PokerHand.FOUROFAKIND:
four1 = None
four2 = None
for key in table1.keys():
if table1[key] == 4:
four1 = key
for key in table2.keys():
if table2[key] == 4:
four2 = key
return four1 > four2
elif result1 == PokerHand.FULLHOUSE:
three1 = None
three2 = None
for key in table1.keys():
if table1[key] == 3:
three1 = key
for key in table2.keys():
if table2[key] == 3:
three2 = key
return three1 > three2
elif result1 == PokerHand.THREEOFAKIND:
three1 = None
three2 = None
for key in table1.keys():
if table1[key] == 3:
three1 = key
for key in table2.keys():
if table2[key] == 3:
three2 = key
return three1 > three2
elif result1 == PokerHand.TWOPAIR:
one1 = None
one2 = None
for key in table1.keys():
if table1[key] == 1:
one1 = key
for key in table2.keys():
if table2[key] == 1:
one2 = key
pairs1 = values1.copy()
pairs1.remove(one1)
pairs1.sort(reverse=True)
pairs2 = values2.copy()
pairs2.remove(one2)
pairs2.sort(reverse=True)
if pairs1 == pairs2:
return one1 > one2
else:
return pairs1 > pairs2
elif result1 == PokerHand.PAIR:
pair1 = None
pair2 = None
for key in table1.keys():
if table1[key] == 2:
pair1 = key
for key in table2.keys():
if table2[key] == 2:
pair2 = key
rest1 = values1.copy()
rest1.remove(pair1)
rest1.remove(pair1)
rest1.sort(reverse=True)
rest2 = values2.copy()
rest2.remove(pair2)
rest2.remove(pair2)
rest2.sort(reverse=True)
if pair1 == pair2:
return rest1 > rest2
else:
return pair1 > pair2
return values1 > values2
def main():
print("Hello, this is Patrick")
t0 = time.time()
counter = 0
with open("54/poker.txt", 'r') as reader:
for line in reader:
words = line.split()
counter += winner(words[:5], words[5:])
print(counter, time.time() - t0)
if __name__ == "__main__":
main()