• Log in or sign up for an account here: https://www.sythe.org/login

Python

asdasd123

Forum Addict
Joined
Feb 2, 2012
Sythe Gold
0
Messages
417
Python
I'm a beginner at python can someone help me out it's for a course at uni and I keep trying to use VB commands lol

# Playing Cards - Poker
#
# THE PROBLEM
#
# Assume the following value has already been entered into the
# Python interpreter, representing a hand of playing cards -
# where picture cards have the following values:
# Ace = 1; Jack = 11; Queen = 12; King = 13
#
# The cards are grouped into the four suits (Hearts, Diamonds,
# Clubs, Spades, in that order) using sub-lists of the hand.

hand = [[5], [], [6, 8], [4, 7]]

# Write an expression, or expressions, to find the card with the
# highest face value in the hand, regardless of suit,
# and print its value.
#
# (Motivation: Sometimes in poker it is necessary to compare players'
# largest cards in order to determine the winning hand.)


# A SOLUTION
#
# Problem solving strategy:
# 1. Combine the sublists into one list
# 2. Find the highest card
# 3. Print that card's value
 
Python
Since finding the highest card really only requires traversing the list once which is O(n) it's better to do that than it is to use sort which probably requires O(n*log(n)) time.

You could find the max value yourself by looping over the list but Python provides the function max() to do it for you.

Also (double) list comprehension can be used to build one array of all the card values in one line.

So the whole thing can actually be done using just this:

Code:
print max([card for suit in hand for card in suit])
 
Runescape Gamblingrunescape gambling
Back
Top