Difference between revisions of "CSC111 Lab 6 2018"

From dftwiki3
Jump to: navigation, search
(Problem 4)
 
(19 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 74: Line 74:
 
  >>> symbols = [ '#', '+', 'o', '$' ]
 
  >>> symbols = [ '#', '+', 'o', '$' ]
 
  >>> for i in range( len( ? ) ):
 
  >>> for i in range( len( ? ) ):
  bar3( ?, symbols[i] )
+
  bar3( ?, ? )
 
   
 
   
 
 
 
 
Line 81: Line 81:
 
  ooo
 
  ooo
 
  $$$$
 
  $$$$
 +
 +
<br />
 +
=Problem 6 =
 +
<br />
 +
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, '+' )
 +
 +
 +
 +
>>>
 +
 +
<br />
 +
=Problem 7=
 +
<br />
 +
Did you think of using '''bar3()''' in your solution for '''bar4()''' above? 
 +
<br />
 +
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
 +
>>>
 +
 +
<br />
 +
 +
=Problem 8=
 +
<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'
 +
>>>
 +
 +
<br />
 +
=Problem 11=
 +
<br />
 +
==Preparation==
 +
<br />
 +
First play with the split() method to refresh your understanding of how it works.  Essentially it cuts strings into a list of strings using a special character or string as a divider.
 +
 +
>>> sentence = "The quick red fox jumped over the dog"
 +
>>> words = sentence.split( ' ' )
 +
>>> words
 +
???
 +
>>> sentence = "    the quick red    fox  "
 +
>>> words = sentence.split( ' ' ) # that's a space between quotes
 +
>>> words
 +
???
 +
>>> sentence
 +
'    the quick red    fox  '
 +
>>> sentence.split() # if no character is specified, splits on whitespace
 +
???
 +
>>> sentence = "Alicia,Grant,2019,Ducket"
 +
>>> words = sentence.split( ',' )
 +
>>> words
 +
????
 +
>>>
 +
 +
;Some explanations:
 +
: When you do not provide a parameter to ''split()'', it splits on groups of ''whitespace'' characters.  Whitespace characters are the space, the return character ('\n'), and the tab character.
 +
if you split on a space ' ' and the string has long groups of spaces, then you will get a list with many empty strings.  In such cases it is best to not provide anything an split on whitespace.
 +
<br />
 +
 +
==Problem==
 +
<br />
 +
Write a function called '''firstWord()''' that is given a string containing many words separated by spaces, and that returns the first word of the string.
 +
<br />
 +
 +
>>> print( "first word: ", firstWord( "Hello CSC111!" ) )
 +
first word:  Hello
 +
>>> sentence = "    The quick red fox    "
 +
>>> print( "first word: ", firstWord( sentence ) )
 +
first word:  The
 +
>>> farm = [ "dog barks", "cat hisses", "
 +
<br />
 +
 +
=Problem 12=
 +
<br />
 +
==Reviewing appending to lists==
 +
<br />
 +
You can append items to a list using the '''.append()''' method.  Play with the following example statements in the console.  Do not limit your self to just these statements.  Change them.  Try new ones...
 +
<br />
 +
 +
>>> farm = [ "dog", "pig", "hen" ]
 +
>>> farm
 +
???
 +
>>> farm.append( "cat" )
 +
>>> farm
 +
???
 +
>>> nums = [ ]
 +
>>> nums
 +
[]
 +
>>> nums.append( 3 )
 +
>>> nums
 +
???
 +
>>> nums.append( 5 )
 +
>>> nums
 +
???
 +
>>> nums.append( 10 )
 +
>>> nums
 +
???
 +
>>> tenNums = [ ]
 +
>>> for i in range( 10 ):
 +
tenNums.append( i )
 +
 +
 +
>>> tenNums
 +
???
 +
>>>
 +
 +
<br />
 +
=Problem 13=
 +
<br />
 +
Write a function called '''listFirstWords()''' that receives a list of sentences (strings with space-separated words), and returns a list of all the first words of each sentence.
 +
<br />
 +
 +
>>> parag = [ "Anna class of 18",
 +
            "Lujun class of 17",
 +
            "Vasanta class of 20" ]
 +
>>> students = listFirstWords( parag )
 +
>>> students
 +
['Anna', 'Lujun', 'Vasanta']
 +
>>> for stu in students: 
 +
print( stu )
 +
 +
 +
Anna
 +
Lujun
 +
Vasanta
 +
>>>
 +
 +
<br />
 +
=Moodle Submission=
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
 +
===Challenge Of the Day===
 +
|}
 +
[[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!  Do not '''call''' your function
 +
<br />
 +
<br />
 +
<br />
 +
</onlydft>
 +
<showafterdate after="20180310 06:00" before="20180601 00:00">
 +
 +
=Solution Program=
 +
<br />
 +
::<source lang="python">
 +
# 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()
 +
 +
</source>
 +
</showafterdate>
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:Python]][[Category:Labs]][[Category:CSC111]]

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>