The instructions are long because this is the first quiz. In the future, an abbreviated set of instructions will suffice!
.py
)..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.)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.
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.
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.
# 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]))
# MCS 275 Quiz 1 Problem 2
# Jennifer Vaccaro
# I edited this code in accordance with the rules defined in the syllabus.
def abbreviate(firstname,lastname):
'''Given input strings representing a first and last name,
returns a string with the first initial and last name.'''
return firstname[0]+". "+lastname
ignore_name = "David"
name_list = [["David","Dumas"],["Alice","Ball"],["Cornelius","Vanderbilt"],["Emmy","Noether"]]
for name in name_list:
# Print abbreviated versions of all names,
# unless we want to ignore it.
if name[0] != ignore_name:
print(abbreviate(name[0],name[1]))
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!
# MCS 275 Quiz 1 Problem 3
# Jennifer Vaccaro
# I wrote this code in accordance with the rules defined in the syllabus.
import sys
# n is the size of the triangle, defined as a command line argument
n = int(sys.argv[1])
for i in range(n):
# Print out a line with spaces then @ symbols creating the triangle
print((n-i-1)*" "+(1+2*i)*"@")
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.
# MCS 275 Quiz 1 Problem 4
# Jennifer Vaccaro
# I wrote this code in accordance with the rules defined in the syllabus.
import sys
# powers is the number of powers of two we will check for sevens
powers = int(sys.argv[1])
# sevens_dict stores how many powers have each number of sevens
sevens_dict = {}
for pow in range(powers):
# Calculate 2**pow and convert to a string
n_string = str(2**pow)
# Count how many 7s are in it
num_sevens = 0
for i in n_string:
if i=="7":
num_sevens+=1
# Add it to the sevens_dict
if num_sevens in sevens_dict.keys():
sevens_dict[num_sevens] += 1
else:
sevens_dict[num_sevens] = 1
# Print out the results
print("Among the first {} powers of 2:".format(powers))
for n in sevens_dict.keys():
print("{} of them have {} instance(s) of the digit 7".format(sevens_dict[n],n))
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.