CS 61A: Quiz 1

Due by 11:59pm on Thursday, 9/11

Instructions: Download, complete, and submit the quiz1.py starter file before the deadline above. You must work alone, but you may talk to the course staff (see Asking Questions below). You may use any course materials, including an interpreter, course videos, slides, and readings. Please do not discuss these specific questions with your classmates, and do not scour the web for answers or post your answers online.

Submit with submit quiz1. You may submit more than once before the deadline; only the final submission will be scored. See Lab 1 for submission instructions.

Your submission will be graded automatically for correctness. Your implementations do not need to be efficient, as long as they are correct.

Asking Questions. If you believe you need clarification on a question, make a private post on Piazza. Please do not post publicly about the quiz contents. If the staff discovers a problem with the quiz or needs to clarify a question, we will email the class via Piazza. You can also come to office hours to ask questions about the quiz or any other course material, but no answers or hints will be provided in office hours.

Readings: You might find the following references useful:

Table of Contents

Question 1

Implement a function two_equal that takes three integer arguments and returns whether exactly two of the arguments are equal and the third is not.

def two_equal(a, b, c):
    """Return whether exactly two of the arguments are equal and the
    third is not.

    >>> two_equal(1, 2, 3)
    False
    >>> two_equal(1, 2, 1)
    True
    >>> two_equal(1, 1, 1)
    False
    >>> result = two_equal(5, -1, -1) # return, don't print
    >>> result
    True

    """
    "*** YOUR CODE HERE ***"

Question 2

Implement same_hailstone, which returns whether positive integer arguments a and b are part of the same hailstone sequence. A hailstone sequence is defined in Homework 1.

Solutions to Homework 1 (which you may reference) have been posted.

def same_hailstone(a, b):
    """Return whether a and b are both members of the same hailstone
    sequence.

    >>> same_hailstone(10, 16) # 10, 5, 16, 8, 4, 2, 1
    True
    >>> same_hailstone(16, 10) # order doesn't matter
    True
    >>> result = same_hailstone(3, 19) # return, don't print
    >>> result
    False

    """
    "*** YOUR CODE HERE ***"

Question 3

A near-golden rectangle has width w and height h where h/w is close to w/h - 1. For example, in an 8 by 13 rectangle, the difference between 8/13 and 5/8 is less than 0.01. Implement near_golden, which returns the height of a near-golden rectangle.

The near_golden function takes an even integer perimeter greater than 3 as its argument. It returns the integer height h of a rectangle with the given perimeter that has the smallest possible difference between h/w and w/h - 1.

NOTE: The words "greater than 3" replaced the word "positive" in the paragraph above at 6:50pm Wednesday 9/10.

Hints: The perimeter of a rectangle is w+w+h+h. Since h and perimeter are integers, w must be an integer as well. The built-in abs function returns the absolute value of a number.

def near_golden(perimeter):
    """Return the integer height of a near-golden rectangle with PERIMETER.

    >>> near_golden(42) # 8 x 13 rectangle has perimeter 42
    8
    >>> near_golden(68) # 13 x 21 rectangle has perimeter 68
    13
    >>> result = near_golden(100) # return, don't print
    >>> result
    19

    """
    "*** YOUR CODE HERE ***"