Difference between revisions of "CSC111 Lab 6 2018"

From dftwiki3
Jump to: navigation, search
(Problem 7)
(Problem 8)
Line 131: Line 131:
 
=Problem 8=
 
=Problem 8=
 
<br />
 
<br />
 +
Write a function called '''cube()''' that receives a number as a parameter, and that returns the cube of that number (or that number multiplied by itself 3 times).  Make '''cube()''' use '''square()''' when it computes its returned value.
 +
 +
>>> a = cube( 3 )
 +
>>> b = cube( 4 )
 +
>>> print( "a = ", a, " b = ", b )
 +
a =  27  b =  64
 +
 +
<br />
 +
=Problem 9=
 +
<br />
 +
Write a function called '''sumSquareCube()''' that receives a number as a parameter, and that returns the sum of the square and the cube of that number.  So, if the functions gets 3, it returns 3*3 + 3*3*3.
 +
 +
Your function '''must''' use '''square()''' and '''cube()''' that you have defined previously.
 +
 +
>>> sumSquareCube( 2 )
 +
12
 +
>>> sumSquareCube( 3 )
 +
36
 +
>>> sumSquareCube( 0 )
 +
0
 +
>>> sumSquareCube( 4 )
 +
80
 +
>>>
 +
 +
<br />
 +
=Problem 10=
 +
<br />
 +
Write a function called '''firstOf()''' that receives a list of strings as a parameter and returns the first element of the list.  You may assume that the list will always contain at least 1 element.
 +
<br />
 +
 +
>>> farm = [ "dog", "cat", "mouse", "pig" ]
 +
>>> firstOf( farm )
 +
'dog'
 +
>>> firstOf( [10, 5, 1, 20, 100] )
 +
10
 +
>>> animal = firstOf( farm )
 +
>>> print( animal )
 +
dog
 +
>>> firstOf( farm[1: ] )
 +
'cat'
 +
>>>

Revision as of 14:19, 4 March 2018

D. Thiebaut (talk) 12:27, 4 March 2018 (EST)


This lab deals, once again, with functions. Many functions, some receiving no parameters, some receiving several parameters, some returning nothing, some returning values. The goal of this lab is for you to feel comfortable creating functions that will perform simple tasks that can vary in



Problem 1


Write a function (in Idle or in the console) called bar() that prints a line of 30 #-characters.

>>> bar()
##############################


Problem 2


Create a new function called bar2() that receives the number of hash-tags and prints a line of that many #-signs.


>>> bar2( 1 )
#
>>> bar2( 10 )
##########
>>> bar2( 5 )
#####
>>> 


Problem 3


Create a new function called bar3() that receives the number of #-signs and how many to print.

>>> bar3( 10, '#' )
##########
>>> bar3( 1, 'A' )
A
>>> bar3( 5, '-o' )
-o-o-o-o-o
>>> 


Problem 4


Modify the for-loop below, replacing the '?' symbols with the appropriate expressions, so that uses the loop prints the series of lines shown.

>>> for i in range( ?, ? ):
	bar3( ?, '+' )

	
+
++
+++
++++
>>> 


Problem 5


First, run this for-loop and observe what it does:

>>> symbols = [ '#', '+', 'o', '$' ]
>>> for i in range( len( symbols ) ):
	print( symbols[i] )


Similarly to what you did in Problem 4, modify the code below so that it displays what is shown...

>>> symbols = [ '#', '+', 'o', '$' ]
>>> for i in range( len( ? ) ):
	bar3( ?, ? )

	
#
++
ooo
$$$$


Problem 6


Create a new function called bar4() that receives the number of symbols to print on one line, the symbol to use, and the number of lines to print.


>>> bar4( 3, 10, '#' )
##########
##########
##########
>>> bar4( 2, 5, '+' )
+++++
+++++
>>> bar4( 0, 5, 'o' )
>>> bar4( 3, 0, '+' ) 



>>> 


Problem 7


Did you think of using bar3() in your solution for bar4() above?
Write a function called square() that receives a number and returns its square. Make sure it behaves the same as the function square() used in the example below.

>>> square( 5 )
25
>>> a = 3
>>> square( a )
9
>>> square( square( 2 ) )
16
>>> for i in range( 1, 5 ): 
       print( "square(", i, ")=", square(i) )

	
square( 1 )= 1
square( 2 )= 4
square( 3 )= 9
square( 4 )= 16
>>> 


Problem 8


Write a function called cube() that receives a number as a parameter, and that returns the cube of that number (or that number multiplied by itself 3 times). Make cube() use square() when it computes its returned value.

>>> a = cube( 3 )
>>> b = cube( 4 )
>>> print( "a = ", a, " b = ", b )
a =  27  b =  64


Problem 9


Write a function called sumSquareCube() that receives a number as a parameter, and that returns the sum of the square and the cube of that number. So, if the functions gets 3, it returns 3*3 + 3*3*3.

Your function must use square() and cube() that you have defined previously.

>>> sumSquareCube( 2 )
12
>>> sumSquareCube( 3 )
36
>>> sumSquareCube( 0 )
0
>>> sumSquareCube( 4 )
80
>>> 


Problem 10


Write a function called firstOf() that receives a list of strings as a parameter and returns the first element of the list. You may assume that the list will always contain at least 1 element.

>>> farm = [ "dog", "cat", "mouse", "pig" ]
>>> firstOf( farm )
'dog'
>>> firstOf( [10, 5, 1, 20, 100] )
10
>>> animal = firstOf( farm )
>>> print( animal )
dog
>>> firstOf( farm[1: ] )
'cat'
>>>