Lab 1 Solutions

Solution Files

Required Questions

Review

Here are the most common ways to run Python on a file.
  1. Using no command-line options will run the code in the file you provide and return you to the command line. If your file just contains function definitions, you'll see no output unless there is a syntax error.

    python3 lab00.py
  2. -i: The -i option runs the code in the file you provide, then opens an interactive session (with a >>> prompt). You can then evaluate expressions, for example calling functions you defined. To exit, type exit(). You can also use the keyboard shortcut Ctrl-D on Linux/Mac machines or Ctrl-Z Enter on Windows.

    If you edit the Python file while running it interactively, you will need to exit and restart the interpreter in order for those changes to take effect.

    Here's how we can run lab00.py interactively:

    python3 -i lab00.py
  3. -m doctest: Runs the doctests in a file, which are the examples in the docstrings of functions.

    Each test in the file consists of >>> followed by some Python code and the expected output.

    Here's how we can run the doctests in lab00.py:

     python3 -m doctest lab00.py

    When our code passes all of the doctests, no output is displayed. Otherwise, information about the tests that failed will be displayed.


In 61A, we use a program called OK for autograding labs, homeworks, and projects.

To use Ok to test a function, run the following command (replacing FUNCTION with the name of the function):

python3 ok -q FUNCTION

If your function contains a call to print that starts with "DEBUG:", then this line will be ignored by OK. (Otherwise, including extra print calls can cause tests to fail because of the additional output displayed.)

print("DEBUG:", x)

There are more features described on the Using OK page. You can quickly generate most ok commands at ok-help.


Here are examples of the division-related operators in Python 3:
True Division: /
(decimal division)
Floor Division: //
(integer division)
Modulo: %
(remainder)
>>> 1 / 5
0.2

>>> 25 / 4
6.25

>>> 4 / 2
2.0

>>> 5 / 0
ZeroDivisionError
>>> 1 // 5 # truncate result of true division
0

>>> 25 // 4
6

>>> 4 // 2
2

>>> 5 // 0
ZeroDivisionError
>>> 1 % 5
1

>>> 25 % 4
1

>>> 4 % 2
0

>>> 5 % 0
ZeroDivisionError

A ZeroDivisionError occurs when dividing by 0.

One useful technique involving the % operator is to check whether a number x is divisible by another number y:

x % y == 0

For example, in order to check if x is an even number: x % 2 == 0


Most functions that you define will contain a return statement that provides the value of the call expression used to call the function.

When Python executes a return statement, the function call terminates immediately. If Python reaches the end of the function body without executing a return statement, the function returns None.

In contrast, the print function is used to display values. Unlike a return statement, when Python evaluates a call to print, the function does not terminate immediately.

def what_prints():
    print('Hello World!')
    return 'Exiting this function.'
    print('61A is awesome!')

>>> what_prints()
Hello World!
'Exiting this function.'

Notice also that print will display text without the quotes, but return will preserve the quotes.

What Would Python Display? (WWPD)

Q1: Return and Print

Use Ok to test your knowledge with the following "What Would Python Display?" questions:

python3 ok -q return-and-print -u

>>> def welcome():
...     print('Go')
...     return 'hello'
...
>>> def cal():
...     print('Bears')
...     return 'world'
...
>>> welcome()
______
Go 'hello'
>>> print(welcome(), cal())
______
Go Bears hello world

Write Code

Q2: Debugging Quiz

The following is a quick quiz on different debugging techniques that will be helpful for you to use in this class. You can refer to the debugging article to answer the questions.

Use Ok to test your understanding:

python3 ok -q debugging-quiz -u

Q3: Pick a Digit

Implement digit, which takes positive integers n and k and has only a single return statement as its body. It returns the digit of n that is k positions to the left of the rightmost digit (the one's digit). If k is 0, return the rightmost digit. If there is no digit of n that is k positions to the left of the rightmost digit, return 0.

Hint: Use // and % and the built-in pow function to isolate a particular digit of n.

def digit(n, k):
    """Return the digit that is k from the right of n for positive integers n and k.

    >>> digit(3579, 2)
    5
    >>> digit(3579, 0)
    9
    >>> digit(3579, 10)
    0
    """
return n // pow(10, k) % 10

Use Ok to test your code:

python3 ok -q digit

Q4: Middle Number

Implement middle by writing a single return expression that evaluates to the value that is neither the largest or smallest among three different integers a, b, and c.

Hint: Try combining all the numbers and then taking away the ones you don't want to return.

def middle(a, b, c):
    """Return the number among a, b, and c that is not the smallest or largest.
    Assume a, b, and c are all different numbers.

    >>> middle(3, 5, 4)
    4
    >>> middle(30, 5, 4)
    5
    >>> middle(3, 5, 40)
    5
    >>> middle(3, 5, 40)
    5
    >>> middle(30, 5, 40)
    30
    """
return a + b + c - min(a, b, c) - max(a, b, c)

Use Ok to test your code:

python3 ok -q middle

Syllabus Quiz

Q5: Syllabus Quiz

Please fill out the Syllabus Quiz, which confirms your understanding of the policies on the syllabus page (linked in the toolbar above).

Check Your Score Locally

You can locally check your score on each question of this assignment by running

python3 ok --score

This does NOT submit the assignment! When you are satisfied with your score, submit the assignment to Gradescope to receive credit for it.

Submit Assignment

Submit this assignment by uploading any files you've edited to the appropriate Gradescope assignment. Lab 00 has detailed instructions.