Introduction to Computer Programming

Program 1: Fruitful Functions

Due date: Friday, September 15 at 5pm


Instructions


  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)
    0
    >>> tri_num(3)
    6
    >>> tri_num(6)
    21
  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)
    0
    >>> complicated(1)
    0
    >>> complicated(2)
    8
    >>> complicated(3)
    -1
    >>> complicated(8)
    52
    >>> 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)
    0
    >>> easy(2)
    52
    >>> easy(3)
    -17
  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)
    'A'
    >>> add_character('w', 3)
    'z'
    >>> add_character('!', 17)
    '2'
  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)
    1
    >>> pow3(2)
    9
    >>> pow3(5)
    243
    >>> pow3(37)
    450283905890997363
  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)
    'ABCDE'
    >>> ascii_forward('3', 5)
    '34567'
    >>> ascii_forward(']', 10)
    ']^_`abcdef'
  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)
    'A'
    >>> make_palindrome('M', 9)
    'VDUETFSGRHQIPJOKNLMLNKOJPIQHRGSFTEUDV'
    >>> make_palindrome('5', 3)
    '8273645463728'
    >>> make_palindrome('m', 15)
    '|^{_z`yaxbwcvduetfsgrhqipjoknlmlnkojpiqhrgsfteudvcwbxay`z_{^|'

  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)
    window.mainloop()

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