A document from MCS 275 Spring 2024, instructor Emily Dumas. You can also get the notebook file.

MCS 275 Spring 2024 Homework 1 Solutions

  • Course Instructor: Emily Dumas

Instructions:

  • Complete the problems below, which ask you to create Python scripts or other files.
  • Upload the files directly to gradescope, i.e. upload the .py files containing your code for problem 3 and an image files containing the screenshot requested in problem 2.

Deadline

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

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Content

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.

Resources you may consult

The materials you may refer to for this homework are:

Point distribution

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.

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

Problem 1 doesn't exist

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.

Problem 2: Show us that Python is working (with 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):

Windows screenshot of checkup.py output

For full credit your image must:

  • Show the full output of checkup.py
  • Indicate that you have some version of Python 3 running
    • MCS 275 asks you to have 3.10 or higher, but any 3.x version will get you credit here.
  • Be captured by you on a computer you plan to use for MCS 275 coursework.

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.

Solution

The problem statement contains a sample screenshot.

Problem 3: Triangle wave

There was a typo in the original assignment; the label "Problem 2" was used for both problems until this was corrected after the deadline.

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:

triangle wave graph

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:

text triangle waves

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

In [ ]:
def triangle_wave(height, cycles):

Write the body of that function so it works as follows:

  • The arguments are assumed to be positive integers.
  • The function returns a string containing only spaces, newline characters, and slashes.
  • If the returned string is printed, it makes a picture of a triangle wave of height height that shows cycles cycles.

Upload hwk1prob3.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.

In [30]:
print(triangle_wave(4,6))
   /\      /\      /\      /\      /\      /\   
  /  \    /  \    /  \    /  \    /  \    /  \  
 /    \  /    \  /    \  /    \  /    \  /    \ 
/      \/      \/      \/      \/      \/      \

In [31]:
print(triangle_wave(3,9))
  /\    /\    /\    /\    /\    /\    /\    /\    /\  
 /  \  /  \  /  \  /  \  /  \  /  \  /  \  /  \  /  \ 
/    \/    \/    \/    \/    \/    \/    \/    \/    \

In [32]:
print(triangle_wave(8,1))
       /\       
      /  \      
     /    \     
    /      \    
   /        \   
  /          \  
 /            \ 
/              \

Solution 1

This way of solving it uses strings that look like " /" and "\\ " to build each row, then shifts those strings left or right by one character to make the next row.

In [9]:
def triangle_wave(height, cycles):
    "Display a triangle wave using text-based graphics"
    ascent = " "*(height-1) + "/"    # something like "    /"
    descent = "\\" + " "*(height-1)  # something like "\\    "
    s = ""
    for shift in range(height):
        t = ascent + descent # horizontal slice of one triangular hump
        ascent = ascent[1:] + ascent[:1] # shift the ascending part right
        descent = descent[-1:] + descent[:-1] # shift the descending part left
        s += t*cycles + "\n" # repeat the slice `cycles` times with newline at the end
    return s

Solution 2

Here's another approach that constructs each row from scratch rather than using the way one row is derived from the one before it. It also uses nested loops to make the cycles, rather than the multiplication operator used in solution 1.

In [1]:
def triangle_wave(height, cycles):
    "Display a triangle wave using text-based graphics"
    s = ""
    for i in range(height): # height = number of lines
        for _ in range(cycles): # assemble one line using `cycles` identical pieces
            s += " "*(height-i-1) + "/" + " "*i  # next / and space on each side
            s += " "*i + "\\" + " "*(height-i-1) # next \ and space on each side
        s += "\n" # newline after each row
    return s