Lab 1, CSC202, Spring 2024
1 Installing Py  Charm
2 Git
3 Setting up the environment
4 The rest of the lab
5 Data Definitions
6 Signature, Purpose Statements, Headers
7 Test Cases
8 Whole Functions
8.12

Lab 1, CSC202, Spring 2024🔗

1 Installing PyCharm🔗

As the syllabus hopefully makes clear, you are absolutely not required to use PyCharm. However, I want to make sure that if you want instructions and you don’t feel strongly about which environment you use, that I provide a reasonable set of instructions.

  1. Download PyCharm, the (free) Community Edition, from the JetBrains website. (You may have to scroll down to see the Community Edition.)

  2. Install Python 3.12. How you do this is going to depend on your operating system.

  3. Start PyCharm, create a new project.

  4. Go to project settings, click + at the top left corner of the packages list. and install the Mypy package.

  5. Add the MyPy plugin.

2 Git🔗

I’m not going to try to include a full intro to Git here; instead, I’m just going to tell you what you need to do for the lab:

  1. Sign up for a GitHub account, if you don’t already have one.

  2. Click on the invitation link to create your own private repository for this assignment.

  3. Check out your fresh new repo. It should contain a file named "lab1.py". But how?
    • VCS > Get From Version Control ...

    • Choose Version Control: Git

    • Paste the link from github

    • Authenticate. But how? It appears that for most folks, the easiest way that actually works is to paste a token from GitHub. PyCharm can help you generate this token.

  4. Edit this file to add a comment to Section 1, part 1, containing the word "persnickety".

  5. Stage, commit, and push your change.

  6. Log into github using their web interface; make sure you can see your Lab 1 repo, and the word "persnickety."

3 Setting up the environment🔗

Type in this program, and make sure that you can get MyPy to show you an error:

def f(x: int) -> None: return 19

4 The rest of the lab🔗

The rest of this lab contains many sections and many numbered parts of each section. You’ll see that the "lab1.py" template comes with lines indicating each of these sections and parts. You should put your answers to each part in the corresponding place in the code. Your code will be sliced and diced programmatically, so if you put your work in the wrong part of the file, or delete or alter the existing headers, you might not get credit for your work, and that would make us *both* sad.

You should push your work to github after each part is done.

5 Data Definitions🔗

Each of the following programs requires a data definition. In many cases, it may be just a single TypeAlias line. In other cases, the data definition may require creation of a new class. In this case, be sure to create the class using a @dataclass declaration, as discussed in class.

Provide two examples for each data definition.

  1. A program that converts celsius to fahrenheit temperatures must accept a celsius temperature as input, and return a fahrenheit temperature as a result. Write a data definition for each of these two kinds of values.

  2. A store might maintain a database that includes prices for various items. Write a data definition for a price (just the price), in cents.

  3. Following on the prior item, write a data definition for a price record; it should include both the item’s name and its price.

  4. A web browser might maintain information about open tabs. Develop a data definition for an open tab, that includes the URL being visited and the most recent date on which it was loaded. (Yes, this data definition may require sub-data-definitions).

6 Signature, Purpose Statements, Headers🔗

For each of the following functions, provide a a purpose statement, and a header including input and output types. You do not need to provide the data definitions; you may assume that this has already been done. Many of these problems are underspecified; make reasonable assumptions.

  1. A function that accepts a price and adds sales tax,

  2. a function that finds the price for a named item in a store’s price database,

  3. a function that computes the median income using a given geographic region and given database, and

  4. a function that accepts a given geographic region and database and determines which cities overlap with that region.

7 Test Cases🔗

For each of the following, write a purpose statement, a header (including types), and a set of test cases. Each test case should take the form of a method whose name begins with test, containing a sequence of calls to self.assertEqual.

None of these require a separate data definition.

In order to allow your code to type-check and run, you should add a near-trivial body to each function that simply returns something of the correct type. Note that I don’t expect the test cases to pass, or the function bodies to contain anything sensible; the point is to write correct test cases.

  1. A function that accepts three distinct numbers and returns the second largest,

  2. a function that accepts a string and returns true if it has no capital letters,

  3. a function that accepts the names of two states and returns the "northernmost" one; that is, the one that contains the point closest to the north pole.

8 Whole Functions🔗

Follow the design recipe (data definitions if necessary, purpose statement, header with tests, test cases, fill in body) to design the following functions.

Place all of your tests for this section in a single TestCase class, so that you can run them. (This will also be helpful for automatic grading.)

Apologies to those of you that experience flashbacks during the following problems.

  1. Develop the f2m function, that accepts a length represented as a number of feet and returns the corresponding length in meters.

  2. Develop a data definition for a Musical Note, which includes a pitch represented as a frequency in Hz, and a duration represented as a length in seconds.

  3. Develop the up_one_octave function, that accepts a note and returns a new note that is higher by one octave. In other words, its frequency is doubled.

  4. (OPTIONAL) Develop the frequency_diff, that accepts two notes and computes the number of half-steps between them by dividing the logarithm of the ratio of the notes by the logarithm of the twelfth root of 2.