Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Set Exercises

  1. 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 sequence.

    When you're done, you should see results like the following:

    >>> 
    dice[7]
    
    set([(5, 2), (6, 1), (1, 6), (4, 3), (2, 5), (3, 4)])
    

    Now you can define the various rules as sets built from other sets.

    lose

    On the first roll, you lose if you roll 2, 3 or 12. This is the set dice[2] | dice[3] | dice[12]. The game is over.

    win

    On the first roll, you win if you roll 7 or 11. The game is over.

    point

    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.

    craps

    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 win sets.

    If the come-out roll is in the point set, 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[4] 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 craps set.

    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 point set, the game is a winner. If the number is in the craps set, the game is a loser, otherwise it continues.

  2. 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, you win.

    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 values.

    redNumbers= set( [1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36] )

    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 wheel.

    We can define a number of sets that stand for bets: red, black, even, odd, 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 even set.

    • If the spin is non-zero and spin % 2 != 0, add the spin to the odd set.

    • If the spin is non-zero and it's in the redNumbers set, add the spin to the red set.

    • If the spin is non-zero and it's not in the redNumbers set, add the value to the black set.

    • If the spin is non-zero and spin <= 18, add the value to the low set.

    • If the spin is non-zero and spin > 18, add the value to the high set.

    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 loser.

    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.

  3. 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 a list.

    Procedure 16.1. Sieve of Eratosthenes - Set Version

    1. Initialize

      Create a set, prime which has integers between 2 and 5000.

      p ← 2

    2. Generate Primes. while 2 ≤ p < 5000

      1. Find Next Prime. while is is not in prime and 2 ≤ p < 5000: increment p.

        At this point, p is prime.

      2. Remove Multiples

        kp + p

        while k < 5000

        1. Remove k from the set prime

        2. kk + p

      3. Next pincrement p

    3. Report

      At this point, the set prime has the prime numbers. We can return the set.

    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.


 
 
  Published under the terms of the Open Publication License Design by Interspire