CSE 1310 - Assignments - Programming Assignment 3

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 in the file on the first line. 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.


Task 1 (5 pts.)

The following code is supposed to create a variable list1 that stores list2 in reverse order. It contains an error and will not run. In answers.xxx, explain what is wrong. Correct the code and save it as task1.py. Do not use the reverse() list method in your answer.

list1 = []
list2 = ['a', 'b', 'c', 'd']

number = len(list2)-1
while number >= 0:
    append(list1, list2[number])
    number = number - 1

for item in list1:
    print item

Task 2 (5 pts.)

The following code is supposed to print elements 0 through 3 of my_list, but does not work correctly. In your answers.xxx file, explain what is wrong. Correct the code and save it as task2.py.

my_list = ['bob', 'jane', 'mary', 'tom', 'rick']
for name in my_list[0:3]:
    print name

Task 3 (5 pts.)

Mr. Bugs, in one of his programs, attempts to make a copy (called L2) of list L1 so that he can experiment with the copy and not accidentally modify the original version. Whenever he tries to use pop() on the copy, it keeps modifying the original. In your answers.xxx file, explain what is wrong. Correct the code and save it as task3.py.

L1 = [1, 2, 3]
L2 = L1
item = L2.pop()
print item
print L1
Note: the above code produces the following output:
3
[1,2]

Task 4 (15 pts.)

Write a program that asks the user for 5 integers, stores them in a list and prints the numbers in the list in order from largest to smallest. Save your program as task4.py.


Task 5 (20 pts.)

Write a program that asks the user for 5 strings, stores them in a list, swaps the first and last word in the list, and prints the words at index positions 2 through the end. Use slicing to obtain the words at position 2 through the end in order to print them. Save your program as task5.py.


Task 6 (25 pts.)

1   5   6
7   0   10
3  -5   33

A mathematical matrix, like the one above, can be simulated with nested lists, in which each row of the matrix is represented as a list. The row lists are then stored in a master list, resulting in the following:

L = [[1,  5,  6],
     [7,  0, 10],
     [3, -5, 33]]

The elements can be accessed in this manner: L[row_number][column_number], so to access the 33 in the above example, you would use L[2][2]. Write a program that asks the user for 3 integers for each row, constructs a matrix like the one above, and prints the diagonal. The diagonal in this case is 1, 0, 33. The numbers will be entered top row first (from left to right), then the middle row, and, finally, the bottom row. The numbers for our example would be entered in this order: 1, 5, 6, 7, 0, 10, 3, -5, 33. Note that this will require the user to be prompted for an integer 9 times. Save your program as task6.py.


Task 7 (25 pts.)

In class, we wrote a program that allowed the user to create a simple phone directory using two lists. The names were stored in one and the numbers in the other. In this task, you will create a simple food inventory system using a single list. Create a loop that continually asks the user for a food and a quantity and stores them in a singles list of the form foods = [food1, quantity1, food2, quantity2, food3, quantity3]. If the user enters the letter 'q' or 'Q', stop the loop and print the inventory in the following manner:

food1 : quantity1
food2 : quantity2
food3 : quantity3

Save your program as task7.py.


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