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

Quiz 2

MCS 275 Spring 2021 - Emily Dumas

Instructions:

Deadline

This quiz must be submitted in Gradescope by 12:00pm CST on Tuesday, January 26, 2021.

Policy reminder

Quizzes are INDIVIDUAL and closed book (no online resources are permitted) unless exceptions are noted in the instructions.

Resources you are allowed to consult

For this quiz:

Cite your sources

Two problems on this quiz encourage you to use code from the solutions to Worksheet 2. You can use your own solutions for this, in which case you'll be the sole author of the final program. Alternatively, you can use code from the solutions written by the course staff, as long as the declaration you put at the top of your program accurately explains the authorship. Here is an example:

# Declaration: I, Anna K. Example, used code from the Worksheet 2 solutions as a
# starting point for this program, and developed all of the additional code
# contained in this program myself.  I followed the rules in the course syllabus.

Hopefully shorter than Quiz 1

Based on feedback from Quiz 1, I have tried to make this quiz shorter.

(No problem number 1, as usual)

The 3 points assigned by the autograder based on syntax and docstring checking will be listed as problem 1 in Gradescope.

Problem 2: Perfect square floor - 4 points

Write a function square_floor(n) that takes a positive integer n and returns the largest perfect square that is less than or equal to n.

For example:

  • square_floor(9) should return 9, because 3**2 = 9 <= 9 but 4**2 = 16 > 9.
  • square_floor(20) should return 16 because 4**2 = 16 <= 20 but 5**2 = 25 > 20

Put this function in a file quiz2prob2.py (with the usual required header and file-level docstring) and submit it. The file quiz2prob2.py doesn't need to do anything other than defining that single function.

Problem 3: Longest run - 4 points

Write a function longest_run_length(text) that takes a string text and returns an integer which is the length of the longest consecutive block of any single character in the string.

The function should consider any character, and use case-sensitive comparisons.

Therefore:

  • longest_run_length("heliotrope!!") should return 2 because "!" appears twice in a row, and no character appers 3 or more times in a row
  • longest_run_length("Distinguished raccoons") should return 3 because there are three spaces in a row in the string, and no character appears 4 times in a row
  • longest_run_length("") should return 0
  • longest_run_length("Eels have a complicated life cycle with many stages") should return 1 because the string is not empty, but no character appears twice (or more) in a row

Put this function in a file quiz2prob3.py (with the usual required header and file-level docstring) and submit it. The file quiz2prob3.py doesn't need to do anything other than defining that single function.

Note: It is expected that you can make this function pretty easily by modifying the solution to one of the problems on Worksheet 2.

Problem 4: RATS classifier - 4 points

Recall the RATS process described in Worksheet 2. This associated to any integer n a sequence of numbers, such as n=12 leading to the sequence 12, 33, 66, 123, 444, 888, 1677, 3489, 12333, 44556, ....

In Worksheet 2 you wrote a program to compute this sequence for one starting value, and to stop if a cycle was found. When we determine whether a certain starting value leads to a cycle, we'll say that we have classified the that starting value. So the Worksheet 2 program classifies a single starting value provided by the user.

Modify the program developed in Worksheet 2 so that instead of classifying one starting value, it tries each value of n between 1 and 100 (inclusive) and prints what it finds for each one. Each line of output should show the starting value and the observed behavior. The modified program shouldn't require any command line arguments or keyboard input.

When it is run, the first six lines of output should be:

1: No periodic sequence found in 150 iterations
2: No periodic sequence found in 150 iterations
3: Ends at periodic sequence [444, 888, 1677, 3489, 12333, 44556, 111, 222]
4: No periodic sequence found in 150 iterations
5: No periodic sequence found in 150 iterations
6: Ends at periodic sequence [444, 888, 1677, 3489, 12333, 44556, 111, 222]

and the last two lines of output should be:

99: Ends at periodic sequence [117, 288]
100: No periodic sequence found in 150 iterations

Note the slightly different way of printing the output compared to Worksheet 2.

Call this program quiz2prob4.py (with the usual required header and file-level docstring) and submit it.

Special request

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

# Completing quiz 2 took 57 minutes and 31.48250123 seconds, according
# to the rubidium-87 atomic clock that I happen to have on my desk.