Dice Rolls. In Craps, each roll of the dice belongs to one of several
sets of rolls that are used to resolve
bets. There are only 36 possible dice rolls, but it's annoying to
define the various sets manually. Here's a
multi-step procedure that produces the various
sets of dice rolls around which you can
define the game of craps.
First, create a sequence with 13 empty
sets, call it dice.
Something like [ set() ]*13 doesn't work because it
makes 13 copies of a single set object.
You'll need to use a for statement to evaluate the
set function 13
different times. What is the first index of this sequence? What is
the last entry in this sequence?
Second, write two, nested, for-loops to iterate through all 36
combinations of dice, creating 2-tuples. The
36 2-tuples will begin with
(1,1) and end with (6,6). The
sum of the two elements is an index into dice. We
want to add each 2-tuple to the appropriate
set in the dice
When you're done, you should see results like the
Now you can define the various rules as
sets built from other
On the first roll, you lose if you roll 2, 3 or 12. This
is the setdice | dice |
dice. The game is over.
On the first roll, you win if you roll 7 or 11. The game
On the first roll, any other result (4, 5, 6, 8, 9, or
10) establishes a point. The game runs until you roll the
point or a seven.
Once a point is established, you win if you roll the
point's number. You lose if you roll a 7.
Once you have these three sets defined,
you can simulate the first roll of a craps game with a relatively
elegant-looking program. You can generate two random numbers to
create a 2-tuple. You can then check to see
if the 2-tuple is in the
lose or winsets.
If the come-out roll is in the pointset, then the sum will let you pick a
set from the dice sequence. For example, if
the come-out roll is (2,2), the sum is 4, and
you'd assign dice to the variable
point; this is the set of
winners for the rest of the game. The set of
losers for the rest of the game is always the
The rest of the game is a simple loop, like the come-out roll
loop, which uses two random numbers to create a
2-tuple. If the number is in the
pointset, the game is a
winner. If the number is in the crapsset, the game is a loser, otherwise it
Roulette Results. In Roulette, each spin of the wheel has a number of
attributes like even-ness, low-ness, red-ness, etc. You can bet on
any of these attributes. If the attribte on which you placed bet
is in the set of attributes for the number,
We'll look at a few simple attributes: red-black, even-odd,
and high-low. The even-odd and high-low attributes are easy to
compute. The red-black attribute is based on a fixed set of
We have to distinguish between 0 and 00, which makes some of
this decision-making rather complex. We can, for example, use
ordinary integers for the numbers 0 to 36, and append the
string "00" to this
set of numbers. For example, set(
range(37) ) | set( ['00'] ). This set
is the entire Roulette wheel, we can call it
We can define a number of sets that
stand for bets: red, black,
high and low. We can iterate
though the values of wheel, and decide which
sets that value belongs to.
If the spin is non-zero and spin % 2 == 0,
add the spin to the evenset.
If the spin is non-zero and spin % 2 != 0,
add the spin to the oddset.
If the spin is non-zero and it's in the
the spin to the redset.
If the spin is non-zero and it's not in the
the value to the blackset.
If the spin is non-zero and spin <= 18,
add the value to the lowset.
If the spin is non-zero and spin > 18, add
the value to the highset.
Once you have these six sets defined,
you can use them to simulate Roulette. Each round involves picking a
random spin with something like random.choice( list(wheel)
). You can then see which set the spin
belongs to. If the spin belongs to a set on
which you've bet, the spin is a winner, otherwise it's a
These six sets all pay 2:1. There are a
some sets which pay 3:1, including the 1-12,
13-24, 25 to 36 ranges, as well as the three columns, spin % 3 == 0,
spin % 3 == 1 and spin % 3 == 2. There are still more bets on the
Roulette table, but the sets of spins for
those bets are rather complex to define.
Sieve of Eratosthenes. Look at Sieve of Eratosthenes. We
created a list of candidate prime numbers,
using a sequence with 5000 boolean flags. We can, without too much
work, simplify this to use a set instead of
Procedure 16.1. Sieve of Eratosthenes - Set Version
Create a set,
prime which has integers between 2 and
p ← 2
Generate Primes. while 2 ≤ p < 5000
Find Next Prime. while is is not in prime and 2 ≤
p < 5000: increment
At this point, p is
k ← p +
while k < 5000
Remove k from the
k ← k +
Next p. increment p
At this point, the setprime has the prime numbers. We can return
In this case, step 2b, Remove Multiples, can be revised to
create the set of multiples, and use
difference_update to remove the multiples
from prime. You can, also, use the
range function to create multiples of
p, and create a set from
this sequence of multiples.
Chapter 17. Exceptions
Published under the terms of the Open Publication License