Difference between revisions of "CSC111 Lab 6 2018"

From dftwiki3
Jump to: navigation, search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 12:27, 4 March 2018 (EST)
 
[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 12:27, 4 March 2018 (EST)
 
----
 
----
 
+
<onlydft>
 
<bluebox>
 
<bluebox>
 
This lab deals, once again, with functions.  Many functions, some receiving no parameters, some receiving several parameters, some returning nothing, some returning values.  
 
This lab deals, once again, with functions.  Many functions, some receiving no parameters, some receiving several parameters, some returning nothing, some returning values.  
Line 282: Line 282:
 
[[Image:QuestionMark5.jpg|right|120px]]
 
[[Image:QuestionMark5.jpg|right|120px]]
  
:* Put your function '''listFirstWords()''' in a file called lab6_13.py, and submit it on Moodle in the Lab 6 section.  If your function listFirstWords() uses other functions you have created, such as '''firstWord.py''', make sure you include them in your program as well!
+
:* Put your function '''listFirstWords()''' in a file called lab6_13.py, and submit it on Moodle in the Lab 6 section.  If your function listFirstWords() uses other functions you have created, such as '''firstWord.py''', make sure you include them in your program as well! Do not '''call''' your function
 
<br />
 
<br />
 
<br />
 
<br />
 
<br />
 
<br />
<showafterdate after="20180310 06:00>
+
</onlydft>
 +
<showafterdate after="20180310 06:00" before="20180601 00:00">
  
 
=Solution Program=
 
=Solution Program=

Latest revision as of 12:57, 1 June 2018

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



...

<showafterdate after="20180310 06:00" before="20180601 00:00">

Solution Program


# lab6Solution.py
# D. Thiebaut

def bar():
    print( 30 * '#' )

def bar2( numChar ):
    print( numChar * '#' )

def bar3( numChar, ch ):
    print( numChar * ch )

def bar4( numLines, numChar, ch ):
    for i in range( numLines ):
        bar3( numChar, ch )

def square( x ):
    return x * x

def cube( x ):
    # return x * x * x
    return square( x ) * x

def sumSquareCube( x ):
    return square( x ) + cube( x )

def firstOf( list1 ):
    return list1[0]

def firstWord( string ):
    words = string.split()
    return words[0]

def listFirstWords( paragraph ):
    lines = paragraph.split( "\n" )
    first = [ ]
    for line in lines:
        first.append( firstWord( line ) )
    return first

def main():
    bar()
    bar2( 10 )
    bar2( 1 )
    bar2( 5 )
    bar3( 6, 'a' )
    bar3( 1, '#' )
    bar3( 5, '-o' )

    for i in range( 1, 5 ):
        bar3( i, '+' )

    symbols = [ '#', '+', 'o', '$' ]
    for i in range( len( symbols ) ):
        bar3( i+1, symbols[i] )
        
    bar4( 3, 10, '#' )

    for i in range( 1, 5 ):
        print( "square(", i, ")=", square(i) )

    print( "sumSquareCube(", 3, ")=",
           sumSquareCube( 3 ) )
    
    farm = ["dog", "hen", "pig", "duck" ]
    animal = firstOf( farm )
    print( "first animal = ", animal )

    word = firstWord( "hello there CSC111!" )
    print( "first word=", word )

    sentence = "  the quick red fox   "
    word = firstWord( sentence )
    print( "first word=", word )

    parag = """Anna class of 18
              Lujun class of 17
              Vasanta class of 20"""
    students = listFirstWords( parag )
    print( "students = ", students )
    
main()

</showafterdate>