This week is again all about lists. You will use them to run an experiment, and you will use them to implement the basic tools for a simple game. Of course, we also get more practice with loops and functions and strings and....
Create a directory on your USB device for this lab, say, lab12, and launch IDLE.
This week, you will not submit your shell window at the end of the session. Do not panic if you close it accidentally along the way.
You will submit a responses.txt file this week. Download this template file and use it to record any answers or predictions asked for in the exercises.
We have two weeks after Thanksgiving break. What would you most like to learn or do during that time? Are there parts of Python you would like for us to study, or a particular program you'd like for us to write? Now is your chance...
We have used functions from Python's random module to generate random numbers for simulations and a few other tasks. Computers can't generate truly random numbers, because they depend on programs and inputs that are not completely random. But we can write programs that generate streams of numbers that look and behave like they are random.
How might we judge the randomness of a random number generator? One measure is the distribution of the numbers. We should expect that Python's randint(lower, upper) function generates all the numbers between lower and upper over time, and that each number is equally likely. We would also expect that numbers in the 60s would be as likely as numbers in the 90s or the 20s.
Let's write code to evaluate the distribution of the numbers produced by randint().
>>> generate_random_list(10) [45, 79, 9, 86, 98, 89, 26, 82, 49, 13] >>> generate_random_list(16) [59, 77, 22, 58, 25, 46, 36, 2, 7, 72, 26, 36, 23, 15, 83, 53]
>>> generate_empty_buckets(10) [[], [], [], [], [], [], [], [], [], []]
For example:
expected bucket size = 100000 smallest bucket size = 99659 largest bucket size = 100489
After you have your code working, change LIST_SIZE to one million. Re-run the experiment at least three times. Record the results in your responses file.
Run the experiment at least three times with lists of one million random integers. Record the results in your responses file.
Lists of lists can be used to represent the boards for many simple games. Suppose that two people are playing the common children's game Tic-Tac-Toe. The players might be making marks on a grid, like this:
| | X ----------- O | | X ----------- | | OWe can represent the state of a Tic-Tac-Toe board using a list of lists:
[ [' ', ' ', 'x'], ['o', ' ', 'x'], [' ', ' ', 'o'] ]
To create a program that lets two people play Tic-Tac-Toe, we will need a number of functions that operate on such lists. Let's do it.
Create a new Python file named tic_tac_toe.py. Show an example of output demonstrating your functions for each step below in your responses file.
For the following steps, assume these assignments:
board_1 = [ [' ', ' ', 'x'], \ ['o', ' ', 'x'], \ [' ', ' ', 'o'] ] board_2 = [ [' ', ' ', 'x'], \ ['o', 'o', 'x'], \ [' ', ' ', 'x'] ] board_3 = [ ['o', ' ', 'x'], \ ['x', 'o', 'x'], \ [' ', ' ', 'o'] ]
>>> display( board_1 ) [' ', ' ', 'x'] ['o', ' ', 'x'] [' ', ' ', 'o'] >>> display( board_2 ) [' ', ' ', 'x'] ['o', 'o', 'x'] [' ', ' ', 'x'] >>> display( board_3 ) ['o', ' ', 'x'] ['x', 'o', 'x'] [' ', ' ', 'o']
Bonus. Provide a prettier, more human-friendly output -- but only after you do Steps 2-5 first!
>>> is_occupied(board_1, 0, 1) False >>> is_occupied(board_2, 2, 2) True
>>> make_move('x', board_1, 1, 1) >>> display( board_1 ) [' ', ' ', 'x'] ['o', 'x', 'x'] [' ', ' ', 'o'] >>> make_move('o', board_1, 2, 0) # original board >>> display( board_1 ) [' ', ' ', 'x'] ['o', ' ', 'x'] ['o', ' ', 'o'] >>> make_move('x', board_1, 2, 2) False
>>> is_a_win_for( 'x', board_1 ) False >>> is_a_win_for( 'x', board_2 ) True >>> is_a_win_for( 'o', board_2 ) False >>> is_a_win_for( 'x', board_3 ) False >>> is_a_win_for( 'o', board_3 ) True
Make sure that your program files are complete and saved. Save your responses.txt file.
Submit your files for grading on the electronic submission system, at lab12 -- Lists for Experiments and Fun.
As always, make sure you see the verification screen that says The files listed above were uploaded.
If you need any help, let me know.