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.
-
Write a function named
mostly_spacesthat takes one argument: a string. The function should returnTrueif the string is more than half spaces, andFalse. (Note: The boolean valuesTrueandFalseare 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 -
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.
-
Write a function
areathat 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 callarea(10)should return 314.16. -
Write a function
feedbackthat 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 callfeedback(55)should return"needs improvement"andfeedback(85)should return"very good".
-
Write a function
-
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
nhas already been set; express the running time in terms ofn. For each of them, the number of “steps” is the number of times the calculation in the loop is repeated.-
x = 0
for i in range(2*n):
x = x + i
print x -
x = 0
for i in range(n):
for j in range(n-1):
x = x + i*j
print x -
x = 1
while x<n:
x = x * 2
print x
Remember to eliminate lower-order terms and leading constants.
-