CS 1101 - Programming Fundamentals
University of the People
Programming Assignment Unit 8
Creating the Input File
First, I need to create the input file input_dict.txt with the dictionary items.
Creating input_dict.txt:
1. I open a text editor (like Notepad, Notepad++, or VS Code).
2. I copy and paste the following content into the editor:
apple: red
banana: yellow
cherry: red
mango: yellow
grapes: black, green
3. I save the file as input_dict.txt in my working directory.
Creating input_dict.txt using a Python script:
As I prefer to use a Python script to create the file, I do the following:
I run the above script to generate the input_dict.txt file.
Writing the Python Program
Next, I write a Python script named invert_dict.py to read the dictionary from input_dict.txt,
invert it, and write the inverted dictionary to output_dict.txt.
Here is my Python script:
I save this script as “inver_dict.py”.
Running the Python Script
I open command prompt, navigate to the directory where invert_dict.py is saved, and run the
script using Python:
This script reads the dictionary from input_dict.txt, inverts it, and writes the inverted dictionary
to output_dict.txt.
Verifying the Output
I open the output_dict.txt file in a text editor to verify its contents. The file contain the following
inverted dictionary:
Here’s my technical explanation for the code and its output:
The provided Python script performs the task of reading a dictionary from a file, inverting it, and
writing the inverted dictionary to another file. Here’s a breakdown of how the script works:
Reading the Dictionary from the File:
The read_dict function opens input_dict.txt and reads its contents line by line. Each line is split
into key-value pairs using ': '.
Values are further split by ', ' to handle cases where multiple values exist for a single key, and are
stored as lists in the dictionary.
Inverting the Dictionary:
The invert_dict function takes the original dictionary and creates an inverted dictionary.
It iterates through each key-value pair of the original dictionary. For each value in the value list,
it adds the key to the list of keys associated with that value in the inverted dictionary.
This results in each unique value in the original dictionary mapping to a list of keys that had that
value.
Writing the Inverted Dictionary to a File:
The write_dict function writes the inverted dictionary to output_dict.txt.
It iterates over the inverted dictionary and writes each key along with its associated list of values
to the file in the format key: value1, value2, ....
Main Logic:
The script first creates the input file with the sample dictionary content (if not already created
manually).
It then reads the dictionary from the input file, inverts it, and writes the inverted dictionary to the
output file.
Example Input and Output
Input (input_dict.txt):
Output (output_dict.txt):
The inverted dictionary correctly maps each color to the corresponding fruits, demonstrating the
successful execution of the script.
References
Downey, A. (2015). Think Python: How to think like a computer scientist (2nd ed.). Green Tea
Press.
Schafer, C. (2016, April 29). Python tutorial: file objects - Reading and writing to files [Video].
YouTube. https://youtu.be/Uh2ebFW8OYM
Python File Open. (n.d.). W3schools.
https://www.w3schools.com/python/python_file_handling.asp