Problem Set 5 - Due Mon, Mar 6

(Files for task 2 and 3 due at 11:59 pm EST, paper copy of task 1 due Monday in lecture.)

Reading

  1. Think Python, Ch. 8: Iteration
  2. Think Python, Ch. 10: Lists
  3. Slides and notebooks from Lec 8 and Lec 9.
  4. Problems and solutions from Lab 06 Lists

About this Problem Set

This problem set will give you practice with lists and loops.

  1. In Task 1, you will will draw memory diagrams to capture changes in lists, practicing the concepts of mutability and aliasing. Take a picture or make a copy of your solution so you can compare it to our solution before grading is finished..
  2. In Task 2, you will write functions that create lists through accumulation. This is a continuation of the Word Play task from PS04.
  3. In Task 3 you will write building block functions for a Tic Tac Toe games, using lists.

    Use this shared Google Doc to find a pair programming partner. Remember that you can work with the same partner a maximum of TWICE this semester. We will deduct 2 points per day for any students who have not recorded their names on the pair programming google doc by Thursday, Mar 2 at 11:59pm.

  4. The CS111 Problem Set Guide gives an overview of psets, including a detailed description of individual and partner tasks.
  5. In Fall 2016, students spent in average 1.2 hours on Task 1 (min = 0.2 hours, max = 2 hours); 1.6 hours on Task 2 (min = 0.5 hours, max = 6 hours); and 4 hours on Task 3 (min = 0.5 hours, max = 10 hours).
  6. There is no quiz for this problem set, due to the scheduling of Exam 1. Task 1 will count in place of a quiz grade (25 points), while Tasks 2 and 3 count for the code grade (55 points).

All code for this assignment is available in the ps05 folder in the cs111/download directory within your cs server account. This assignment also uses the Otter Inspector program to help you do a final check for Task 2, Task 3, and your Honor Code form before you submit.


Task 1: Memory Diagram

This is an individual problem which you must complete on your own, though you may ask for help from the CS111 staff.

Take a picture or make a copy of your solution so you can compare it to our solution before grading is finished..

Consider the following sequence of nine statements that create and manipulate nested lists in Python:

r = [[3,4,5], [6,7], [8]]

s = r[0]

s[1] = 9

r[0][2] = r[1]

r[1].append(r.pop())

s[2][1] = r[0][0] + r[0][1]

s[2][2][0] = s[2][0] + r[1][1]

r.insert(0, r[1][2])

r[2][2] = 17

Your task is to draw a sequence of nine memory diagrams that show the state of Python's memory after each of the nine statements.

Notes

Task 2: Word Search

This task is an individual problem which you must complete on your own, though you may ask for help from the CS111 staff.

This task is a continuation of Task 2 from PS4. In that task, you wrote the functions isPalindrome and scrabbleScore.

You will use these functions to write three new functions that use a list of English words to find and return words that fulfill a certain property, stored in lists and tuples.

Additionally, you will write a function, reverseWords, that takes any sentence as a string of space-separated words, and returns a string where every word is reversed.

Fill out the body of these functions in the file wordsearch.py following their contracts.

Notice that the file imports the functions from wordplay.py as well as a list of words from vocabulary.py. This list is called englishwords, and is the list that your functions should search through.

Sample Execution (run otterInspect.py for more test cases):

In [1]: listPalindromes()
Out[1]: ['aa', 'aha', 'anna', 'bib', 'bob', 'boob', 'cc',
 'civic', 'dad', 'deed', 'deified', 'did', 'dud',
 'ere', 'eve', 'ewe', 'eye', 'gag', 'gig', 'hrh',
 'huh', 'kayak', 'level', 'll', 'madam', 'mam', 'marram',
 'minim', 'mm', 'mom', 'mum', 'nan', 'nauruan', 'noon',
 'nun', 'oho', 'otto', 'pap', 'peep', 'pep', 'pip', 'poop',
 'pop', 'pp', 'pup', 'qq', 'radar', 'redder', 'rotor',
 'sagas', 'sees', 'sexes', 'shahs', 'sis', 'solos', 'sos',
 'ss', 'stets', 'tat', 'tenet', 'tit', 'tnt', 'toot',
 'tot', 'tt', 'tut', 'wow']

In [2]: listGoodScrabbleWords(35)

