Problem Set 4: Lists, Dictionaries, Functions & Imports
Problem Set 4: Lists, Dictionaries, Functions & Imports
Overview
This assignment focuses on working with Python data structures (lists and dictionaries), writing functions, using imports, and exploring basic recursion.
GitHub Classroom Link: Accept Assignment
Learning Objectives
- Master list operations and comprehensions
- Work with dictionaries and nested data structures
- Write functions that process collections
- Use Python modules (json, random, etc.)
- Understand basic recursion concepts
Assignment Structure
This problem set consists of 4 required problems and 1 optional bonus problem:
Problem 1: List Operations (8 functions)
Practice working with Python lists - creating, modifying, filtering, and transforming them.
Functions to implement:
create_number_list(start, end)- Create a list of numbersfilter_even_numbers(numbers)- Filter even numberssquare_numbers(numbers)- Square each numberfind_max_min(numbers)- Find maximum and minimumremove_duplicates(items)- Remove duplicates while preserving ordermerge_lists(list1, list2)- Merge two lists alternating elementslist_statistics(numbers)- Calculate statistics (sum, average, count, etc.)chunk_list(items, chunk_size)- Split list into chunks
Problem 2: Dictionary Operations (9 functions)
Practice working with Python dictionaries - creating, accessing, modifying, and nesting them.
Functions to implement:
create_student_record(name, age, major, gpa)- Create a student dictionaryget_value_safely(dictionary, key, default)- Safe dictionary accessmerge_dictionaries(dict1, dict2)- Merge two dictionariescount_word_frequency(text)- Count word frequency in textinvert_dictionary(dictionary)- Swap keys and valuesfilter_dictionary(dictionary, keys_to_keep)- Filter dictionary by keysgroup_by_first_letter(words)- Group words by first lettercalculate_grades_average(students)- Calculate grade averagesnested_dict_access(data, keys)- Access nested dictionary values
Problem 3: Mini Contact Manager (9 functions)
Build a simple contact manager using lists and dictionaries - combines all concepts from Problems 1 & 2.
Functions to implement:
create_contact(name, phone, email)- Create a contact dictionaryadd_contact(contacts, name, phone, email)- Add contact to listfind_contact_by_name(contacts, name)- Find contact by namesearch_contacts(contacts, search_term)- Search by name or phonedelete_contact(contacts, name)- Remove a contactcount_contacts_with_email(contacts)- Count contacts with emailget_all_phone_numbers(contacts)- Extract all phone numberssort_contacts_by_name(contacts)- Sort contacts alphabeticallycontact_exists(contacts, name)- Check if contact exists
Problem 4: Data Persistence with JSON (9 functions)
Learn to use Python modules (imports) and save data to files using JSON.
Functions to implement:
save_to_json(data, filename)- Save data to JSON fileload_from_json(filename)- Load data from JSON filesave_contacts_to_file(contacts, filename)- Save contacts to JSONload_contacts_from_file(filename)- Load contacts from JSONappend_contact_to_file(contact, filename)- Add contact to existing filebackup_file(source_filename, backup_filename)- Create backup copyget_file_stats(filename)- Get file statisticsmerge_json_files(file1, file2, output_file)- Merge two JSON filessearch_json_file(filename, key, value)- Search JSON file
Bonus: Introduction to Recursion (8 functions - Optional)
Learn about recursive functions - functions that call themselves!
Functions to implement:
factorial(n)- Calculate factorial recursivelycountdown(n)- Print countdown using recursionsum_list(numbers)- Sum list recursivelyfibonacci(n)- Calculate Fibonacci numberpower(base, exponent)- Calculate power recursivelyreverse_string(text)- Reverse string recursivelycount_down_list(n)- Create countdown listflatten_list(nested_list)- Flatten nested list
Getting Started
- Accept the assignment using the GitHub Classroom link above
- Clone your repository to your local machine or Nuvolos
- Complete each problem in its respective file (
problem1.py,problem2.py, etc.) - Test your solutions by running each file:
python problem1.py - Commit and push your work regularly
Testing Your Code
Each problem file includes built-in test cases. Run them with:
python problem1.py
python problem2.py
python problem3.py
python problem4.py
python bonus_recursion.py # if completed
For automated testing:
pip install pytest
pytest test_assignment.py -v
Submission
Push your completed code to your GitHub repository:
git add .
git commit -m "Complete Problem Set 4"
git push origin main
GitHub Actions will automatically run tests on your code.
Tips for Success
- Start early - Give yourself time to debug
- Test incrementally - Test each function as you write it
- Use print statements - Debug by printing intermediate values
- Read error messages - They tell you what went wrong
- Ask for help - Use Discord or office hours if stuck
Resources
- Python Functions Documentation
- Python Data Structures
- JSON Module Documentation
- Week 4 Lesson Guide
- Week 4 Practice Slides
Academic Integrity
This is an individual assignment. You may:
- Use Python documentation
- Discuss concepts with classmates
- Ask questions on Discord
You may NOT:
- Copy code from other students
- Use AI to generate complete solutions
- Submit work that is not your own
Assessment
This is an ungraded practice assignment designed to build your programming skills. Focus on understanding concepts rather than just getting tests to pass.
Good luck! Remember: struggling with problems is part of learning to code.