## Question

## Transcribed Text

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

"""Poker game

"""

import cards

def less_than(c1,c2):

'''Return

True if c1 is smaller in rank,

True if ranks are equal and c1 has a 'smaller' suit

False otherwise'''

if c1.rank() < c2.rank():

return True

elif c1.rank() == c2.rank() and c1.suit() < c2.suit():

return True

return False

def min_in_list(L):

'''Return the index of the mininmum card in L'''

min_card = L[0] # first card

min_index = 0

for i,c in enumerate(L):

if less_than(c,min_card): # found a smaller card, c

min_card = c

min_index = i

return min_index

def cannonical(H):

''' Selection Sort: find smallest and swap with first in H,

then find second smallest (smallest of rest) and swap with second in H,

and so on...'''

for i,c in enumerate(H):

# get smallest of rest; +i to account for indexing within slice

min_index = min_in_list(H[i:]) + i

H[i], H[min_index] = H[min_index], c # swap

return H

def flush_7(H):

'''Return a list of 5 cards forming a flush,

if at least 5 of 7 cards form a flush in H, a list of 7 cards,

False otherwise.

'''

# 5 cards of the same suit

suits = {}

# make dictionary of same suit

for c in H:

if c.suit() in suits:

suits[c.suit()].append(c)

else:

suits[c.suit()] = [c]

if len(suits[c.suit()]) >= 5:

break

# return entry with 5 cards of same suit

for k,v in suits.items():

if len(v)>= 5:

return v

return False

def straight_7(H):

'''Return a list of 5 cards forming a straight,

if at least 5 of 7 cards form a straight in H, a list of 7 cards,

False otherwise.'''

# Sequence of 5 cards in increasing value

H = sorted(H, key=lambda x: x.rank())

for i in range(3):

cards = [H[i]]

for j in range(i+1,len(H)):

if j < len(H)-1:

if (H[j].rank() == H[j-1].rank()+1) and (H[j].rank() == H[j+1].rank()-1):

cards.append(H[j])

else:

if (H[j].rank() == H[j-1].rank()+1):

cards.append(H[j])

if len(cards)>=5:

return cards

return False

def straight_flush_7(H):

'''Return a list of 5 cards forming a straight flush,

if at least 5 of 7 cards form a straight flush in H, a list of 7 cards,

False otherwise.

'''

# Straight of the same suit

H = sorted(H, key=lambda x: x.rank())

if flush_7(H) == straight_7(H):

return flush_7(H)

return False

def four_7(H):

'''Return a list of 4 cards with the same rank,

if 4 of the 7 cards have the same rank in H, a list of 7 cards,

False otherwise.

'''

# four cards with the same value

ranks = {}

# make dictionary of same rank

for c in H:

if c.rank() in ranks:

ranks[c.rank()].append(c)

else:

ranks[c.rank()] = [c]

# return entry with 4 cards of same rank

for k,v in ranks.items():

if len(v)== 4:

return v

return False

def three_7(H):

'''Return a list of 3 cards with the same rank,

if 3 of the 7 cards have the same rank in H, a list of 7 cards,

False otherwise.

You may assume that four_7(H) is False....

By purchasing this solution you'll be able to access the following files:

Solution.py.