Python Camp: Final Homework#

This notebook should be completed after the exercises in the Programming Techniques notebook.

How to Approach This Homework

Unlike the previous homework notebooks you completed, this notebook does not introduce new concepts or syntax. Instead, it offers you an opportunity to put into practice what you’ve learned throughout Python Camp. The exercises build toward a more complex solution, but we have tried to make each step explicit, so as to give you occasion to reflect on many of the concepts you have learned and how they fit together.

It’s quite possible that you can find hints to the solutions of these exercises in previous Python Camp homeworks, in code you wrote during in-class activities, and/or by Googling or using ChatGPT. You’re welcome to use any and all sources of information to complete these exercises. You’re also encouraged to ask your Python-Camp colleagues and facilitators for help!

At the same time, you might get the most out of these final exercises if you try to solve each one from memory before turning to other sources. Fluency in Python is like fluency in any other language: it comes with practice, and every mistake is itself instructive!

Instructions#

Below you will find a series of exercises. For each exercise, do the following:

  1. Run the first code cell under the exercise heading. This will execute some code needed to set up the problem.

  2. Reading the instructions and write code in the empty code cell (labeled with the comment Your code here). Run this code.

  3. Your code cell should include the comment line #Your code below. Please do not delete this comment, and if you accidentally delete the cell and have to recreate it, please insert that comment line at the top of the cell before entering your code.

  4. Assuming your code does not produce any errors, run the code in the cell below the Tests heading. Each line of this code consists of a Python assert statement.

    • If you see an AssertionError, don’t panic! The error is written so as to help you pinpoint the part of your code that’s not working as expected. After making a change in your code, re-run both the cell with your code and the cell with the tests. Repeat until you see no errors.

    • If you run the cell with the test code and you see no output, congratulations! That means that your code has passed all the tests and works as expected. Go to step 5 to submit.

  5. Submit your code by downloading this notebook from JupyterHub and uploading it to the GitHub repository for this assignment. See the documentation for detailed instructions.

How to Get Help

If you get stuck on any of these exercises, don’t hesitate to ask for help! Asking for help is not only part of the learning process; it’s part of nearly every programmer’s professional practice.

  • Post a question on the Python Camp Slack channel. Make sure you spell out both what you’re expecting your code to do, and what your code is actually doing. That way others can identify the problem right away. Including code snippets and/or screenshots from the notebook is usually a good idea.

  • Email the facilitators (PYTHON_CAMP@groups.gwu.edu). We’re happy to schedule a time to meet, either in person or via Zoom, and walk through your questions together.

Exercise 1#

The following code creates a variable, my_course, from a string describing a GW course, with a course code, number, and section.

my_course = 'WSTU 6566 10'

In the cell below, write some Python code that creates three new variables, my_dept_code, my_course_num, and my_section. Assign to each of those variables the portions of the string corresponding, respectively, to the department code (WSTU), the course number (6566), and the section (10). All three elements should be strings.

Note that it’s possible to accomplish this with a literal assignment, e.g., my_dept_code = 'WSTU', but doing so won’t help you solve the later problems that build on this one. Can you use one of Python’s string methods to accomplish this?

#Your code below
# ---- Do not delete this cell! ---- 

Tests for Exercise 1#

The cell below defines one or more tests that will be run to check your code.

Once you have completed your answer, run the cell. If you see no output, then contratulations, your code has passed the tests!

If, on the other hand, you see an AssertionError, it means that one of the tests has failed. Look at the error message for a hint as to where the problem lies.

assert my_dept_code == 'WSTU', f'Variable my_dept_code has the wrong value. Should be "WSTU" but is "{my_dept_code}".'
assert my_course_num == '6566', f'Variable my_course_num has the wrong value. Should be "6566" but is "{my_course_num}".'
assert my_section == '10', f'Variable my_section has the wrong value. Should be "10", but is "{my_section}".'

Exercise 2#

The following code reassigns the variable my_course to a new string with the same elements as above, except that here the name of the instructor follows the section number (separated by a space).

my_course = 'BSCI 1333 15 Thomson'

In the cell below, write some Python code that will create a dictionary called my_course_dict.

