hw1.py

hw1.py (plain text)


"""CS 61A HW 01
Name:
Login:
TA:
Section:
"""

from operator import add, mul, sub

#### Q1
question1 = "PUT ANSWER HERE"

#### Q2
def two_of_three(a, b, c):
    """Return x**2 + y**2, where x and y are the two largest of a, b, c.
    
    >>> two_of_three(1, 2, 3)
    13
    >>> two_of_three(5, 3, 1)
    34
    >>> two_of_three(10, 2, 8)
    164
    >>> two_of_three(5, 5, 5)
    50
    """
    "*** YOUR CODE HERE ***"

#### Q3
def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and false_result otherwise."""
    if condition:
        return true_result
    else:
        return false_result

def with_if_statement():
    if c():
        return t()
    else:
        return f()

def with_if_function():
    return if_function(c(), t(), f())

def c():
    "*** YOUR CODE HERE ***"

def t():
    "*** YOUR CODE HERE ***"

def f():
    "*** YOUR CODE HERE ***"

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

    >>> a_plus_abs_b(3, 4)
    7
    >>> a_plus_abs_b(3, -5)
    8
    >>> a_plus_abs_b(3, 0)
    3
    """
    if ___:
        op = ___
    else:
        op = ___
    return op(a, b)

#### Q5
def piecewise(f, b, g):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
    g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> def identity(x):
    ...     return x
    >>> abs = piecewise(negate, 0, identity)
    >>> abs(6)
    6
    >>> abs(-1)
    1
    """
    "*** YOUR CODE HERE ***"

#### Q6
def product(n, term):
    """Return the product of the first n terms in the sequence formed
    by applying term to the integers 1, ..., n.

    term -- a function that takes one argument

    >>> def identity(x):
    ...     return x
    >>> def square(x):
    ...     return x * x
    >>> product(3, identity) # 1 * 2 * 3
    6
    >>> product(5, identity) # 1 * 2 * 3 * 4 * 5
    120
    >>> product(3, square) # 1^2 * 2^2 * 3^2
    36
    >>> product(5, square) # 1^2 * 2^2 * 3^2 * 4^2 * 5^2
    14400
    """
    "*** YOUR CODE HERE ***"

def factorial(n):
    """Returns factorial.
    
    >>> factorial(5)
    120
    >>> factorial(3)
    6
    >>> factorial(2)
    2
    """
    "*** YOUR CODE HERE ***"

#### Q7

def accumulate(combiner, start, n, term):
    """Return the result of combining the first n terms in a sequence.
    
    >>> def identity(x):
    ...     return x
    >>> def square(x):
    ...     return x * x
    >>> accumulate(add, 0, 5, identity) # 0 + 1 + 2 + 3 + 4 + 5
    15
    >>> accumulate(add, 11, 5, identity) # 11 + 1 + 2 + 3 + 4 + 5
    26
    >>> accumulate(add, 11, 0, identity) # 11
    11
    >>> accumulate(add, 11, 3, square) # 11 + 1^2 + 2^2 + 3^2
    25
    """
    "*** YOUR CODE HERE ***"

def summation_using_accumulate(n, term):
    """Return the sum of the first n terms in the sequence formed by applying
    term to the integers 1, ..., n.

    term -- a function that takes one argument

    >>> def identity(x):
    ...     return x
    >>> def square(x):
    ...     return x * x
    >>> summation_using_accumulate(3, identity) # 1 + 2 + 3
    6
    >>> summation_using_accumulate(5, identity) # 1 + 2 + 3 + 4 + 5
    15
    >>> summation_using_accumulate(3, square)   # 1^2 + 2^2 + 3^2
    14
    >>> summation_using_accumulate(5, square)   # 1^2 + 2^2 + 3^2 + 4^2 + 5^2
    55
    """
    "*** YOUR CODE HERE ***"

def product_using_accumulate(n, term):
    """Return the product of the first n terms in the sequence formed
    by applying term to the integers 1, ..., n.

    term -- a function that takes one argument

    >>> def identity(x):
    ...     return x
    >>> def square(x):
    ...     return x * x
    >>> product_using_accumulate(3, identity) # 1 * 2 * 3
    6
    >>> product_using_accumulate(5, identity) # 1 * 2 * 3 * 4 * 5
    120
    >>> product_using_accumulate(3, square) # 1^2 * 2^2 * 3^2
    36
    >>> product_using_accumulate(5, square) # 1^2 * 2^2 * 3^2 * 4^2 * 5^2
    14400
    """
    "*** YOUR CODE HERE ***"