Homework 1 Solutions

Solution Files

You can find the solutions in hw01.py.

Required Questions

Welcome Forms

Q1: Welcome Forms

Please fill out both the Syllabus Quiz, which is based off of our policies found on the course syllabus, as well as the optional Welcome Survey.

Parsons Problems

To work on these problems, open the Parsons editor:

python3 parsons

Q2: k in Num

Write a function k_in_num which takes in two integers, k and num. k_in_num returns True if num has the digit k and returns False if num does not have the digit k. 0 is considered to have no digits.

def k_in_num(k, num):
    """
    Complete k_in_num, a function which returns True if num has the digit k and
    returns False if num does not have the digit k. 0 is considered to have no
    digits.

    >>> k_in_num(3, 123) # .Case 1
    True
    >>> k_in_num(2, 123) # .Case 2
    True
    >>> k_in_num(5, 123) # .Case 3
    False
    >>> k_in_num(0, 0) # .Case 4
    False
    """
while num: if k == num % 10: return True num = num // 10 return False

Code Writing Problems

Q3: A Plus Abs B

Python's operator module defines binary functions for Python's intrinsic arithmetic operators. For example, calling operator.add(2,3) is equivalent to calling the expression 2 + 3; both will return 5.

Fill in the blanks in the following function for adding a to the absolute value of b, without calling abs. 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 = sub
else:
f = add
return f(a, b)

Use Ok to test your code:

python3 ok -q a_plus_abs_b

If b is positive, we add the numbers together. If b is negative, we subtract the numbers. Therefore, we choose the operator add or sub based on the sign of b.

Q4: 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 min(i*i+j*j, i*i+k*k, j*j+k*k) # Alternate solution def two_of_three_alternate(i, j, k): return i**2 + j**2 + k**2 - max(i, j, k)**2

Hint: Consider using the max or min function:

>>> max(1, 2, 3)
3
>>> min(-1, -2, -3)
-3

Use Ok to test your code:

python3 ok -q two_of_three

We use the fact that if x>y and y>0, then square(x)>square(y). So, we can take the min of the sum of squares of all pairs. The min function can take an arbitrary number of arguments.

Alternatively, we can do the sum of squares of all the numbers. Then we pick the largest value, and subtract the square of that.

Q5: Largest Factor

Write a function that takes an integer n that is greater than 1 and returns the largest integer that is smaller than n and evenly divides n.

def largest_factor(n):
    """Return the largest factor of n that is smaller than n.

    >>> largest_factor(15) # factors are 1, 3, 5
    5
    >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
    40
    >>> largest_factor(13) # factor is 1 since 13 is prime
    1
    """
factor = n - 1 while factor > 0: if n % factor == 0: return factor factor -= 1

Hint: To check if b evenly divides a, you can use the expression a % b == 0, which can be read as, "the remainder of dividing a by b is 0."

Use Ok to test your code:

python3 ok -q largest_factor

Iterating from n-1 to 1, we return the first integer that evenly divides n. This is guaranteed to be the largest factor of n.

Submit

Make sure to submit this assignment by running:

python3 ok --submit