CSE 163, Winter 2021: Assessment 0: Infrastructure Setup

See EdStem Resources for a link to introduction video for this assignment.

Overview

This assessment is a warm-up to get you used to the development process we ask you to do in this class and how to submit your assessment on Ed. It will have a lot of similarities with what you did in Section 1, but in the context of what a take-home assessment will look like.

Learning Objectives

This assessment will show students have mastery in the following skills:

  • Follow a Python development workflow for this course, including:
    • Use the course infrastructure (flake8, test suites, course resources).
  • Write documentation for their code.
  • Write unit tests to test each function written including their edge cases.
  • Be able to write and test a function in python.

Expectations

Here are some baseline expectations we expect you to meet:

  • Follow the course collaboration policies

  • While most assignments we support the option to download the assessment files and develop locally, for this warmup assignment we do not. The reason is a fair amount of this assignment is getting used to turn-in system of Ed.

Part 0: Understanding the Setup

For this assignment, we want you to get used to the workflow of developing code for this class and turning in your assignment on Ed. This involves a little bit of finding and fixing bugs in your code and writing tests to verify your code works. This assignment is a bit artificial in the sense that most of the code is provided for you (which will not be true for future assignments). However, we still think that there is a lot of value to doing this practice.

Navigate to Ed to view the assignment. This view should look very similar! It's almost exactly the same as a coding challenge (i.e. practice problem) from the lessons. You should see the following files in the assignment:

  • hw0.py The (potentially buggy) code for the problems described below.
    • Notice that it doesn't have the main method pattern! The reason is we don't need hw0.py to be a runnable program (it only defines functions). Since it's not meant to run, we don't need to take this extra main-method pattern precaution like we have in all of the other programs we have written so far.
  • hw0_test.py The file to put your own tests you write for the assignment (described below).
  • cse163_utils.py A helper file we provide that has code to help you test your code. You do not need to look at or modify this file ever.

In every assignment, we will define a set of problems we would like you to solve. Even if the assignment is solving some larger problem, we usually break it up into small sub-problems for you to help manage the work. For this assignment, we have already implemented some solutions to these problems below. We are providing specifications for these problems though since our solution may or may not have bugs in it!

Problem 0: funky_sum

Write a function (we already did!) called funky_sum that does a special sum to combine two numbers. The function should take three parameters, the first two are numbers a and bto combine and a third mix is a number to determine the ratio to use from each. mix acts as a "slider" to control how much to use of each number. If mix is 0 or less, the result should be the same as a. On the other hand, if mix is 1 or more, the result should be the same as b. For any value of mix between 0 and 1, it should add 1-mix times a and mix times b (the technical name for this operation is a "linear interpolation"). This is clearer with some examples (return value shown in the comment after the line).

funky_sum(1, 3, 0.5)   # 2
funky_sum(1, 3, 0)     # 1
funky_sum(1, 3, 0.25)  # 1.5
funky_sum(1, 3, 0.6)   # 2.2
funky_sum(1, 3, 1)     # 3

Problem 1: total

Write a function (we already did!) called total that takes a number n and returns the sum of the integers from 0 (inclusive) to n (inclusive). If n is negative, the function should return the value None instead.

Part 1: Running Code

Notice that for this assignment, there is no need to run hw0.py since it doesn't do anything but define functions (e.g. it doesn't print any output on its own). That means the only file you need to run on this assignment is hw0_test.py. On most assignments, this is not quite the case. There will commonly be cases where your main program does some interesting computation (e.g. trains a machine learning model) but you also have a test program to test things. Unfortunately, this does not line up with Ed's model for assignments since they assume only one file will be run, therefore only allowing us to specify the "Run" button to run one of the files. This means we generally can't support the "Run" button on each assignment because it's not clear which of your files (main or test) that we should run.

This means you will need to be comfortable enough with the terminal to run your Python programs and flake8 for the assignments. Don't worry, we will show you how to do this for the first few assignments so you get comfortable with it. Recall that to use the terminal on Ed, you click on the terminal tab on the bottom of the editor and click the big activate button. You then just type in commands to get the computer to run particular programs for you! For this assignment, the terminal commands you will need are:

  • python hw0_test.py to run the tests you write.
  • flake8 hw0.py to run flake8 on your solution.
  • flake8 hw0_test.py to run flake8 on your tests.
  • Alternatively, you can actually just type flake8 (with no file name after) and it will run flake8 on all Python files in the directory!

You can also re-visit Section 1 and the Ed Tutorial readings on Ed to see how to do this!

Part 2: Your Tasks

This section outlines the tasks we want you to finish on this assignment that you will need to be able to do for the assignments to come. The following tasks will require you modifying hw0.py and hw0_test.py. You do not need to modify (or even open) cse163_utils.py.

Problem 0: Write Documentation

