Cosi10 Pa5
Cosi10 Pa5
Programming Assignment 5
Program Description:
This assignment will test your understanding of the definite loops. Write three scripts to solve the problems
below. All your programs should follow the submission guideline at the end.
Problem 1:
Write a program that gets input from the user and writes it to the file. Your program needs to have at least
two functions, main and fileHandler. The function fileHandler accepts the output file name as a
parameter, asks the user to type the texts line by line to write, and stores the input to given file until user
types Done.
: Done
____________________________________________________________________
In out.txt,
Problem 2:
Write a program that counts the number of appearances for the given character. Your program needs to have
at least two functions, main and charCount. The main gets a filename and a target character and checks
the give file exists or not. If the file does not exist, your program asks the user to type a valid file name.
Similarly, if the user submitted the string with more than 2 characters, your program asks the user to submit
the valid character. The charCount gets the string data from the file and the target word, counts the
number of appearances of the target word in the string data using dictionary, and returns the number of
appearances. Note that charCount is case-insensitive, if the target character is ‘t’, then, your program
needs to find both ‘t’ and ‘T’. You may use the file PA5_1.txt which is available in the course Moodle
page.
Problem 3:
Write a program that finds the names of the movie with highest and lowest rating in the file. You need to
use PA5_2.csv for this problem. Each line of the file has three values: name, rating, and number of votes,
and these three are separated by ‘,’. Your program needs to have at least three functions, main,
findMin and findMax. Both findMin and findMax get a list of data and returns the index of the
minimum or maximum value. Your program needs to print the title of a movie with highest and lowest
number of votes and highest and lowest ratings.
Note that you can separate the line by comma using split(’,’).
Problem 4:
Write a program that checks the given dictionary has unique values. Your program needs to have at least
two functions, main and isUnique. The functions isUnique accepts a dictionary whose keys and
values are strings as a parameter and returns True if no two keys map to the same value (and False if
any two or more kyes do map to the same value). You may assume that the dictionary is given in the main
(no need to use input function).
Stuart / Reges
Jessica / Wolk
Allison / Abourn
Hal / Perkins
____________________________________________________________________
Marty / Perkins
Stuart / Reges
Jessica / Wolk
Allison / Abourn
Hal / Perkins
Problem 5:
Write a program that manages the inventory to track products and their quantities in stock. Your program
needs to have at least three functions, main, addProduct, and removeProduct. The main gets the
name of the product and quantity that user wants. If the user gives a positive integer, it means that product
is added to the inventory. But, for negative number, the product is retrieved from the inventory. The
addProduct gets the inventory dictionary, product name, and quantity of the product as parameters and
updates the inventory using the product name and quantity. The removeProduct gets the inventory
dictionary, product name, and quantity and updates the inventory using the product name and quantity.
When the quantity is less than the user wants, your program needs to notify the user. If the quantity reaches
0, then your program needs to delete the product from the inventory. Your programs repeat this until the
user types Done.
Apple 15
Banana -50
Orange -10
Banana -30
Banana -1
Done
Submit via Moodle a zip file named yourfirstname_yourlastname_PA5.zip which contains all
your python scripts. Please make sure to use the exact same file names and function names, including
identical capitalization.
Your program should be submitted via Moodle the day it is due (for late policy check the syllabus).
Write each program in a separate file named Problem1.py, Problem2.py, etc. You should not have any
code, except a call to your main function, outside of a function. You should use the main function to call
other functions that implement the solution.
You also need to include comments in your code. Include a header comment at the beginning of each
file with some basic information and a description of the program in your own words.
# Name
# COSI 10a, Fall 2024
# Programming Assignment 5
#
# Description: ...
For this assignment, you should limit yourself to the Python features covered up to lecture 22. Though
we will cover more material while you work on this assignment, please do not use it on this assignment.
External Correctness: The output of your program should match exactly what is expected.
Programs that have errors will not receive points for external correctness.
Internal Correctness: Your source code should follow the stylistic guidelines shown in class.
Remember to include the comment header at the beginning of your program and comment your
code.