CSC111 Lab 9 2011
--D. Thiebaut 13:15, 2 November 2011 (EDT)
Contents
Working with Functions
- For this part, work in Idle or emacs and create the various functions this lab presents you, and test them in main().
- For example, if you are asked to write a function that receives a string as parameter and prints it with a box around it, you could write something like this:
def boxIt( string ):
noChars = len( s )
print( "-" * ( 2 + noChars + 2 ) )
print( "| " + string + " |" )
print( "-" * ( 2 + noChars + 2 ) )
def main():
boxIt( "hello!" )
boxIt( "This is a very long string!" )
boxIt( "" ) # empty string
main()
- No need to add documentation for the code in this lab (or just in spots where you think you need to add markers for yourself)
Box
- Go ahead and create the boxIt() function above.
- Test it. Notice that I tested it with 3 different strings, one of them the empty string. It is important to test for "strange" conditions. An empty string is a totally valid string, and if our program is supposed to work with strings, it should work with empty strings as well without crashing.
Triple Boxes
- Add a new function called BoxIt3( s1, s2, s3), that receives 3 strings and prints each string in its own box.
- If you call your function in main() like this BoxIt3( "Hello", "There", "Smith College" ), it will print
--------- | Hello | --------- --------- | There | --------- ----------------- | Smith College | -----------------
- Go ahead and test your new function. Test it with different strings.
- If your solution does not use the function BoxIt() created earlier, modify BoxIt3() so that it calls BoxIt() 3 times.
Multiple Boxes
- Add a new function called multipleBoxes( L ) that receives a list of strings and prints each string in the list with BoxIt().
- Here is are different possible ways one could test your function:
multipleBoxes( [ "Doc", "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey" ] ) multipleBoxes( [ "Hello", "", "", "There!" ] ) multipleBoxes( "Hello Smith College!" . split() )
Functions Returning Values
Median of 3
- write a function that returns the median of 3 numbers.
- For example:
def median3( a, b, c ):
# could it be b?
if a <= b and b <= c:
return b
# if we're here, it's not b. Could it be a?
if b <= a and a <= c:
return a
# if we're here it's not b and not a. It has to be c!
return c
def main():
print( median3( 10, 10, 20 ) )
print( median3( 1, 2, 3 )
print( median3( 6, 5, 7 )
print( median3( 8, 10, 9 )
main()
- Modify the code above and make it simpler by remembering that you can put a, b, and c in a list, sort the list, and return the element at Index 1.
Second Smallest
- Write a function that receives a list of numbers (or strings), and that returns the second smallest of them.
- For example:
L = [ 1, 10, 2, 3, 30, 40, 50 ]
ss = secondSmallest( L )
print( ss ) # prints out 2
Testing Second Smallest
- Test the function you have just written, and print the contents of the List of numbers (or strings) before you call secondSmallest() and after you call it.
- You will likely see that your list has changed. (if not, then skip to the next problem)
- If it has changed it is because you are sorting the list inside your function, and the list is passed by reference to the function.
- Modify your function so that the list does not get modified by the function when it is passed.