Beginner Question

Kevin Reeder avail at uponrequest.org
Fri Mar 28 03:13:06 CET 2003


I'm working through the online Python beginner's guide "How to Think Like a
Computer Scientist" and am stumped on one of the exercises. A
function to shuffle cards within a class definition called Deck appears
as follows:

def shuffle(self):
    import random
    nCards = len(self.cards)
    for i in range(nCards):
      j = random.randrange(i, nCards)
      self.cards[i], self.cards[j] = self.cards[j], self.cards[i]

The problem is to rewrite the last line without using a sequence
assignment. Everything I've thought of has the result of only inserting
the random integer j at each index which doesn't solve the problem. I'm
not really asking for the "right" answer as much as something to jog my
thought in the right direction!

If you're interested to reference the text:

http://www.ibiblio.org/obp/thinkCSpy/chap15.htm#7

TIA,

Kevin




More information about the Python-list mailing list