Homework 1: Functions

  • Due: Tuesday 09/08 @ 11:59pm
  • Points: 1
  • Download: hw01.zip

To receive credit, you must solve each problem and then complete a short checkoff interview about your solution. You may use Preceptor for the interview or come to office hours to be interviewed by a member of the course staff. Staff will be doing in-person checkoffs for up to 3 business days after the assignment deadline (including any approved extension). If a checkoff happens more than 3 business days after the regular deadline, staff may ask to confirm your extended deadline via Flextensions. You will submit a Provenance bundle that includes a record of how you used VS Code, including interactions with Preceptor. Please do not use AI tools other than Preceptor for this assignment.

Readings: This homework relies on the following readings from Composing Programs:

Required Questions

Q1: A Plus Abs B

Python's operator module contains two-argument functions such as add and sub for Python's built-in arithmetic operators. For example, add(2, 3) evalutes to 5, just like the expression 2 + 3.

Fill in the blanks in the following function to add a to the absolute value of b, without calling the abs function. You may not modify any of the provided code other than the two blanks.

def a_plus_abs_b(a, b):
    """Return a+abs(b), but without calling abs.

    >>> a_plus_abs_b(2, 3)
    5
    >>> a_plus_abs_b(2, -3)
    5
    >>> a_plus_abs_b(-1, 4)
    3
    >>> a_plus_abs_b(-1, -4)
    3
    """
    if b < 0:
        f = _____
    else:
        f = _____
    return f(a, b)
python3 -m pytest -k a_plus_abs_b

Q2: Two of Three

Write a function that takes three positive numbers as arguments and returns the sum of the squares of the two smallest numbers. Use only a single line for the body of the function.

def two_of_three(i, j, k):
    """Return m*m + n*n, where m and n are the two smallest members of the
    positive numbers i, j, and k.

    >>> two_of_three(1, 2, 3)
    5
    >>> two_of_three(5, 3, 1)
    10
    >>> two_of_three(10, 2, 8)
    68
    >>> two_of_three(5, 5, 5)
    50
    """
    return _____

Hint: Consider using the max or min function:

>>> max(1, 2, 3)
3
>>> min(-1, -2, -3)
-3
python3 -m pytest -k two_of_three

Q3: Move to the End

Implement move_to_end, which takes a positive integer n and a non-negative integer k. It returns an integer with all of the digits of n, but with the digit that is k positions to the left of the rightmost digit (the one's digit) moved to the end, so that it becomes the new one's digit. If there is no digit of n that is k positions to the left of the one's digit, then a zero is added to the end instead.

The body of move_to_end must be a single return statement.

Reimplement pick_digit from Lab 1 and cut from Discussion 1, then call them in your implementation of move_to_end.

def move_to_end(n, k):
    """Return n with the digit at position k moved to the end.

    >>> move_to_end(97531, 2)
    97315
    >>> move_to_end(97531, 0)
    97531
    >>> move_to_end(97531, 4)
    75319
    >>> move_to_end(97531, 10)
    975310
    """
    return ____

def pick_digit(n, k):
    """Return the k-th digit from the right of n.

    >>> pick_digit(3579, 2)
    5
    >>> pick_digit(3579, 0)
    9
    >>> pick_digit(3579, 10)
    0
    """
    return ____

def cut(n, k):
    """Return n with the kth digit from the right removed.

    >>> cut(3579, 2)
    379
    >>> cut(3579, 0)
    357
    >>> cut(3579, 1)
    359
    >>> cut(3579, 5)
    3579
    """
    return ____

Check each function separately:

python3 -m pytest -k pick_digit
python3 -m pytest -k cut
python3 -m pytest -k move_to_end

Q4: Print Smaller

Implement print_smaller, which takes two different numbers x and y. It prints the smaller of the two numbers and returns the larger one.

Before writing any code, study the doctest below to understand why these four lines are printed in this order.

def print_smaller(x, y):
    """Print the smaller of x and y and return the larger.

    >>> print_smaller(print_smaller(5, 3), print_smaller(4, 6))
    3
    4
    5
    6
    """
    "*** YOUR CODE HERE ***"
python3 -m pytest -k print_smaller

Submit

Run Provenance: Prepare Submission Bundle from the VS Code command palette to create your submission zip, and upload that zip to Gradescope. For a refresher on how to do this, refer to Lab 00.


Back to Top

Accessibility Nondiscrimination

Copyright ©2026, Regents of the University of California and respective authors.

This site is built following the Berkeley Class Site template, which is generously based on the Just the Class, and Just the Docs templates.

View all course offerings