There is no documentation in the provided code! You do not need to necessarily do anything right now for this step, but we recommend writing your documentation while you code to build up the habit. Writing documentation for your function is a great way to start to get you thinking about what your method needs to do. You should go through and add the following documentation:

  • Each function in hw0.py and hw0_test.py should have a doc-string comment describing their parameters, returns and behavior.
  • Each of hw0.py and hw0_test.py should have a doc-string comment as the first lines of that file. This comment should describe what the whole file is for. A doc-string for a file looks the same as a doc-string for a function, but goes as the first lines of the file. Each doc-string for the file should have your name and your section in it as well. Since this assignment doesn't really have a context, the description for hw0.py can just say something like "Implements the functions for HW0".

Importantly, it helps to think about who the audience of your comments will be. They are interested in knowing WHAT your function does and how to call it, they are not looking for you to simply rewrite the code in English. You commonly want to avoid phrases that are telling the reader how you accomplish something (e.g. "uses a for loop") since that is a little too detailed into the specific implementation. Instead, describe your function at a high-level and make sure to describe any import edge-cases that the reader might want to know about (e.g. does your function ever return None?).

Your task: Document the code as described above in both files hw0.py and hw0_test.py.

Problem 1: flake8

The provided code has some style errors according to flake8. Run flake8 in the terminal on Ed with the command flake8 file.py where file.py is the name of one of those files. This will report all the style errors in that file. Your task for this part is to go fix them all so that flake8 reports no errors. You will likely find the flake8 reference on the resources page to be very helpful in identifying what the flake8 errors are.

Your task: Fix all of the flake8 errors.

Problem 2: Testing

We use testing to help us feel confident that the programs we write are correct. Testing involves calling the functions you wrote with some input and comparing the output with some expected value.

We can't just call functions defined in hw0.py from hw0_test.py since they come from a different file. To fix this, we have to import hw0 in hw0_test.py and any time we call a function from hw0.py we have to prefix it with hw0. (e.g. hw0.total(10)). You do not need to understand how imports work yet, just know that you will need to use this syntax to call the functions you define in your hw0.py.

We have provided a function called assert_equals that takes an expected value and the value returned by your function, and compares them: if they don't match, the function will crash the program and tell you what was wrong. The starter file hw0_test.py has some test-cases already included to show you an example of how to write tests. Note that assert_equals is defined in cse163_utils.py, but imported in a special way so you don't have prefix all the function calls with cse163_utils..

Your task: The provided test function for total doesn't test a really important case described in the spec for that problem. Write a test-case in that function to test that special case is handled correctly.

Problem 3: Testing - Part 2

You might have noticed that we only provided one test function even though there were three functions that were supposed to be "implemented" in this assignment. In general, you should have a separate test function for each function that you were supposed to write in the assignment to make sure you're confident that it works.

This will be very helpful because there is some sort of bug in our starter code! When you press Mark, the provided code should fail some tests. To get you in the practice of writing your own tests and debugging your solution, our test cases from the Mark interface are intentionlly vague. This will not be purposefully done in future assignments, but as we mentioned in Section 1, it is a common fact that it is difficult to use the Mark interface to debug your code. The next problem asks you to fix the bug.

Your task: You should write a function named test_funky_sum in hw0_test.py that makes at least 4 calls to assert_equals. At least two calls should be examples shown in the specification and at least two should be new examples you make up. Make sure this function is actually called by calling it from main!

Problem 4: Fix the bug

Your task: Fix the bug(s) in funky_sum. You should pass all of your own tests and all of ours.

Problem 5: Write swip_swap

Your task: Write a function called swip_swap that takes a string source and characters c1 and c2 and returns the string source with all occurrences of c1 and c2 swapped. You may assume that the c1 and c2 are single characters. Here are some example calls:

swip_swap('foobar', 'f', 'o')  # 'offbar'
swip_swap('foobar', 'b', 'c')  # 'foocar'
swip_swap('foobar', 'z', 'c')  # 'foobar'

Problem 6: Test swip_swap

Your task: You should write a function named test_swip_swap in hw0_test.py that makes at least 4 calls to assert_equals. At least two calls should be examples shown in the specification and at least two should be new examples you make up. Make sure this function is actually called by calling it from main!

Evaluation

Your submission will be evaluated on the following dimensions:

  • Your solution works externally for the cases shown.
  • For this assignment, we will show you all tests that will be run against your submission on Ed to give you a better idea how to test your code. In future weeks, some tests will 'hidden' in that they will show you whether you pass/fail the test, but it won't show the exact call being made. The hidden tests will be things described in the spec so they shouldn't be a surprise, but an important part of this class is learning how to test your own solution to be very sure it's correct. If you find yourself failing some of these hidden tests, a good sign is that you need to write some more of your own tests to test more cases.
  • Your code meets our style requirements:
    • All files submitted pass flake8.
    • Your program should be written with good programming style. This means your code should satisfy the requirements within the CSE 163 Code Quality Guide.

Submission

This assignment is due by Thursday, January 14 at .

You should submit your finished hw0.py and hw0_test.py on Ed.

You may submit your assignment as many times as you want until the deadline for the initial submission. Recall on Ed, you submit by pressing the "Mark" button. Work after the due date will not be accepted for an initial submission. Please see the syllabus for the policies on initial submissions and resubmissions.