# CS61A Homework 1

# Homework 1: Variables & Functions, Control hw01.zip

## Parsons Problems

### 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.

1 | def k_in_num(k, num): |

## 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.

1 | def a_plus_abs_b(a, 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.**

1 | def two_of_three(i, j, k): |

Hint:Consider using the`max`

or`min`

function:

1

2

3

4 >>> max(1, 2, 3)

3

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

-3

### 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`

.

1 | def largest_factor(n): |

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.”