Strict Standards: Declaration of Doku_Renderer_metadata::table_open() should be compatible with Doku_Renderer::table_open($maxcols = NULL, $numrows = NULL, $pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 480

Strict Standards: Declaration of Doku_Renderer_metadata::table_close() should be compatible with Doku_Renderer::table_close($pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 480

Lab 3: Examples using functions (first hour)

## What do these functions return?
def mystery(x):
    if x > 8:
        return 1;
    if x < 3:
        return 5;
    else:
        return 10;
 
val = 2
mystery(val)
mystery(mystery(val))
mystery(mystery(mystery(val)))
## What is the output?
from numpy import zeros
 
def identity(n):
    myarray = zeros((n,n))
    for i in range(n):
        myarray[i][i] = 1
    return myarray
 
myarray = zeros((4,4))
A = identity(4)
print A
print myarray
def doublelist(mylist):
    for i in range(len(mylist)):
        mylist[i] = mylist[i] * 2
    return mylist
 
listA = [1, 2, 3, 4, 5]
listB = ["a", "b", "c", "d", "e"]
 
listA2 = doublelist(listA)
listB2 = doublelist(listB)
 
## What do the following statements print?
print listA
print listA2
print listB
print listB2
## Which function swaps the contents of two variables? Use of function is shown in each case
def swap1(a, b):
   tmp =b; b = a; a = tmp
 
a, b = 2, 4
swap1(a,b)
print a, b 
 
def swap2(a,b):
   return b, a 
 
a, b = 2, 4
a, b = swap2(a,b)
print a, b 
 
## How can we do this without a function?
## What will this print?
 
def message(n):
   if n < 3:
      print "This"
   if n < 1 and n > 10:
      print "never"
   if n > 3:
      print "happens."

Clicker Questions:

def double(x):
    x = x * 2
    return x
 
## What is printed?
x = 10
double(x)
double(x)
double(x)
print x 
A None
B 10
C 20
D 40
E 80
def forgetful(a, b, total):
    total = a * b
 
## What is the output?
total = 2
x = forgetful(2, 3, total)
print x
A 4
B 6
C 9
D None
E There is a syntax error.
## What is the output?
multiplier = 2.0
 
def multiplyIt(n):
    n = multiplier * n
    return n
 
t = 3
result = multiplyIt(t)
multiplier = result
result = multiplyIt(0.5)
print result
A 2.0
B 3.0
C 6.0
D 9.0
E None of the above

Audio read/write utilities (Second Hour)

 
cs190c/lab3.txt · Last modified: 2008/07/24 12:11 by seh
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki