0% found this document useful (0 votes)
8 views5 pages

Programming Assignment Unit 7

The document describes the function invert_dictionary(student_courses), which reverses the mapping of students to their enrolled courses, transforming it into a structure where courses are keys and students are values. It provides an example with three students and their respective courses, illustrating how the function processes the input to create an inverted dictionary. The output allows for easy analysis of course enrollments by listing students under each course.

Uploaded by

ashigull487
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Programming Assignment Unit 7

The document describes the function invert_dictionary(student_courses), which reverses the mapping of students to their enrolled courses, transforming it into a structure where courses are keys and students are values. It provides an example with three students and their respective courses, illustrating how the function processes the input to create an inverted dictionary. The output allows for easy analysis of course enrollments by listing students under each course.

Uploaded by

ashigull487
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Programming Assignment Unit 7

Ayesha Gul

University of the People

CS 1101: Programming Fundamentals

Dr Josiah Jolaoluwa

20th of March, 2025


2
Programming Assignment Unit 7

The function invert_dictionary(student_courses) is designed to reverse the mapping of

students and their enrolled courses. In the given dictionary, students are the keys, and their

respective course lists are the values. The goal is to transform this structure so that courses

become the keys, and the students enrolled in each course become the values. To achieve this, the

function initializes an empty dictionary called inverted_dict. It then iterates through each student

and their corresponding courses using a for loop. For each course in a student's list, the function

checks whether the course already exists as a key in inverted_dict. If not, it initializes an empty

list for that course. Then, it appends the student's name to the list corresponding to that course.

Once all students and courses are processed, the function returns the newly structured dictionary.

The program also prints both the original and inverted dictionaries to showcase the

transformation. This approach effectively groups students based on the courses they are enrolled

in, providing an efficient way to analyze course enrollments.

Input:
3
Programming Assignment Unit 7

Explanation:

The input is a dictionary named student_courses, where each key represents a student,

and the corresponding value is a list of courses they are enrolled in. In this case, three

students—Ayesha, Ashhar, and Rafay—are registered for various courses. Ayesha is taking

"CS1101," "CS2402," and "CS2001." Ashhar is enrolled in "CS2402," "CS2001," and "CS1102."

Rafay is registered for "CS1101" and "CS2001." The structure of this input allows for multiple

students to share the same course. The goal of the function is to reorganize this data by making

courses the keys and listing the students under each respective course.

Output:
4
Programming Assignment Unit 7

Explanation:

The function invert_dictionary(student_courses) processes the input and returns an

inverted dictionary where courses become the keys, and the students who are enrolled in each

course become the values. In the above output, each course has a list of students who are

enrolled in it. For example, "CS1101" is taken by Ayesha and Rafay, while "CS2001" is shared

by all three students. The function achieves this by iterating through each student and their

courses, ensuring that each course is properly mapped to its respective students in a structured

format. This transformation allows a teacher to quickly see which students are in each course,

simplifying course management.


5
Reference List

Python on Windows. (n.d.). Python Documentation.

https://docs.python.org/3/using/windows.html

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.

https://greenteapress.com/thinkpython2/thinkpython2.pdf

You might also like