University of Arizona, Department of Computer Science

CSc 120: Word Grid

This problem involves learning to use Python’s random number generator.

Restrictions

  1. For the long problems, your code should follow the style guidelines for the class. You must following the updated guidelines for commenting classes.
  2. You may not use concepts and/or short-hand syntax not yet covered in class. The restrictions include the following:

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:

  1. At the top of your program after the header comment, import the module​ random:
    import random
  2. Write a function init(), with no argument, that does the following:
    1. Use the input()​ function (with no argument) to read in the value of grid_size as the first value read in.
    2. Use the input()​ function (with no argument) to read in the value of seed_value as the second value read in.
    3. Initialize the random number generator with the value seed_value.
    4. 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)
      	
    5. Return grid_size as the return value of the function.
  3. 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:

    1. each row of the grid is represented as a list of length grid_size; and
    2. the grid then consists of a list of grid_size 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’] ]

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

  5. Write the​ main()​ function to do the following:
    1. call init(), which returns the grid size;
    2. call make_grid() ​ with the grid size as an argument; the function returns the grid created
    3. 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.)

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.