Page 1 of 1

Solved in python please Objectives:  Apply simple data structures to a common problem: strings, lists, dictionaries, tu

Posted: Sat May 14, 2022 7:09 pm
by answerhappygod
Solved in python please
Objectives:
 Apply simple data structures to a common problem: strings, lists,
dictionaries, tuples
 Iterate over sequential data structures
 Write functions that accept, mutate and return new
structures
 Practice functional decomposition
 Write legible, concise code with proper documentation
 Use debugging tools
 Following instructions closely  MOST IMPORTANT!!
Important Concepts:
 Basic Data Structures: String, List, Tuple, Dictionary
 Ranged (indexed) for loops: for i in range(len(structure))
 For each loops (non-indexed): for item in structure:
Card Games
Card games provide excellent practice for dealing with data
structures. Let’s go through some
exercises to build up the necessary objects involved in programming
a card game. We will end
with the rudiments for Poker
The Card:
For our purposes we will
represent a playing card with a
tuple of two values: rank and
suit
Examining the cards to the right
we can see that
 Suit: Hearts
 Rank: {1 – 10} counting
the Ace card as 1
 Face Card Ranks:
o Jack = 11
o Queen = 12
o King = 13
 To make our cards easy to organize and test we will define them
as a tuple of two
numbers. Remember, tuples are immutable and cannot be changed. This
works well for
our game as we would not want to risk the chance of a card being
mutated. Since a
tuple is a single object it can be placed in a list, passed to a
function, returned from a
function . . . etc. This organization is just the underlying
methodology for the program to
store and process the cards. As you’ll see we will be outputting
the cards with more
descriptive information.
poker_hand_generator:
# TESTS
'''
SUITS => {0:"Hearts", 1:"Diamonds", 2:"Clubs",
3:"Spades"} dict to map card suit to hand suit
RANKS => (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
tuple of card ranks
CARD => (rank,suit)

card is a tuple of
rank and suit
DECK => [(rank,suit), (rank,suit), (rank,suit) . . .
etc] list of cards
HAND => {0:[], 1:[], 2:[], 3:[]}

hands are organized by suit
HAND RANKS
1. Straight Flush: All five cards are the same suit and in
order
2. Four of a Kind: Four Cards of the same rank
3. Full House: Three Cards of one rank and
two of another
4. Flush: Five cards of the
same suit but not in order
5. Straight: Five Cards in sequence
but not the same suit
6. Three of a Kind: Three Cards of the same rank
7. Two Pair: A pair is two cards of
the same rank. Two pair is two collections of pairs
8. One Pair: Two cards of the same
rank
9. No combination: Ranked by highest card
'''
import random
def empty_hand():
return {0:[], 1:[], 2:[], 3:[]}
def generate_two_pair():
# setup
h = empty_hand()
rank = random.randint(1, 13)
used = []
ranks = [rank]
# generate first pair
while len(used) < 2:
suit = random.randint(0, 3)
if (rank, suit) not in used:
h[suit].append((rank,
suit))
used.append((rank,
suit))
# pick a new rank, make sure it's new
rank = random.randint(1, 13)
while rank in ranks:
rank = random.randint(1, 13)
ranks.append(rank)
# generate second pair
while len(used) < 4:
suit = random.randint(0, 3)
if (rank, suit) not in used:
h[suit].append((rank,
suit))
used.append((rank,
suit))
# pick last card, must not have been used
while len(used) < 5:
suit = random.randint(0, 3)
rank = random.randint(1, 13)
if (rank, suit) not in used and rank
not in ranks:
h[suit].append((rank,
suit))
used.append((rank,
suit))
return h
def generate_N_of_a_kind(n):
# setup
h =
empty_hand()
rank =
random.randint(1, 13)
used_card = []
used_rank = [rank]
# generate N cards of same rank
while len(used_card) < n:
suit = random.randint(0, 3)
if (rank, suit) not in
used_card:
h[suit].append((rank,
suit))
used_card.append((rank, suit))
# generate remaining (5 - N) cards, must not have
been used
while len(used_card) < 5:
rank = random.randint(1, 13)
suit = random.randint(0, 3)
if (rank, suit) not in used_card and
rank not in used_rank:
h[suit].append((rank,
suit))
used_card.append((rank, suit))
used_rank.append(rank)
return h
def generate_straight():
# setup
h = empty_hand()
rank = random.randint(1, 8)
# generate a sequence of cards of same rank
for i in range(5):
suit = random.randint(0, 3)
h[suit].append((rank, suit))
rank += 1
return h
def generate_flush():
# setup
h = empty_hand()
suit = random.randint(0, 3)
used = []
# generate five cards of same suit, must be
unique
while len(used) < 5:
rank = random.randint(1, 13)
if rank not in used:
h[suit].append((rank,
suit))
used.append(rank)
return h
def generate_full_house():
# setup
h = empty_hand()
rank = random.randint(1, 13)
suit = random.randint(0, 3)
ranks = [rank]
# generate 3 cards of same rank
for i in range(3):
h[suit].append((rank, suit))
suit = (suit + 1) % 4
# pick a new rank, must be new
rank = random.randint(1, 13)
while rank in ranks:
rank = random.randint(1, 13)
# generate remaining 2 cards of new rank
suit = random.randint(0, 3)
for i in range(2):
h[suit].append((rank, suit))
suit = (suit + 1) % 4
return h
def generate_straight_flush():
# setup
h = empty_hand()
suit = random.randint(0, 3)
rank = random.randint(1, 8)
# generate sequence of five cards in same suit
for i in range(5):
h[suit].append((rank, suit))
rank += 1
random.shuffle(h[suit])
return h