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

Quiz 1

MCS 275 Spring 2021 - Emily Dumas

Instructions:

The instructions are long because this is the first quiz. In the future, an abbreviated set of instructions will suffice!

General

  • Quizzes are INDIVIDUAL. Work on this yourself.
  • Quizzes are closed-book and online resources are not permitted unless the quiz instructions make an exception.

Resources you are allowed to consult

How to work on this quiz

  • Each problem on this quiz asks you to create a Python source file (.py).
  • Prepare these in Visual Studio Code. You can use syntax highlighting and any tips and assistance that this editor provides (with or without the Python extension installed).
  • Run your code and make sure it works before submitting it.

How to submit

  • Use the course web page to open Gradescope
  • Within Gradescope, you may see "Quiz 1" on the home screen, or you may need to select "Assignments" to see it.
  • Select "Quiz 1" and upload all of your .py files using the dialog box that pops up. (It allows you to upload multiple files before you hit submit, and you should do that.)
  • Read the report that is generated by the autograder, and then try to fix any tests that did not pass. A failing test will contain a description of what went wrong, which should help you locate the problem. If you have trouble interpreting the autograder report, reach out to the instructor or TA.

Don't panic

If you have a lot of trouble with this quiz, remember that you can use your monthly exemption on it. Email the TA before the quiz deadline. If you do this, it would be a good idea to contact the instructor or TA in office hours to discuss what you can do to get on track for success on future quizzes.

Problem 1 doesn't exist

In Gradescope, the score assigned to your quiz submission by the autograder (checking for syntax and docstrings) will be recorded as "Problem 1". Therefore, the numbering of the actual problems begins with 2.

Problem 2: Fixme

The code below does something, but it doesn't obey the course coding standards. Modify it so that it does the same thing, but follows the coding standards. You'll need to figure out what it does (to some extent) to choose meaningful function and variable names; running the code will probably be helpful in that effort. Add at least one comment that would help someone reading it for the first time understand what's going on.

Save your fixed version as fixed.py and submit it.

In [ ]:
# TODO: Fix this code
def xx(x,y):
 return x[0]+". "+y
x0 = "David"
L = [["David","Dumas"],["Alice","Ball"],["Cornelius","Vanderbilt"],["Emmy","Noether"]]
for x in L:
   if x[0] != x0:
    print(xx(x[0],x[1]))

Problem 3: Text triangle

Write a program triangle.py that accepts one command line argument, an integer n, and prints n lines of text that form a text triangle of size n. The precise definition of a "text triangle" is given below, but it's probably easiest to understand from a few examples.

Here is a text triangle of size 1:

@

Here is a text triangle of size 2:

 @
@@@

Here is a text triangle of size 5:

    @
   @@@
  @@@@@
 @@@@@@@
@@@@@@@@@

Now, here's the full definition: A text triangle of size n consists of n lines of text, each of which contains only the characters "@" and " " (space). The first line contains 1 "@", the next line contains 3, the next 5, and so on, using consecutive odd integers. These blocks of "@" characters are indented with spaces so that the center "@" on each line is aligned with the center of each other line.

Warning: Your program needs to work for any value of n given by the user, so you can't just write the answers for a few values of n as string literals in your source code and choose which one to display!

Problem 4: Sevens in powers of two

To fix terminology, we'll say that 1,2,4,8,16 are the "first five powers of two". Analogously, the "first n powers of two" starts with 1 (which is 2**0) and ends with 2**(n-1).

Write a program that expects one command line argument, n, and which then examines the first n powers of two and determines how many times the digit 7 appears in each one. It should then print a report (histogram) in the following format:

Among the first 25 powers of 2:
20 of them have 0 instance(s) of the digit 7
4 of them have 1 instance(s) of the digit 7
1 of them have 3 instance(s) of the digit 7

Make sure you don't use floats anywhere in your program. A good test for this is to run it with n=1500, which will make some of the integers involved in the task too large to be converted to floats, so you'll see an error if you used floats.

Save your program as sevens.py and submit it.

Special request

Add a comment at the bottom of sevens.py indicating how long it took you to complete all of the quiz problems, e.g.

# Completing quiz 1 took me about 1 hour and 45 minutes to complete, including the time it took to read the surprisingly detailed instructions.