# Homework 5 Solutions

**Solutions:** You can find the file with solutions for all
questions here.

### Question 1: Lambdas and Currying

We can transform multiple-argument functions into a chain of single-argument, higher order functions by taking advantage of lambda expressions. This is useful when dealing with functions that take only single-argument functions. We will see some examples of these later on.

Write a function `lambda_curry2`

that will curry any two argument
function using lambdas. See the doctest if you're not sure what this
means.

```
def lambda_curry2(func):
"""
Returns a Curried version of a two argument function func.
>>> from operator import add
>>> x = lambda_curry2(add)
>>> y = x(3)
>>> y(5)
8
"""
return lambda arg1: lambda arg2: func(arg1, arg2)
```

Use OK to test your code:

`python3 ok -q lambda_curry2`

### Question 2: Palindrome

A number is considered a palindrome if it reads the same forwards and backwards. Fill in the blanks '_' to help determine if a number is a palindrome. In the spirit of exam style questions, please do not edit any parts of the function other than the blanks.

```
def is_palindrome(n):
"""
Fill in the blanks '_____' to check if a number
is a palindrome.
>>> is_palindrome(12321)
True
>>> is_palindrome(42)
False
>>> is_palindrome(2015)
False
>>> is_palindrome(55)
True
"""
x, y = n, 0
f = lambda: y * 10 + x % 10
while x > 0:
x, y = x // 10, f()
return y == n
```

Use OK to test your code:

`python3 ok -q is_palindrome`

### Question 3: String Transformer

Using a `lambda`

expression, complete the following function. Your function
should only contain a return statement.

```
from operator import add, sub
def caesar_generator(num, op):
"""Returns a one-argument Caesar cipher function. The function should "rotate" a
letter by an integer amount 'num' using an operation 'op' (either add or
sub).
You may use the provided `letter_to_num` and `num_to_letter` functions,
which will map all lowercase letters a-z to 0-25 and all uppercase letters
A-Z to 26-51.
>>> letter_to_num('a')
0
>>> letter_to_num('c')
2
>>> num_to_letter(3)
'd'
>>> caesar2 = caesar_generator(2, add)
>>> caesar2('a')
'c'
>>> brutus3 = caesar_generator(3, sub)
>>> brutus3('d')
'a'
"""
return lambda char: num_to_letter(op(letter_to_num(char), num))
```

Use OK to test your code:

`python3 ok -q caesar_generator`

### Question 4: Polynomial

A polynomial function is a function with coefficients, variables and constants. A polynomial function is said to be the nth degree polynomial if there is a term in the function with the variable to the nth degree. For example, a 4th degree polynomial must contain the term x^4 with some coefficient multiplied to it.

Complete the function `polynomial`

, which takes in a degree and a list of coefficients. The function should output the corresponding polynomial function.

Hint: the staff solutions is one line and uses lambda + a list comprehension.

```
def polynomial(degree, coeffs):
"""
>>> fourth = polynomial(4, [3,6,2,1, 100])
>>> fourth(3) # 3*(3**4) + 6*(3**3) + 2*(3**2) + 1*(3**1) + 100
526
>>> third = polynomial(3, [2, 0, 0, 0])
>>> third(4) # 2*(4**3) + 0*(4**2) + 0*(4**1) + 0
128
"""
# Option 1
return lambda x: sum([coeffs[i]*(x ** (degree - i)) for i in range(degree + 1)])
# Option 2
def poly_func(x):
return sum([coeffs[i]*(x ** (degree - i)) for i in range(degree + 1)])
return poly_func
```

Use OK to test your code:

`python3 ok -q polynomial`