Lecture 20 Finger Exercise

The questions below are due on Friday May 08, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Question 1. Implement the function that meets the specification below:

def count_sqrts(numbers):
    """
    Given a list of unique positive ints, return the number of elements
    whose squared values are also in the list.

    Assume (and check) the list is non-empty, and raise a ValueError if
    its elements are not all unique positive ints.
    """

For exammple:

  • count_sqrts([3, 4, 2, 1, 9, 25]) should evaluate to 3
  • count_sqrts([]) should raise an AssertionError

Question 2. Consider the following function, which takes in a list and a function object.
def sum_on_opposing_pairs(numbers, func):
    """
    Return the sum of what func evaluates to when called on each pair of
    elements in numbers, at indices i and j, where i + j = len(numbers) - 1.

    Parameters:
        numbers (list): A non-empty list of ints.
        func (function): A function that takes in two ints and returns a number.

    Raises:
        ValueError: If numbers is empty.
        TypeError: If not all elements in numbers are ints.
    """

For example:

  • sum_on_opposing_pairs([3, 1], lambda x, y: x - y) should evaluate to 0, because we add (3 - 1) and (1 - 3).
  • sum_on_opposing_pairs([1, 2, 3], lambda x, y: x * y) should evaluate to 10. This is the sum of (1 * 3), (2 * 2), and (3 * 1).
  • sum_on_opposing_pairs([], lambda x, y: x * y) should raise a ValueError.

Now, rather than provide an explicit list, we"d like to specify one via parameters to a wrapper function.

def soop_range(params, func):
    """
    Construct a list of numbers from given parameters, then return the
    result of calling sum_on_opposing_pairs() on that list and a given
    function.

    The params argument is a tuple of ints (lb, ub, step, div). The list
    is constructed by passing lb, ub, step into range(), and then
    dividing the elements of that range by div.

    If the resulting list turns out to be empty, return 0. If it turns
    out to contain float elements due to division, raise a ValueError.
    """

For example:

  • soop_range((10, 50, 10, 2), lambda x, y: x + y) should evaluate to 100.
  • soop_range((10, 50, 10, 5), lambda x, y: x + y) should evaluate to 40.
  • soop_range((50, 10, 10, 5), lambda x, y: x + y) should evaluate to 0.
  • soop_range((10, 50, 10, 20), lambda x, y: x + y) should raise a ValueError.

Implement a soop_range() function that satisfies the docstring above. You may assume that within params, the given lb, ub, and step components form valid inputs to range(), and that div is non-negative.

You should assume that sum_on_opposing_pairs() is correctly implemented and available to you. For convenience, you may also assume the following function is available.

def truediv_int(a, b):
    """a and b are ints"""
    if a % b == 0:
        return a // b  # returns an int
    else:
        return a / b   # returns a float

Note: This is an exercise in exception-handling. However, you may also use conditionals (i.e., if statements) to express equivalent logic.