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. You’re welcome to revise your code as many times as you like until you get a working solution to the problem.

  4. When you’re satisfied with your solution, or if you feel stuck and unable to make further progress, submit your homework for feedback from the Python Camp instructors. See the bottom of this notebook 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! ---- 

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! -----

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! -----

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

Submitting your homework#

If you completed this homework in Google Colab, submitting it is straighforward.

  1. In Google Colab, click the Share button in the upper right-hand corner of the screen.

  2. In the box at the top of the sharing panel, enter PYTHON_CAMP@groups.gwu.edu to share your notebook with the team.

  3. Click the Copy link button at the bottom of the panel.

  4. Click the Done button to save your changes.

  5. The link to your shared notebook should now be in your computer’s clipboard. Open the Python Camp roster spreadsheet (where you recorded your attendance each day), and paste this link in the row containing your name, in the column marked Homework link.