mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 08:41:20 +00:00
Problem is obviously way more complex than expected. Changing to a more complex structure. Still in refactoring
This commit is contained in:
100
e_54.py
100
e_54.py
@@ -15,6 +15,48 @@ is a clear winner.
|
||||
How many hands does Player 1 win?
|
||||
'''
|
||||
|
||||
card_order = [1, 2, 3]
|
||||
|
||||
def enum(*sequential, **named):
|
||||
enums = dict(zip(sequential, range(len(sequential))), **named)
|
||||
reverse = dict((value, key) for key, value in enums.iteritems())
|
||||
enums['reverse_mapping'] = reverse
|
||||
return type('Enum', (), enums)
|
||||
|
||||
class Poker:
|
||||
_ranks=enum("High_Card",
|
||||
"One_Pair",
|
||||
"Two_Pairs",
|
||||
"Three_of_a_Kind",
|
||||
"Straight",
|
||||
"Flush",
|
||||
"Full_House",
|
||||
"Four_of_a_Kind",
|
||||
"Straight",
|
||||
"Flush",
|
||||
"Royal_Flush")
|
||||
|
||||
class PokerGame:
|
||||
"""
|
||||
A game is defined as two players having a hand of 5 cards each.
|
||||
"""
|
||||
def __init__(self, hand_1, hand_2):
|
||||
self.hand_1 = hand_1
|
||||
self.hand_2 = hand_2
|
||||
|
||||
class Hand:
|
||||
def __init__(self, cards):
|
||||
self.cards = cards # should be 5
|
||||
|
||||
class Card:
|
||||
_values = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
|
||||
_colors = ["H", "C", "S", "D"]
|
||||
def __init__(self, value, color):
|
||||
self.value = value
|
||||
self.color = color
|
||||
|
||||
##
|
||||
|
||||
def load_data(filename):
|
||||
"""
|
||||
Loads the given file according to the problem description.
|
||||
@@ -38,32 +80,60 @@ def load_data(filename):
|
||||
file.close()
|
||||
return hands
|
||||
|
||||
def sort_n_strip(hands):
|
||||
def create_games(data):
|
||||
"""
|
||||
As description indicates data is correct, we can ignore the color of the
|
||||
cards. (and hence strip the value)
|
||||
We also want to sort the players hands (to be worked with easier later)
|
||||
Transforms a list of lists of poker cards into proper Poker games
|
||||
"""
|
||||
# just keeping the first element of the cards
|
||||
stripped_hands = [[[val[0] for val in h] for h in hand] for hand in hands]
|
||||
def create_hand(cards):
|
||||
"""
|
||||
Create a poker hand out of a list of cards
|
||||
"""
|
||||
if len(cards) != 5:
|
||||
raise AttributeError("Expecting 5 cards!")
|
||||
|
||||
sorted_hands = stripped_hands
|
||||
poker_cards = [Card(card[0], card[1]) for card in cards]
|
||||
return Hand(poker_cards)
|
||||
|
||||
games = [PokerGame(create_hand(game[0]), create_hand(game[1])) for game in data]
|
||||
return games
|
||||
|
||||
|
||||
def sort_hands(hands):
|
||||
#FIXME: Not working. To refactor
|
||||
def sort_hand(hand):
|
||||
"""
|
||||
Sorts a hand of cards in descending order.
|
||||
There should be no reference to card color
|
||||
"""
|
||||
sorted_hand = list(hand)
|
||||
sorted_hand.sort(key=lambda x: card_order.index(x[0]), reverse=True)
|
||||
return sorted_hand
|
||||
sorted_hands = [[sort_hand(h) for h in hand] for hand in hands]
|
||||
#need to sort now
|
||||
return sorted_hands
|
||||
|
||||
def winning_hands(filename):
|
||||
def winning_hands(filename, player=1):
|
||||
"""
|
||||
Returns the first of the firs num consecutive primes.
|
||||
Returns the number of winning hands for player 1 or 2
|
||||
"""
|
||||
hands = load_data(filename)
|
||||
data = load_data(filename)
|
||||
games = create_games(data)
|
||||
|
||||
data = sort_n_strip(hands)
|
||||
print hands
|
||||
print data
|
||||
#data = sort_hands(hands)
|
||||
#print hands
|
||||
|
||||
#for val in hands:
|
||||
# player_hand = data[0][0]
|
||||
# player_card = player_hand[0]
|
||||
|
||||
#print val
|
||||
# card = Card(player_card[0], player_card[1])
|
||||
# print card.value
|
||||
# print card.color
|
||||
|
||||
# print player_hand
|
||||
|
||||
# print Hand.Values.reverse_mapping['K']
|
||||
# we want to knwo what is his rank
|
||||
#get_rank(player_hand)
|
||||
|
||||
if __name__ == '__main__':
|
||||
winning_hands("./e_54_poker.txt")
|
||||
|
||||
Reference in New Issue
Block a user