CSC111 Exercises on Functions Returning Values

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 17:07, 3 November 2011 (EDT)



List exercises

 name = "The storm of Halloween 2011."
 L1  = [ 3, 10, 1, 4, 6, 100, 200, 0, 0, 3, 10 ]
Exercise 1
Find all the methods you know that will remove the period at the end of name
Exercise 2
Create a new list L2 that contains the 3 smallest elements of L1
Exercise 3
Create a new list L3 that contains the 10 largest elements of L1
Exercise 4
Same exercises as 2 and 3, but this time do so in a way that does not modfify L1.


Function Exercises

Exercise 1
write a function that receives 3 numbers and returns the median, i.e. the number that is not the min and not the max, but the one in between.
Exercise 2
write a function that receives two points, as defined in the graphics system, and returns the distance between the two points
Exercise 3
Write a function that receives a point and a rectangle as parameters, and returns 1 if the point is inside the rectangle, and 0 otherwise.
Exercise 4
Assume that we have several lines formatted as follows:
  name: Yvonne, age: 23, box: 3345, email: yvonne123@smith.edu
Write a function that receives a string similar formatted that way, and that returns a list containing just the name, the age, the box, and the email, i.e. [ "Yvonne", "23", "3345", "yvonne123@smith.edu" ]
Exercise 5
Transform strings representing dates of the form 11032011 into Nov 03, 2011. Your solution should work for other strings, such as 01012000, 08021980, etc.
Exercise 6
This one should make sense just by looking at what it returns.



  user = makeUserName( "Alex Andra" )
  print( user )
  # will print aandra
 
  print( makeUserName( "Mini Driver" ) )
  # will print mdriver
 
  print( makeUserName( "Alan J. Smith" ) )
  # will print asmith



Exercise 7
What is printed by the code below?

def f1( a ):
    return 2 * a - 1

def f2( b ):
    return f1( f1( b ) - 3 )

print(  f2( 5 ) )


Solutions

def median3( x, y, z ):
    L = [x, y, z]
    L.sort()
    return L[2]

def distance( P1, P2 ):
    x1 = P1.getX()
    y1 = P1.getY()
    x2 = P2.getX()
    y2 = P2.getY()
    return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) )

def isInside( p, r ):
    """p is a point, r a rectangle, returns 1 if
    p is inside r, and 0 otherwise"""
    x  = p.getX()
    y  = p.getY()
    x1 = r.getP1().getX()
    y1 = r.getP1().getY()
    x2 = r.getP2().getX()
    y2 = r.getP2().getY()
    if ( ( x1 <= x <= x2 or x2 <= x <= x1 )
       and ( y1 <= y <= y2 or y2 <= y <= y1 ) ):
        return 1
    return 0

def mySplit( line ):
    fields = line.split( ',' )
    # fields is [ "nnnn: vvv", "nnnn: vvvv", ...]
    
    L = []
    for field in fields:
        # field is "xxxxx : xxxx"
        L.append( field.split( ':' )[1].strip() )

    return L

def transformDate( date ):
    months = ["Jan", "Feb", "Mar", "Apr" ]
    day = date[0:2]
    month = date[2:4]
    year  = date[4: ]
    month = months[ month-1 ]
    return month+ ' ' + day + ' ' + year

def makeUserName( fullName ):
    words = fullName.lower().split()
    firstName = words[0]
    firstInitial = firstName[0]
    lastName  = words[-1]
    return firstInitial + lastName

def f1( a ):
    return 2 * a - 1

def f2( b ):
    return f1( f1( b ) - 3 )

print( f2( 5 ) )
# prints 11