Out[2]: [('acquaintanceships', 35), ('compartmentalized', 35),
 ('compartmentalizing', 36), ('cryptographically', 35),
 ('czechoslovak', 35), ('czechoslovakia', 37),
 ('czechoslovakian', 38), ('czechoslovakians', 39),
 ('czechoslovaks', 36), ('electroencephalograph', 36),
 ('electroencephalographs', 37), ('embezzlement', 36),
 ('embezzlements', 37), ('extemporization', 35),
 ('extemporizations', 36), ('jazzily', 35), ('mozambiquean', 36),
 ('mozambiqueans', 37), ('overcapitalizations', 35),
 ('psychoanalytically', 36), ('psychokinetically', 36),
 ('psychopathically', 36), ('quinquagesimas', 35),
 ('quizzed', 35), ('quizzical', 38), ('quizzically', 43),
 ('quizzing', 36), ('schizophrenically', 41),
 ('schizophrenics', 35), ('sympathizingly', 37)]

In [3]: bestScrabbleWord()
Out[3]: ('quizzically', 43)

In [4]: reverseWords('we are never ever getting back together')
Out[4]: 'ew era reven reve gnitteg kcab rehtegot' 

Notes

Task 3: Tic Tac Toe

This task is a partner problem in which you are required to work with a partner as part of a two-person team.

You will write a program to help you and a friend play Tic Tac Toe.

Here is an example transcript of running this program.

Current board (turn 0):
_ _ _
_ _ _
_ _ _

Player X: Enter the row,column coordinates of your play, separated by comma: 1,3
Your move is outside the board, or in a filled cell. Please try again.

Player X: Enter the row,column coordinates of your play, separated by comma: 0,0
Current board (turn 1):
X _ _
_ _ _
_ _ _

Player O: Enter the row,column coordinates of your play, separated by comma: 1,2
Current board (turn 2):
X _ _
_ _ O
_ _ _

Player X: Enter the row,column coordinates of your play, separated by comma: 0,1
Current board (turn 3):
X X _
_ _ O
_ _ _

Player O: Enter the row,column coordinates of your play, separated by comma: 0,2
Current board (turn 4):
X X O
_ _ O
_ _ _

Player X: Enter the row,column coordinates of your play, separated by comma: 1,1
Current board (turn 5):
X X O
_ X O
_ _ _

Player O: Enter the row,column coordinates of your play, separated by comma: 2,2
Current board (turn 6):
X X O
_ X O
_ _ O

Player O wins!
X X O
_ X O
_ _ O

We have provided 3 functions in the tictactoe.py file in your ps05 folder:

Notice that play is called within the body of if __name__ == '__main__', as you saw in lab. The code in this block will be executed only when run directly from tictactoe.py, and not when it is imported elsewhere.

Understanding play:

You are provided with the following play function, which runs the TicTacToe game, invoking various helper functions along the way. You should carefully study this play function so that you understand how each of the helper functions you are defining will be used. Do not change this definition of the play function in any way!

def play(n, players):
    """Main function: 
       -- n should be the dimensions of the board
       -- players should be a non-empty list of unique strings, serving
          as the symbol of each player.
          Typically, there are two players, but any number is supported.

       Creates a new board of size n x n and alternates between the given 
       players, prompting for moves until game is won or tied
    """

    # Create n x n game board
    board = initializeBoard(n)

    # Iterate over turns. Board has n*n cells, so there at most n*n turns.
    for turn in range(n*n): 

        # Select player by turn.
        player = players[turn % len(players)] 

        # Display board.
        print 'Current board (turn ' + str(turn) + '):'
        displayBoard(board)

        # Prompt player for a move.  Update the board.
        playMove(getValidMove(player, board), player, board)

        # Check for a win by player.
        if won(player, board):

            # Display winning player and final board
            print 'Player', player, 'wins!'
            displayBoard(board)
            return # Exit from loop early since game is done

    # Reaching here means the board is full after n*n moves,
    # but no player has won.    
    print 'Game is tied.'
    displayBoard(board)
    return # Not necessary to say this, but indicates play function is done.

Your task:

In this problem, you will implement the following helper functions (plus several more of your own design) to complete the implementation of play. Do not change play itself.

Notes

Task 4: Honor Code Form and Final Checks

As in the previous problem sets, your honor code submission for this pset will involve defining entering values for the variables in the honorcode.py file. This is a Python file, so your values must be valid Python code (strings or numbers).

If you wrote any function invocations or print statements in your Python files to test your code, please remove them, comment them out before you submit, or wrap them in a if __name__ == '__main__' block. Points will be deducted for isolated function invocations or superfluous print statements.

Remember to run otterInspect.py one final time before you submit to check that your wordsearch.py and tictactoe.py programs work as intended, and that your honor code form is complete. While running your code through otterInspect is not required, it is highly recommended since it may catch errors that you haven't noticed.

If you have issues running otterInspect.py, first check the following:

If you're unable to resolve the issue after going through this checklist, seek help from any of the course staff.


How to turn in this Problem Set

Soft-Copy Submission

Hard-Copy Submission

Bring to class on Monday the paper-copy of your Task 1. Note your name and section on your submission. Take a picture or make a copy of your solution so you can compare it to our solution before grading is finished..