.py
files containing your code for problem 3 and an image files containing the screenshot requested in problem 2.This homework assignment must be submitted in Gradescope by Noon central time on Wednesday 17 January 2024.
(Note:That's a deviation from the usual schedule where homework is due on Tuesdays.)
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
This assignment is based on the first week of lecture, so it's basically checking that you have Python working and giving you a MCS 260-level coding exercise. Homework 2 will be similar but more challenging. Homework 3 will be about new material.
The materials you may refer to for this homework are:
This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:
Points | Item |
---|---|
4 | Autograder |
5 | Problem 2 |
5 | Problem 3 |
14 | Total |
The part marked "autograder" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc. The result of these checks is shown immediately after you submit.
Ask your instructor or TA a question by email, in office hours, or on discord.
Homework problem numbers will always start at 2, because Gradescope uses "Problem 1" to report the Autograder results---i.e., a few automated checks of whatever you submit for the other problems.
This will happen on every assignment this semester.
checkup.py
)¶You probably downloaded and ran the "Python checkup script" in the first lab. If not, please download it now and save it somewhere you can find it:
For this problem, I'd just like you to show that you can run this script on the computer you plan to use for MCS 275 and that you have a recent version of Python.
To do that, open a terminal, change to the directory containg checkup.py
, then run the command python3 checkup.py
(or replace python3
with your interpreter name, if different). Then take a screenshot of the part of your screen where the output from the script is displayed.
By "take a screenshot" I mean "capture the contents of part of your computer screen as an image file", which is possible in every popular operating system (e.g. Windows, MacOS, linux). The exact way to do it varies, but you can find instructions here:
Save the screenshot as an image file (ideally PNG
) and upload that file as part of your homework 1 submission.
Here's an example of what the image might look like. It was captured on the instructor's Windows PC by pressing the key combination (Windows key) + (shift) + (s):
For full credit your image must:
checkup.py
Note: You don't need to submit an image of your entire desktop! Just capture the part with the checkup.py output. Don't worry about cropping perfectly, it's OK if a bit of the background is visible around the edges.
There was a typo in the original assignment; the label "Problem 2" was used for both problems.
A triangle wave is the graph of a piecewise-linear function that alternates between having slope 1 and -1 and is periodic. Here's a picture of one:
One way to render something like this using text is to print a combination of forward and backward slashes (\
and /
) and spaces. It's natural to print a certain number of whole cycles (triangular humps). For example, here is a a block of text that makes a picture of a triangle wave of height 2 that shows 6 cycles:
/\ /\ /\ /\ /\ /\
/ \/ \/ \/ \/ \/ \
and here is one of height 4 that shows 3 cycles:
/\ /\ /\
/ \ / \ / \
/ \ / \ / \
/ \/ \/ \
and finally one of height 1 that shows 10 cycles:
/\/\/\/\/\/\/\/\/\/\
Note: On a mobile device or in a narrow window, line wrapping might make the text blocks above not look the way they're supposed to. In case that happens to you, here's a picture of what the three examples above should look like:
In each case, the height is the number of lines of text that make up the picture, and the number cycles is the total number of upward-pointing triangular "peaks".
Create a file called hwk1prob3.py
that contains a single function with the following definition
def triangle_wave(height, cycles):
Write the body of that function so it works as follows:
height
that shows cycles
cycles.Upload hwk1prob2.py
as part of your homework 1 submission.
Here are some examples where the expected return values of this function are printed. You can use the same statements to test your work.
print(triangle_wave(4,6))
print(triangle_wave(3,9))
print(triangle_wave(8,1))
To produce an single backslash in a Python string, you need to put two backslashes in a row inside the quotation marks in your code. That's because Python considers a single \
to be the start of an escape code like \n
(newline) that gets special handling. Two backslashes in a row means "really, I just want a backslash".
For example note the problem if you try to print "\"
:
print("\") # error, because \" is considered a character and so the string has no end
And here's how you can actually print a single backslash.
print("\\") # the correct way to make a string containing one backslash
Finally, this is proof that Python considers "\\"
to be a single character.
len("\\")