See EdStem Resources for a link to introduction video for this assignment.
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.
This assessment will show students have mastery in the following skills:
flake8
, test suites, course resources).Here are some baseline expectations we expect you to meet:
Follow the course collaboration policies
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.
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!
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 b
to 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
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.
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.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!
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
.
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:
hw0.py
and hw0_test.py
should have a doc-string comment describing their parameters, returns and behavior.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
.
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.
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.
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!
Your task: Fix the bug(s) in funky_sum
. You should pass all of your own tests and all of ours.
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'
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!
Your submission will be evaluated on the following dimensions:
flake8
.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.