Sythe.Org Forums     Register     FAQ     Members List     Calendar     Mark Forums Read    
 
Sythe.Org Forums  
   Runescape Gold

Sythe.org — A Virtual Goods Trading Hub

Make real cash! buying and selling in-game items.

We have a no-scam policy.

You can make thousands playing your favourite games here at Sythe.org.

Just sign up an account and follow the rules!


Take me to

Runescape Markets

Other Game Markets

Support Center

Register an Account

Close
Python
Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 03-08-2012, 01:07 PM
Forum Addict
 
Join Date: Feb 2012
Posts: 391
Default 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
Reply With Quote
  #2  
Old 03-08-2012, 03:40 PM
novice's Avatar
Active Member
 
Join Date: Jun 2008
Posts: 111
Default Re: Python

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

combinedHand = list()

for suit in hand:
    combinedHand += suit

combinedHand.sort(reverse=True)

print combinedHand[0]
Reply With Quote
  #4  
Old 03-10-2012, 03:32 AM
Guru
 
Join Date: Jan 2007
Location: /home/Canada
Posts: 1,768
Default Re: 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])
Reply With Quote
Reply



Cheap RS Gold Store  Runescape Gold

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT +1. The time now is 04:43 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.1