0% found this document useful (0 votes)
31 views3 pages

Inverting Student-Course Dictionary

The document outlines a programming assignment where a Python function is created to invert a dictionary that associates students with their courses, transforming it into a dictionary with courses as keys and lists of students as values. The function ensures that each course key can have a maximum of three students enrolled. Sample input and output are provided to demonstrate the functionality of the inverted dictionary.

Uploaded by

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

Inverting Student-Course Dictionary

The document outlines a programming assignment where a Python function is created to invert a dictionary that associates students with their courses, transforming it into a dictionary with courses as keys and lists of students as values. The function ensures that each course key can have a maximum of three students enrolled. Sample input and output are provided to demonstrate the functionality of the inverted dictionary.

Uploaded by

Zin Myo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1|Page

Programming Assignment Unit 7

By

Zin Myo Tun


Department of Computer Science, University of the People
CS-1101-01: Programming Fundamentals - AY2024-T2

Supervised By

Dr Vikas Thada
Instructor, Department of Computer Science
University of the People

January 4, 2024

Zin Myo Tun, C1839791


2|Page

Programming Assignment Unit 7


A Python function that inverts a dictionary where students are associated with their respective
courses, and the output is a dictionary with courses as keys and lists of students enrolled in each
course:

The invert_dictionary function is designed to address the teacher's requirement of having a


dictionary with courses as keys and students enrolled in each course as values. The function uses
two nested loops to iterate through the original dictionary, where each key is a student, and the
corresponding value is a list of courses for that student.

For each course in the list of courses for a student, the function checks whether the course is
already a key in the inverted_dict. If the course is already a key, it appends the current student to
the list of values for that key, but with a constraint: each key can have a maximum of three
values (students). This ensures that the teacher gets exactly three students for each course.

Zin Myo Tun, C1839791


3|Page

If the course is not a key, it creates a new key with the course and sets the value as a list
containing the current student. This process continues until the inverted dictionary is fully
constructed.

The sample input provided includes two students ('Stud1' and 'Stud2') with their respective
courses. The output demonstrates the inverted dictionary, satisfying the teacher's requirement,
with courses as keys and three students enrolled in each course as values (or fewer if there are
not enough students).
Word Count – 240 words
(Allen Downey, 2015)

References
Allen Downey, J. E. (2015). How to Think Like a Computer Scientist. In A. Downey, Think Python. 9
Washburn Ave, Needham MA 02492: Green Tea Press.

kjdElectronics. (2017, August 5). Python beginner tutorial 8 - For loop, lists, and dictionaries [Video].
YouTube. [Link]

Python Dictionaries. (n.d.). W3schools.

Zin Myo Tun, C1839791

Common questions

Powered by AI

The invert_dictionary function demonstrates an application of for loops by iterating through each student and their list of courses in the original dictionary. Conditional logic is applied when checking whether a course already exists as a key in the inverted dictionary and whether the list of students associated with each course has reached its maximum capacity. This use of for loops combined with if-else statements showcases effective iteration and decision-making in Python programming.

Using nested loops can lead to increased computational complexity, especially as the size of the input data—the number of students and courses—grows, potentially reducing the function's efficiency. Challenges include managing index errors and ensuring that each iteration correctly updates the output dictionary without creating performance bottlenecks. Care must be taken to avoid redundancy and to handle dynamic key creation subtly, particularly when involving multiple conditions, like student number limits for each course.

Transforming the original student-course dictionary into the inverted format with courses as keys and students as values can be useful for teachers and administrators who want to easily access and manage course enrollments. It allows quick lookup of students registered for a given course and helps in managing course capacity efficiently, especially when limitations like maximum enrollment are considered.

Dictionary data structures facilitate the efficient mapping and retrieval of data by associating unique keys with specific values. In the problem presented, dictionaries allow for the straightforward inversion of student-course data, enabling quick lookups of course enrollments and ensuring that each course key corresponds to a precise, constrained list of students. Their intrinsic ability to handle dynamic and complex data structures supports logical re-configuration and constraint enforcement.

Allowing more than three students per course could exceed the intended capacity constraint set by the teacher, leading to over-enrollment issues. This could complicate classroom management, strain resources, and affect the quality of instruction if too many students are handled per course. Moreover, it might disrupt the specific requirement and criterion the function was designed to meet originally, potentially causing errors in scheduling and resource allocation.

The implementation of the invert_dictionary function underscores the importance of constraints as critical elements in software design, guiding data structuring and functional behavior towards fulfilling specific needs, such as enrollment limits. Constraints ensure that the software adheres to predefined rules and performance criteria, thereby enhancing reliability and purposefulness of the output. They also prevent data inconsistencies and operational issues by enforcing logical bounds within which the system operates.

The invert_dictionary function uses a constraint within its nested loops to ensure that each course, which serves as a key in the inverted dictionary, has at most three students in its list of values. During iteration, when a course is already present as a key, the function only appends a student to the list if the current number of students is less than three. Thus, maintaining a maximum of three students per course.

Checking if a course is already a key in the inverted dictionary is necessary to determine whether to append a student to an existing list of students or to create a new entry. This step avoids overwriting existing data and maintains the structure where courses map correctly to all enrolled students. It ensures that no duplicate key entries occur, which is crucial for data integrity and prevents logical errors in dictionary operations.

When creating such a function, one must consider ensuring that the transformation logic correctly iterates through the existing student-course data. It must handle the creation of new keys dynamically for courses and efficiently manage list operations to append students to the appropriate course key while respecting constraints such as maintaining a maximum number of students per course key. Additionally, edge cases such as handling students with no courses or courses with more than available students must be considered.

The invert_dictionary function is designed to create a dictionary with courses as keys and lists of students enrolled in those courses as values. It addresses the teacher's requirement by ensuring that each course has a maximum of three students. This is done using nested loops to iterate over the original dictionary of students and their respective courses, and manipulating the data to store it in the desired inverted format.

You might also like