CSc 120: Word Grid
This problem involves learning to use Python’s random number generator.
File Names
Your program should be in a file named word-grid.py
Expected Behavior
Your program should read two integer values from
the input: a grid size n and a
random number seed s;
use s to initialize the random number generator (i.e., s is the “seed”); create an n × n grid of randomly
generated lower-case letters; and print out the grid of letters one row per line.
Write a program, in a file named word-grid.py, that behaves as follows:
-
Write a function init() that does the following:
-
Use the input() function to read in the value of n as the
first value read in.
-
Use the input() function to read in the value of s as the
second value read in.
-
Initialize the random number generator with the value s. You will have to import the
module random:
import random
or
from random import *
The initialization is done by calling
random.seed( s ).
Note that your code should not prompt the user for input. It will simply read in
two numbers and treat the first one as the grid size n and the second one as the seed s.
-
Write a function make_grid( n ) that creates and returns an
n × n grid of randomly generated letters:
-
each row of the grid is represented as a list of length n; and
-
the grid then consists of a list of n such rows.
For example: the grid
is represented as the list of lists
[ [‘a’, ‘b’, ‘c’], [‘d’, ‘e’, ‘f’], [‘g’, ‘h’, ‘i’] ]
-
Write a function print_grid( g ) that takes a grid (i.e., list of lists)
g and prints it out one row per line, with a single comma after each letter except for the
last one in the row.
For example, the grid
[ [‘p’, ‘q’, ‘r’, ‘s’], [‘t’, ‘u’, ‘v’, ‘w’], [‘x’, ‘y’, ‘z’, ‘a’], [‘b’, ‘c’, ‘d’, ‘e’] ]
is printed out as
p,q,r,s |
t,u,v,w |
x,y,z,a |
b,c,d,e |
note: The indentation in this example is just to improve readability. The output
from your print_grid() function should not have any whitespace at the
beginning of any line for indentation purposes.
-
Write the main() function to do the following:
-
call init() ;
-
call make_grid() with the appropriate input argument ; and
-
call print_grid() with the grid returned by
make_grid() to print it out.
Programming Requirements
When converting from a random number to a letter, do not use a big if-statement.
See the number to letter problem for this.
Development Strategy
The representation of a grid of letters as a list of lists has been explained above. The key issue is to
generate letters using the random number generator (initialized with the seed value s read in from the
input).
-
You can use the function random.randint( m, n ) to generate
a random integer between the values m and n. For example,
random.randint(10, 20) will generate a random integer between
10 and 20. Given this, think of how you can call random.randint(...)
to generate values such that all possible (lower-case) letters, and only those
letters, can be generated?
-
Once you have the random number returned by random.randint(...), you have to convert
it to a letter. Use your solution for the
number to letter problem for this.
Comment: If you are lazy like me and don't like unnecessary typing, you can import from the
random library as follows:
from random import *
after which you can simply refer to randint(...), i.e., without having to type the
prefix random..