1
h09
CCS CS20 S18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h09: Perkovic 5.3-5.6 (2-d lists, more on loops)

ready? assigned due points
true Thu 05/24 12:30PM Thu 05/31 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

This assignment should be submitted by scanning the pages in the correct order to a PDF file and uploading to gradescope.com.

For more information, visit ucsb-cs8.github.io and look for Gradescope: Student Self Submission under "topics".

Even though it is a Gradescope submission, nevertheless, *please fill in the information at the top of this homework sheet*, including your name and umail address.


READING ASSIGNMENT

Please read Perkovic 5.3-5.6 (2-d lists, more on loops). Then complete these problems.

  1. (5 pts) Please fill in the information at the top of this homework sheet, as usual. WRITE DARK, and remember, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED. No staples, paper clips, fold/tear etc or anything that would jam up the scanner.

  2. Each of the problems below shows a function definition at left, then one or more function calls at right. For each function call, write what the function call evaluates to.

    1. def foo(x):
         color = "blue"
         if x > 10:
            color = "red"
         color = "green"
         return color
      
      pts function call write result here
      (5 pts) foo(5)  
      (5 pts) foo(10)  
      (5 pts) foo(15)  
    2. def bar(x):
         color = "blue"
         if x > 10:
            color = "red"
         else:
            color = "green"
         return color
      
      pts function call write result here
      (5 pts) bar(5)  
      (5 pts) bar(10)  
      (5 pts) bar(15)  
    3. def baz(x):
         count = 0
         i = 1
         while i < x: 
            if i % 5 == 0:
               count += 1
            i+=1
         return count
      
      pts function call write result here
      (5 pts) baz(3)  
      (5 pts) baz(10)  
      (5 pts) baz(12)  
  3. (10 pts) Section 5.3 describes how a “list of lists” can be used to represent a 2-dimensional list, such as a matrix or grid.

    Suppose we wanted to program a Python program to play Tic-Tac-Toe. Tic-Tac-Toe is played in a grid similar to the ones shown in the figures below. Figure (a) shows an empty Tic-Tac-Toe grid, while Figures (b),(c),(d) show what might be the first three moves of the game. Players take turns filling in squares with ‘x’ and ‘o’.

    We can represent the Tic-Tac-Toe boards shown here as lists of lists of strings. A list of list of strings representation is shown below for three of the five boards. The top level list represents a list of three rows. Each of the lists has three elements, each of which is a space, x or o for that square.

    The tables below shows the representations for the boards in Figures (1),(3) and (5). Fill in the Python representations for Figures (2) and (4).

    Figure 1
     | |
    -+-+-
     | |
    -+-+-
     | | 
    
    board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
    
    Figure 2
     | |
    -+-+-
     | |
    -+-+-
    x| | 
    

    (10 pts)

    board = 
    
    Figure 3
     | |
    -+-+-
     |o|
    -+-+-
    x| | 
    
    board = [[' ',' ',' '],[' ','o',' '],['x',' ',' ']]
    
    Figure 4
     | |
    -+-+-
    x|o|
    -+-+-
    x| | 
    

    (10 pts)

    board = 
    
    Figure 5
    o| |
    -+-+-
    x|o|
    -+-+-
    x| | 
    
    board = [['o',' ',' '],['x','o',' '],['x',' ',' ']]
    
  4. (10 pts) Since lists are mutable, the change from Figure 2 to Figure 3 could be made by an assignment statement. Which of the following assignments statements would do the job? (Circle one)

    board[1][1]='o'

    board[1,1]='o'

    board[2][2]='o'

    board[2,2]='o'

    none of these
  5. (10 pts) Likewise, which assignment statement changes the value of board from the one shown in Figure 3 to the one shown in Figure 4? (Circle one)

    board[0][1]='x'

    board[0,1]='x'

    board[1][0]='x'

    board[2,1]='x'

    none of these