CSc 120: Word Grid
This problem involves learning to use Python’s random number generator.
Restrictions
-
For the long problems, your code should follow the style guidelines
for the class.
-
You may not use concepts and/or short-hand syntax not yet covered in class. The restrictions include the following:
-
Python dictionaries or sets
-
dictionary or list comprehensions, e.g., [n * 2 for i in range(10)]
-
with open (explicitly open and close the file instead)
-
the ternary operator (use an if instead)
-
recursion
-
exceptions (try/except)
-
type annotations
-
lambda expressions
-
importing libraries (except the random library for this word grid problem)
File Names
Your program should be in a file named word_grid.py. (NOTE: use an underscore, not a dash.)
Expected Behavior
First recall that a grid is a 2d-list (that is, a list of lists) where the length of each
inner list is the same length as the outer list. Your program should read two integer values
from the input. The first value is the grid size. The second is a
random number seed.
The program should use the random number seed to initialize the random number generator, create a grid of size grid size × grid size of randomly
generated lower-case letters and, finally, print out the grid of letters one row per line.
Specifically, write a program, in a file named word_grid.py, that behaves as follows:
-
At the top of your program after the header comment, import the
module random:
import random
-
Write a function init(), with no argument, that does the following:
-
Use the input() function (with no argument) to read
in the value of grid_size as the first value read in.
-
Use the input() function (with no argument) to read
in the value of seed_value as the second value read in.
-
Initialize the random number generator with the value seed_value.
Note that your code should not prompt the user for input (that is, you should not supply a string to display to the user). Your program will simply read in
two numbers and treat the first one as the grid size (which needs to be an integer) and the second one as the seed (which needs to remain a string). Use the following code to initialize the variables grid_size and seed_value, and then intialize
the random number generator:
grid_size = int(input())
seed_value = input()
random.seed(seed_value)
-
Return grid_size as the return value of the function.
Write a function make_grid(grid_size) that takes an integer argument
grid_size and creates a grid of size grid_size × grid_size whose elements are randomly generated letters.
The function returns the grid created.
Notes:
-
each row of the grid is represented as a list of length grid_size; and
-
the grid then consists of a list of grid_size 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(grid) that takes a list of lists
grid as an argument 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(), which returns the grid size;
-
call make_grid() with the grid size as an argument;
the function returns the grid created
-
call print_grid() using the grid returned by
make_grid() as its argument; print out the grid.
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. (The random
number generator is initialized with the random number seed by your
init() function.)
-
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: 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..
Testing
The test cases used in the Gradescope autograder are given in the assg01-long.zip file
on the Assignments webpage.