Introduction to Computer Programming: Lab 1

Fruitful Functions and For Loops


  1. Complete the function change(nickels, dimes, quarters) so that it returns the total dollar (not cent) equivalent of specified number of nickels, dimes and quarters. Examples:

    >>> change(0, 0, 0)
    >>> change(1, 1, 1)
    >>> change(20, 10, 4)
  2. Complete sum_odds(m) so that it returns the sum of the first m odd numbers, i.e., 1 + 3 + ... + (2m-1). Use a repeating for loop. Examples:

    >>> sum_odds(0)   # nothing to sum, so result is 0
    >>> sum_odds(3)   # 1 + 3 + 5
    >>> sum_odds(50)  # 1 + 3 + ... + 99
  3. Complete emphasize(s) so that it returns a new string that is s surrounded by * on each side. Examples:

    >>> emphasize('')
    >>> emphasize('kidding')
  4. Complete bold(s, n) so that it returns a new string that is s surrounded by n ''s on each side. Use a repeating for loop and call* emphasize. Examples:

    >>> bold('boring', 0)
    >>> bold('NOT', 5)

  5. Function composition; built-in functions. In IDLE's interaction window, write one line expressions. You may use string concatenation, the standard arithmetic operations, and these standard Python functions:int, len, chr, ord, abs, min, max. Example: if the instruction said "Generate the concatenation of the strings 'hello' and 'bye'" you should type:

    >>> 'hello' + 'bye'
    1. Generate the ASCII character that corresponds to the number that is the length of the phrase "Machines take me by surprise with great frequency".

    2. Generate the ASCII character that corresponds to 32 more than the ASCII value of the letter Q.

    3. Compute the maximum of 59 and the absolute value of the minimum of 43 and -71.

    4. Generate the ASCII character that corresponds to 5 times the value of the integer interpretation of the string formed by concatenating the ASCII character corresponding to the value of 7 squared with the ASCII character corresponding to three more than the ASCII value corresponding to the string '0'.

  6. Complete diff_squares(a, b) so that given two numbers a and b it returns the difference of the square of the larger number minus the square of the smaller number. You may use max, min, abs as you see fit; you should also call square. Examples:

    >>> diff_squares(5, 3)
    >>> diff_squares(3, 4)
    >>> diff_squares(5, 13)
  7. Complete az(n) so that it returns a string consisting of 2n characters alternating by going up in the alphabet from A and down in the alphabet from Z. You may assumes n < 26. Examples:

    >>> az(0)
    >>> az(1)
    >>> az(4)
  8. Complete mid(a, b, c) so that it returns the middle number (that is the number that is neither the maximum nor the minimum of the a, b, c). Hint: use max and min. Examples:

    >>> mid(1, 2, 3)
    >>> mid(9, -7, 5)
    >>> mid(400, 401, 0)

  9. Complete sum_powers(n, x) so that it returns the sum of the first n powers of x (counting from 0). That is it should compute: 1 + x + x^2 + x^(n-1). You are welcome to use the exponent (**) operator. Recall that x^0 is 1. You should use the more general for/range loop. Examples:

      >>> sum_powers(0, 99)  # there is nothing to add
      >>> sum_powers(4, 2)   # 1 + 2 + 4 + 8
      >>> sum_powers(3, 3)   # 1 + 3 + 9
      >>> sum_powers(5, 7)
  10. Complete the nonfruitful function looper so that it asks the user for a number n and then for the first n numbers (starting at 1), prints that number and thrice that number plus one. Use the more general for/range loop. Example:

    >>> looper()
    Enter a number: 4
    1 --> 4
    2 --> 7
    3 --> 10
    4 --> 13

  11. Use Python's turtles to write a function that draws a regular "n-gon" - a polygon with n sides of equal length and equal angles. It should take three parameters: a turtle, the number of sides, and the length of the sides. You can test it using the supplied test_ngon function. For example, test_ngon(8, 20) should generate an octagon like:

    Some relevant geometry: a regular n-gon has n sides of equal length; its combined exterior angles add up to 360 degrees. Its interior angles are complements of their corresponding exterior angles (i.e., 180 - the exterior angle). However, when the turtle turns to visually form that angle, the angle actually specified is relative to the direction the turtle had been heading in, so, to draw the interior angle, the turtle has to turn at an angle of the exterior value.

  12. Write a function that uses a general for loop and your draw_regular_ngon function to draw, from left to right a triangle, square, regular pentagon and regular hexagon, each of increasing size. Example output: