CSE 1310 - Assignments - Programming Assignment 7

The assignments will be graded out of 100 points. Create a text document entitled answers.xxx (where you replace xxx with whatever extension is appropriate, depending on the file format you use). Acceptable file formats are plain text, Word document, OpenOffice document, and PDF. Put your name and UTA ID at the top. Your answers that are not part of a program will be added to this file. Each task below will instruct you where to put your answers.


Smarter Tic-Tac-Toe (Tasks 1, 2)

Extend your Tic-Tac-Toe program from the previous assignment so that the computer player will block human player wins. For example, if the human is X and the computer is O in this scenario (open moves are represented as _):

X _ X
O _ _
_ _ _

then the computer should play the first row, second column to block the win. Obviously, if the human player can make one of two plays to win, the computer cannot block both:

X _ X
_ O _
X _ O

In this case, the computer should play either the first row, second column or the second row, first column.


Task 1 (10 pts.)

Design a function that determines if the computer needs to block a win and returns the necessary move to do so. If it does not need to block a win, it can return any open move. You can modify any of your existing functions to do this. In your answers.xxx file give a description of the funtion and its inputs and outputs, as we do in the class examples.


Task 2 (20 pts.)

Implement the code for your function and save it in your tic-tac-toe_functions.py file from the previous assignment. If you were unable to complete your tic-tac-toe game from the previous assignment, write a detailed description of exactly how you would implement this and include pseudo-code or Python code. Also, explain the test cases you used to verify this function works properly in your answers.xxx file.


File Processing (Tasks 3, 4)

You will write a function that takes a filename as an argument, opens and reads the specified file and counts the number of occurrences of each word. I am providing a function, process_string(string), that accepts a single string as an argument, removes the punctuation and newline characters from the string, and returns the words as a list of strings, You can download process_string.py here, save the file in the directory with your code, and import it in the file with your function using the following line:

from process_string import *

Now you can use it like any other function. If you pass it an illegal argument, it will print an error message and return None. You can keep track of the number of occurrences of each word using any method or data structure you like, print the most used word and how many times it was used, and save the number of occurrences of each word to an output file, output.txt, as follows. Each line of the file will be the word followed by a space, followed by the number of occurrences for that word, like this:

hello 12
world 50
the 21

A capitalized word should be the same as its lower case equivalent, so keep that in mind. It doesn't matter if you use the capitalized or lower case version of the word in the file, as long as the count is correct. Here is a simple example:

Input file (file1.txt):
The dog went to the park
to play with the other dogs.
It got hungry and barked at the
other dogs.
Program output:
the : 4
Output file output.txt:
and 1
play 1
hungry 1
park 1
dog 1
dogs 2
to 2
other 2
at 1
it 1
got 1
went 1
with 1
the 4
barked 1

Your output may be in a different order. That is okay. Here is another sample file for testing: file2.txt.


Task 3 (10 pts.)

Design your function and any helper functions you create in answers.xxx. Be sure to describe the inputs and outputs for each function. Be sure to NOT describe how you will implement each function, how you implement a function is not relevant information for the function design part.


Task 4 (20 pts.)

Implement the function (and any helper functions you decide you need) as described above and save in task4_functions.py. Create a main program to test your function with the input files provided and any others you'd like to use. Name it task4_main.py. Make sure your program appends the output of each test to the output file. Put a blank line after each test. If you use any additional files for testing purposes, include them in the zipped folder when you submit.

Your task4_main.py file should also include test cases that you used for the individual functions that you have implemented. Part of the score for this task will depend on how good your selection of test cases is.


Tasks 5, 6, 7

The file albums.txt contains a list of albums that are in Rolling Stone Magazine's list of top 100 albums of all time. Each line contains the name of a band, the name of an album, and the date the album was released. For example:

Beatles - Revolver (1966)
...

Task 5 (20 pts.)

Design a function (name it whatever you like) that takes the filename as an argument, reads the file and returns a dictionary of the albums in the following format:

albums = {'band1': {'album1': 'date1', 'album2': 'date2'},
          'band2': {'album1': 'date1', 'album2': 'date2'}}

Note that, in the lines above, albums is defined as a Python dictionary, associating each band name with another band-specific dictionary, which associates album names with dates. For full-credit, your implementation should comply with this format. For partial credit, your implementation can use different formats (such as lists, tuples, and so on), as long as it stores all information in a manner that allows the print_albums function (discussed later in this task) to do what it needs to do.

Also note that some bands have multiple albums, so you will have to check if you have already added a band to avoid overwriting it. Be sure to discuss the inputs and outputs of your function in answersxxx. I am providing a helper function, process_album(data), that takes a string as an argument (a line from the file) and returns a tuple in the following format:

('band_name', 'album_name', 'album_year')

process_album(data) can be downloaded here.

Also, design a function called print_albums(data) that takes your dictionary of albums as an argument and prints each album in your dictionary on a separate line in a format similar to the following:

band_name - album_name (album_year)

Task 6 (20 pts.)

Implement the two functions described above and save them in band_functions.py. You can import the helper function at the top of this file as follows:

from process_album import *

Task 7 (10 pts.)

Write a main file that uses your functions to generate and print the top album data. Do not worry about alphabetizing the output. Save this file as band_test.py. You will have to import your band_functions module.

Your main file should also include test cases that you used for the individual functions that you have implemented. Part of the score for this task will depend on how good your selection of test cases is.


Suggestions

The code needs to run on Python 2.x. Notify the instructor or TA of any problems you may have.

Pay close attention to all specifications on this page, including file names and submission format. Even in cases where the program works correctly, points will be taken off for non-compliance with the instructions given on this page (such as wrong file names, wrong compression format for the submitted code, and so on). The reason is that non-compliance with the instructions makes the grading process significantly (and unnecessarily) more time consuming. Contact the instructor or TA if you have any questions.

How to submit

The assignment should be submitted via Blackboard. Submit a ZIPPED directory called assignment7.zip (no other forms of compression accepted, contact the instructor or TA if you do not know how to produce .zip files). The zipped directory should contain your answers.xxx document and all the Python code files (task5.py, task6.py, etc).

Submission checklist