CMPT 120 Lab 5

This site applies only to CMPT 120 D1 (Burnaby) in Summer 2011. See the other instructors' pages for other sections.

This won't be completed in the regular lab time because of the Academic Enhancement Program, but it's good practice anyway.

  1. Write a function named mostly_spaces that takes one argument: a string. The function should return True if the string is more than half spaces, and False. (Note: The boolean values True and False are not the same as the strings "True" and "False".)

    It should be possible to use your function in the main part of the program like this:

    print mostly_spaces("a  c  d")
    print mostly_spaces("ac d")
    print mostly_spaces("x x x ")
    if mostly_spaces("1 2 3 4"):
         print "yes"
    else:
         print "no"

    The result if you run this program with your function should be:

    True
    False
    False
    no
  2. In this question, you will recreate older lab exercises as functions. You are welcome to use the code from the older lab solutions. You don't have to worry about error checking (numbers out of bounds, etc.) in either.
    1. Write a function area that calculates the area of a circle, as in lab 2. It should take a number as it argument (the radius) and return the area. So, the function call area(10) should return 314.16.
    2. Write a function feedback that gives feedback on a mark, as in lab 3. It should take a number as it argument (the grade) and return a string with English feedback on the grade. So, the function call feedback(55) should return "needs improvement" and feedback(85) should return "very good".
  3. Create a text file or answer this question on paper. Give the running time of these algorithms (which have been expressed in Python). Assume the value of n has already been set; express the running time in terms of n. For each of them, the number of “steps” is the number of times the calculation in the loop is repeated.
    1. x = 0
      for i in range(2*n):
          x = x + i
      print x
    2. x = 0
      for i in range(n):
          for j in range(n-1):
              x = x + i*j
      print x
    3. x = 1
      while x<n:
          x = x * 2
      print x

    Remember to eliminate lower-order terms and leading constants.