Introduction to Computer Programming

Program 1: Fruitful Functions

Due date: Friday, September 15 at 5pm


  1. Complete the function tri_num(m) that returns half of the product of m and one more than m. For example, if the input is 4, it should returns one half of 4 times 5. Examples:

    >>> tri_num(0)
    >>> tri_num(3)
    >>> tri_num(6)
  2. Consider this function:

    def complicated(x):
        a = x % 3
        b = x // 3
        c = x - 1
        d = (4 * a * c) - (b * b)
        return d

    Verify that complicated produces results such as:

    >>> complicated(0)
    >>> complicated(1)
    >>> complicated(2)
    >>> complicated(3)
    >>> complicated(8)
    >>> complicated(-1)
    => -17

    Complete the function easy(y) so that it applies the function complicated twice over to its input: in other words it calls the function complicated on y and then calls it again on what that returns. Examples:

    >>> easy(1)
    >>> easy(2)
    >>> easy(3)
  3. Complete the function add_character(c, x) that returns the character that corresponds to adding x to the ASCII value of c. The body of your function should be just one line - a return statement. The expression returned must involve composition and the built-in functions chr and ord. Examples:

    >>> add_character('A', 0)
    >>> add_character('w', 3)
    >>> add_character('!', 17)
  4. Complete the function pow3(n) so that it returns 3 to the power of n without using the built-in exponentiation operator (**). You can (and should use a for loop). Remember that 3 raised to the 0 power is 1. Examples:

    >>> pow3(0)
    >>> pow3(2)
    >>> pow3(5)
    >>> pow3(37)
  5. Complete the function ascii_forward(start_character, n) that returns a string of n consecutive ASCII characters starting with start_character. Examples:

    >>> ascii_forward('B', 0)
    >>> ascii_forward('A', 5)
    >>> ascii_forward('3', 5)
    >>> ascii_forward(']', 10)
  6. A palindrome is a string that is its own mirror image: it reads the same reading left to right as it would be from right to left. (For example, 'radar' and 'amanaplanacanalpanama' are palindromes; 'radon' is not.) Complete the function make_palindrome(start_character, n) that returns a string of length 4n+1 that is a palindrome centered around start character. The sequence of characters that are mirrored to the left and right of the start character should consists of pairs of symbols that ascend and descend (in terms of ASCII) from start_character in lockstep. Examples:

    >>> make_palindrome('A', 0)
    >>> make_palindrome('M', 9)
    >>> make_palindrome('5', 3)
    >>> make_palindrome('m', 15)

  7. Write a (nonfruitful) function draw_square(yertle, length) that draws a square with sides of length length using yertle the turtle. Assume that yertle has already been created. Example use:

    window = turtle.Screen()
    t = turtle.Turtle()
    draw_square(t, 50)

    (You should not assume that yertle's pen is already down. Nor should you assume that yertle is facing any particular direction. Consider using pendown and setheading. )

  8. Write a (nonfruitful) function nested_squares(n, start_length, gap) that draws n nested squares from the inside out using turtles and the draw_square function from the preceding problem. The innermost square has sides of length start_length. Each subsequent square should be centered around the preceding smaller square with gap units of space between the squares on all sides. As an example:

    >>> nested_squares(5, 20, 10)

    should draw:

  9. Write a fruitful function from scratch that takes two arguments. The arguments can be integers, strings or one of each. The type of the result (string or integer) is up to you. Your function should compute something that we have not discussed in class or lab. Include documentation (a docstring!) with examples following the models used elsehwere on the assignment.