I'm trying to understand the following code:
import random
SUITS = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
def generate_deck(deck):
for suit in SUITS:
for rank in RANKS:
card = [rank+' of '+suit,suit,rank]
deck += [card]
return deck
def shuffle(deck):
n = len(deck)
for i in range(n):
r = random.randrange(i, n)
temp = deck[r]
deck[r] = deck[i]
deck[i] = temp
return deck
The part that I don't really get is:
temp = deck[r]
deck[r] = deck[i]
deck[i] = temp
Does the program take a card, store it in temp and then assigns the new card to the old cards place?