We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,567 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

card game

How to write a function for highest denomination of any card in the hand.The denominations from lowest to highest are 2, 3,4,5,6,7,8,9,10,'jack,''queen','king','ace'.For example,
highestDenomination({(6,'club'),(6,'heart'),('queen','heart'),('queen','diamond'),(2,'heart')})denotes‘queen’.

3
Contributors
2
Replies
1 Hour
Discussion Span
6 Months Ago
Last Updated
5
Views
Question
Answered
dtripath
Newbie Poster
1 post since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Do dictionary mapping the denominations to their numeric value.

pyTony
pyMod
Moderator
6,299 posts since Apr 2010
Reputation Points: 879
Solved Threads: 984
Skill Endorsements: 26

Here is another approach ...

'''card_deck_5.py
create a deck of cards, shuffle and draw a hand of five cards
suit: club=C, diamond=D, heart=H spade=S
rank: ace=A, 10=T, jack=J, queen=Q, king=K, numbers=2..9
ace of spade would be AS, 8 of heart would be 8H and so on ...
'''

import random

def create_deck():
    """
    create a full deck of cards as a list of codes
    """
    ranks = "A23456789TJQK"
    suits = "CDHS"
    deck = [rank + suit for rank in ranks for suit in suits]
    return deck

def shuffle_deck(deck):
    random.shuffle(deck)
    return deck

deck = create_deck()
shuff = shuffle_deck(deck)
# pull five cards and show
card5 = shuff[:5] 
print(card5)
print('-'*30)
# sorted by rank
print(sorted(card5))
# sorted by suit
print(sorted(card5, key=lambda x: x[1]))

'''possible result ...
['4S', '2D', '2H', 'QS', '4D']
------------------------------
['2D', '2H', '4D', '4S', 'QS']
['2D', '4D', '2H', '4S', 'QS']
'''
vegaseat
DaniWeb's Hypocrite
Moderator
6,464 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,607
Skill Endorsements: 34
Question Answered as of 6 Months Ago by vegaseat and pyTony

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0715 seconds using 2.72MB