University of Arizona, Department of Computer Science

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:

  1. Write a function init() that does the following:
    1. Use the input()​ function to read in the value of n as the first value read in.
    2. Use the input()​ function to read in the value of s as the second value read in.
    3. 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.

  2. Write a function ​make_grid(​ n ) that creates and returns an n × n grid of randomly generated letters:
    1. each row of the grid is represented as a list of length n; and
    2. the grid then consists of a list of n such rows.

    For example: the grid

    abc
    def
    ghi
    is represented as the list of lists
    [ [‘a’, ‘b’, ‘c’], [‘d’, ‘e’, ‘f’], [‘g’, ‘h’, ‘i’] ]

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

  4. Write the​ main()​ function to do the following:
    1. call init() ;
    2. call make_grid() ​ with the appropriate input argument ; and
    3. 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).

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