It should have the following keys:

  • dept_code

  • course_num

  • section

  • instructor

The keys should be assigned the appropriate values from the string my_course (as assigned in the cell above).

#Your code below
# ---- Do not delete this cell! -----

Tests for Exercise 2#

keys = ['dept_code', 'course_num', 'section', 'instructor']
values = ['BSCI', '1333', '15', 'Thomson']
assert 'my_course_dict' in locals() and isinstance(my_course_dict, dict), 'Variable my_course_dict has not been defined as a dictionary.'
for k in keys:
    assert k in my_course_dict, f'Key "{k}" was not found in my_course_dict'
for k,v in zip(keys, values):
    assert my_course_dict[k] == v, f'Value for my_course_dict["{k}"] should be "{v}", not "{my_course_dict[k]}".'

Exercise 3#

The following code defines a list of strings that represent courses. Each string consists of a department code, a course number, a section number, and an instructor’s name.

courses = ['CHEM 1001 10 Mack',
          'CHEM 1002, 10 Srinivasan',
          'CHEM 1002 11 Mack',
          'BISC 1100 10 Liu',
          'BISC 1102 10 Thomson',
          'PSC 2001 10 Wolters',
          'PSC 2001 11 Rath',
          'PSC 2001 12 Cho',
          'WSTU 6999 10 Cho',
          'WSTU 6999 11 Delaney']

In the cell below, write some code that will transform the data in the courses list into a list of dictionaries. Each dictionary should be structured as follows:

{'dept_code': 'CHEM',
 'course_num': '1001',
 'section': '10',
 'instructor': 'Mack'} 

In other words, each dictionary should have the same four keys, and there should be one dictionary for each of the courses in the courses list.

Call this new list courses_db (for “courses database”).

#Your code below
# ---- Do not delete this cell! -----

Tests for Exercise 3#

keys = {'dept_code', 'course_num', 'section', 'instructor'}
values = {'CHEM', '1001', '10', 'Mack'}
assert 'courses_db' in globals(), 'Variable courses_db is not defined'
assert isinstance(courses_db, list), 'Variable courses_db is not a list.'
assert isinstance(courses_db[0], dict), 'List courses_db does not contain a dictionary as its first element.'
assert len(courses_db) == len(courses), 'List courses_db is the wrong length.'
assert set(courses_db[0].keys()) == keys, 'The first dictionary in courses_db does not have the right keys.'
assert set(courses_db[0].values()) == values, 'The first dictionary in courses_db does not have the right values.'
assert set(courses_db[-1].values()) == {'WSTU', '6999', '11', 'Delaney'}, 'The last element of courses_db does not have the right values.'

Exercise 4#

Using the courses_db variable defined in Exercise 3, write a function, count_courses, to count how many courses a given instructor is teaching. The function should accept an argument corresponding to an instructor’s name, and it should return the number of courses in courses_db associated with that name. If the instructor is not in the database, your function should return 0.

For instance, if my_instructor is "Liu", your function would return 1. If my_instructor is "Cho", your function would return 2.

A skeleton for the function is defined in the cell below. Fill it in with your code.

#Your code below
# ---- Do not delete this cell! -----
def count_courses(instructor_name, courses_db):
    
    return course_count

Tests for Exercise 4#

assert count_courses('Rath', courses_db) == 1, f'Rath is teaching one course, but count_courses returns {count_courses("Rath", courses_db)}.'
assert count_courses('Mack', courses_db) == 2, f'Mack is teaching two courses, but count_courses returns {count_courses("Mack", courses_db)}.'
assert count_courses('Smith', courses_db) == 0, f'Smith is not teaching any courses, but count_courses returns {count_courses("Smith", courses_db)}.'

Submitting your homework#

If you’ve run all 4 code cells under the Tests headings, and if you see no error, congratulations! That means you’ve successfully completed this homework and are ready to submit it.

You will submit your homework via GitHub, which will simply run the same tests that you have run above, record any errors, and alert the Python Camp facilitators that you have made a submission.

Submitting this homework notebook (with no errors in the code) and attendance for all four days of Python Camp satisfy the requirements to receive the certificate of completion.