Difference between revisions of "CSC111 Lab 5 2018"

From dftwiki3
Jump to: navigation, search
 
(11 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
----
 
----
  
 
+
<onlydft>
 
<br />
 
<br />
 
<!--center><videoflash>tDhhDKjatLI</videoflash></center-->
 
<!--center><videoflash>tDhhDKjatLI</videoflash></center-->
 
<br />
 
<br />
<showafterdate after="20180228 11:00" before="20180601 00:00">
+
 
 
<bluebox>
 
<bluebox>
This lab deals with functions, passing parameters, and returning values.  It is also about for you to start the Python.org documentation on the Web.  The online documentation is a great help when writing programs.  The Moodle submissions are due on Friday 10/09 at 11:55 p.m.
+
This lab deals with functions, passing parameters, and returning values.  It is also about for you to start the Python.org documentation on the Web.  The online documentation is a great help when writing programs.  The Moodle submissions are due on Friday 3/2/18 at 11:55 p.m.
 
</bluebox>
 
</bluebox>
 
<br />
 
<br />
Line 254: Line 254:
 
==Trimming DNA==
 
==Trimming DNA==
 
<br />
 
<br />
* Assume that we have a string containing the description of a DNA sequence, say "AAGACTAAAAAAGACTT."  (DNA sequences are made of 4 symbols: A, C, G, and T.)  Assume we know that "AGA" is a special marker in our DNA strings.  So our string is marked in this way: "A<font color="magenta">AGA</font>CTAAAAA<font color="magenta">AGA</font>CTT."
+
* Assume that we get a string from the user containing the description of a DNA sequence, for example "AAGACTAAAAAAGACTT."  (DNA sequences are made of 4 symbols: A, C, G, and T.)  Assume that all the DNA strings the user works with always contain two special substrings (markers) equal to "AGA".  So our DNA string is marked in this way: "A<font color="magenta">AGA</font>CTAAAAA<font color="magenta">AGA</font>CTT."
  
* Write a function called '''trimDNA( )''' that receives the original DNA string as a parameter, and returns the string where everything on the left and right of the markers has been removed.  We assume that the string will '''always contain the two markers''', so you do not have to worry about not finding the markers.  You may want to search for the '''find()''' string method in the [https://docs.python.org/3/library/stdtypes.html#string-methods Python.org] documentation to see how to find the second marker once you found the first one.  
+
* Write a function called '''trimDNA( )''' that receives a DNA string provided by the user, and returns the string where everything on the left and right of the markers has been removed.  We assume that the string will '''always contain the two markers''', so you do not have to worry about not finding the markers.  You may want to search for the '''find()''' string method in the [https://docs.python.org/3/library/stdtypes.html#string-methods Python.org] documentation to see how to find the second marker once you found the first one.  
  
 
* Example:
 
* Example:
Line 281: Line 281:
 
Make sure your function works strings of the type A<font color="magenta">AGA</font><font color="magenta">AGA</font>CTT, or <font color="magenta">AGA</font>TTTT<font color="magenta">AGA</font>.
 
Make sure your function works strings of the type A<font color="magenta">AGA</font><font color="magenta">AGA</font>CTT, or <font color="magenta">AGA</font>TTTT<font color="magenta">AGA</font>.
 
<br />
 
<br />
{| style="width:100%; background:silver"
+
<br />
|-
 
|
 
 
 
===Challenge #2===
 
|}
 
[[Image:QuestionMark2.jpg|right|120px]]
 
  
:* Add a new function to your program called '''identifyMarkers()''' that receives the DNA string and returns a different version of it, where the markers are replaced by three '#' characters.
+
* Add a new function to your program called '''identifyMarkers()''' that receives the DNA string and returns a different version of it, where the markers are replaced by three '#' characters.
  
 
<br />
 
<br />
Line 314: Line 308:
 
AGACTAAAAAAGA
 
AGACTAAAAAAGA
 
</source>
 
</source>
 +
<br />
 
<br />
 
<br />
 
==Happy Birthday!==
 
==Happy Birthday!==
Line 382: Line 377:
 
[[Image:QuestionMark9.jpg|right|120px]]
 
[[Image:QuestionMark9.jpg|right|120px]]
 
<br />
 
<br />
* Modify your solution program for Challenge 8 so that the user can enter several names on one line when prompted by your program.  Your program then "sings" happy birthday to all the friends, one after the other...
+
* Modify your solution program for the previous challenge  so that the user can enter several names on one line when prompted by your program.  Your program then "sings" happy birthday to all the friends, one after the other...
 
<br />
 
<br />
 
* Example:
 
* Example:
Line 411: Line 406:
 
<br />
 
<br />
 
<br />
 
<br />
</showafterdate>
+
<br />
  
 +
==Challenging Problems of the Day ==
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
  
 +
===Challenge #4===
 +
|}
 +
[[Image:QuestionMark3.jpg|right|120px]]
 +
<br /><br />
 +
* Write a program prompt the user for a sentence of several words.
 +
* Make your program calculate the average word length of all the words in the sentence.
 +
* Make your program outpu the average word length with TWO decimal places.
 +
* Example output:
 +
::<source lang="text">
 +
Sentence? the quick red fox jumped
 +
average word length = 4.00
 +
</source>
 
<br />
 
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
  
== Paper and Pencil Exercises ==
+
===Challenge #5===
Question 1:
+
|}
Write a program that calculates the average word length in a sentence entered by the user. Make sure you format your output to only show TWO decimal places!
+
[[Image:QuestionMark5.jpg|right|120px]]
 
+
<br />
 
 
 
 
Question 2:
 
 
Write a program that does the following:
 
Write a program that does the following:
    1. Displays the provided list of students in the class:
+
1. It contains a string with a list of student names:
              students = ["Brown,Sarah", "McCharles,Charlie", "Hudson,Stanley", "Smith,Margaret", "Brown,Ben", "Yang,Debbie"]  
+
:::<source lang="python">
    2. Prompts the user to type in a number between 0 and the length of the list of students
+
    students = ["Brown,Sarah", "McCharles,Charlie", "Hudson,Stanley",  
    3. Using the user input, get the appropriate student name and print out a new user account with the new format First Initial Last Name  
+
                "Smith,Margaret", "Brown,Ben", "Yang,Debbie"]  
         ie. Sarah Brown turns in to SBrown
+
</source>
 +
2. It displays the list of names on the screen, with an index in front of each one.
 +
2. It prompts the user to type in an integer number between 0 and 5, included.
 +
3. Using this number, it gets the appropriate student name and prints out a new computer account with format First Initial Last Name  
 +
         ie. Sarah Brown turns into sbrown.
 +
4. Try to use functions if possible!
  
 
Example Input/Output
 
Example Input/Output
 
  >>> main()
 
  >>> main()
  1 Brown,Sarah
+
 
  2 McCharles,Charlie
+
  0 Brown,Sarah
  3 Hudson,Stanley
+
  1 McCharles,Charlie
  4 Smith,Margaret
+
  2 Hudson,Stanley
  5 Brown,Ben
+
  3 Smith,Margaret
  6 Yang,Debbie
+
  4 Brown,Ben
  Which student do you want to process? 6
+
  5 Yang,Debbie
 +
  Which student? 5
 
   
 
   
  Debbie Yang: DYang
+
  Yang,Debbie dyang
 
  >>>  
 
  >>>  
  
 +
* Make sure you print a blank line before the result.  The autograder needs this blank line to fully isolate the last line of your program.
 +
<br />
 +
==Moodle Submission==
 +
<br />
 +
* Submit your solution program for Challenge 5 to Moodle, in Section LAB 5 PB 5.  You should call your program lab5_5.py, or copy/paste your program in the Edit window in Moodle.
 +
<br />
 +
<br />
 +
 +
</onlydft>
  
 
<!-- =================================================================== -->
 
<!-- =================================================================== -->
Line 454: Line 480:
 
::<source lang="python">
 
::<source lang="python">
 
   
 
   
 +
  
 
# --------------------- stickFigure -----------------------
 
# --------------------- stickFigure -----------------------
Line 543: Line 570:
  
 
# ---------------------- Snow Man ----------------------
 
# ---------------------- Snow Man ----------------------
"""
+
_ = """
 
     __
 
     __
 
   _|==|_
 
   _|==|_
Line 551: Line 578:
 
   `======'
 
   `======'
 
"""
 
"""
def hat():
+
def hat2():
 
     print("    __" )
 
     print("    __" )
 
     print("  _|==|_" )
 
     print("  _|==|_" )
  
def headRightArm():
+
def headRightArm2():
 
     print( "    ('')__/" )
 
     print( "    ('')__/" )
  
def leftArm():
+
def leftArm2():
 
     print( ">--", end="" )
 
     print( ">--", end="" )
  
def body():
+
def body2():
 
     print( "(`^^`)" )
 
     print( "(`^^`)" )
 
     print("  (`^'^'`)" )
 
     print("  (`^'^'`)" )
  
def base():
+
def base2():
 
     print( "  `======'" )
 
     print( "  `======'" )
 
     print()
 
     print()
 
      
 
      
 
def snowMan():
 
def snowMan():
     hat()
+
     hat2()
     headRightArm()
+
     headRightArm2()
     leftArm()
+
     leftArm2()
     body()
+
     body2()
     base()
+
     base2()
   
 
 
 
snowMan()
 
 
 
 
      
 
      
  
Line 620: Line 643:
 
def singHappy2( ):
 
def singHappy2( ):
 
     # get a line of names, separated by spaces
 
     # get a line of names, separated by spaces
     line = input( "? " )
+
     line = input( "Give me several names, please: " )
  
 
     # remove extra spaces at the front or back
 
     # remove extra spaces at the front or back
Line 635: Line 658:
 
     print( "\n" + ( "-"*30 ) + "oOo" + ("-"*30 ) +"\n" )
 
     print( "\n" + ( "-"*30 ) + "oOo" + ("-"*30 ) +"\n" )
  
 +
def identifyDNA( DNA ):
 +
    return DNA.replace( "AGA", "###" )
 +
 +
def trimDNA( DNA ):
 +
    marker = "AGA"
 +
    index1 = DNA.find( marker )
 +
    index2 = DNA.find( marker, index1+1 )
 +
    return DNA[index1:index2 + len( marker )]
 +
 +
def DNAProblem():
 +
    DNA = "AAGACTAAAAAAGACTT" #input( "Enter DNA: " )
 +
    print( "DNA =", DNA )
 
      
 
      
 +
    # show where the markers are
 +
    newDNA = identifyDNA( DNA )
 +
    print( newDNA )
 +
 +
    # remove symbols on each side of the markers
 +
    newDNA = trimDNA( DNA )
 +
    print( newDNA )
 +
 +
def challenge4():
 +
    sentence = "the quick red fox jumped" # input( "Sentence? " )
 +
    print( "Sentence =", sentence )
 +
    sum = 0
 +
    words = sentence.split()
 +
    for word in words:
 +
        sum = sum + len( word )
 +
    print( "average word length = {0:1.2f}".format( sum/len(words) ) )
 +
 +
def challenge5():
 +
    students = ["Brown,Sarah", "McCharles,Charlie", "Hudson,Stanley",
 +
                "Smith,Margaret", "Brown,Ben", "Yang,Debbie"]
 +
   
 +
    # display the students
 +
    for i in range( len( students ) ):
 +
        print( i, students[i] )
 +
 +
    # get index from user
 +
    index = 3 # input( "Which student? " )
 +
    name = students[index]
 +
    last, first = name.split( ',' )
 +
    account = first[0] + last
 +
    account = account.lower()
 +
    print( name, account )
 +
 
def main():
 
def main():
 
     newStickFigure()
 
     newStickFigure()
Line 642: Line 710:
 
     singHappy()
 
     singHappy()
 
     singHappy2()
 
     singHappy2()
 
+
    DNAProblem()
 +
    challenge4()
 +
    challenge5()
 +
   
 
main()
 
main()
  

Latest revision as of 13:50, 1 June 2018

D. Thiebaut (talk) 05:52, 25 February 2018 (EST)



...


<showafterdate after="20180309 08:00" before="20180601 00:00">

Solution Programs


 


# --------------------- stickFigure -----------------------

def head():
    print( "  o" )

def leftArm():
    print( " /", end="" )

def torso():
    print( "O", end="" )

def rightArm():
    print( "\\" )

def leftLeg():
    print( " /", end="" )

def rightLeg():
    print( " \\" )

def stickFigure():
    head()
    leftArm()
    torso()
    rightArm()
    leftLeg()
    rightLeg()

def body():
    leftArm()
    torso()
    rightArm()

def legs():
    leftLeg()
    rightLeg()

def newStickFigure():
    head()
    body()
    legs()

# --------------------- stickFigure1 ----------------------

def leftForearm1():
    print( " /", end="" )

def sword1():
    print( "+=====>" )

def leftArm1():
    print( " \\", end="" )

def head1():
    print( "O", end="" )

def rightArm1():
    print( "___" )

def body1():
    print( "  |" )
    print( "  |" )

def leftThigh1():
    print( " / ", end="" )

def rightThigh1():
    print( "\\" )

def leftCalf1():
    print( "/   ", end ="" )

def rightCalf1():
    print( "\\" )

def stickFigure1():
    leftForearm1()
    sword1()
    leftArm1()
    head1()
    rightArm1()
    body1()
    leftThigh1()
    rightThigh1()
    leftCalf1()
    rightCalf1()

# ---------------------- Snow Man ----------------------
_ = """
     __
   _|==|_
    ('')__/
>--(`^^`)
  (`^'^'`)
  `======'
"""
def hat2():
    print("     __" )
    print("   _|==|_" )

def headRightArm2():
    print( "    ('')__/" )

def leftArm2():
    print( ">--", end="" )

def body2():
    print( "(`^^`)" )
    print("  (`^'^'`)" )

def base2():
    print( "  `======'" )
    print()
    
def snowMan():
    hat2()
    headRightArm2()
    leftArm2()
    body2()
    base2()
    

    
# ---------------------------------------------------------


def happyBirthday():
    print( "Happy birthday to you!" )

def happyBirthdayDear( name ):
    print( "Happy birthday, dear " + name +"!"  )

def singSong( name ):
    happyBirthday()
    happyBirthday()
    happyBirthdayDear( name )
    happyBirthday()


def singHappy( ):
    # get the name from the user
    name = input( "Who's birthday should we celebrate? " )

    # sing the song for that person
    singSong( name )

# ----------------- Another Version ---------------------
def happyBirthday():
    print( "Happy birthday to you!" )

def happyBirthdayDear( name ):
    print( "Happy birthday, dear " + name +"!"  )

def singSong( name ):
    happyBirthday()
    happyBirthday()
    happyBirthdayDear( name )
    happyBirthday()

def singHappy2( ):
    # get a line of names, separated by spaces
    line = input( "Give me several names, please: " )

    # remove extra spaces at the front or back
    line = line.strip()

    # split the line into individual names, and sing song for each
    for name in line.split(  ):
        singSong( name )
        # blank line separates the different songs
        print()


def bar():
    print( "\n" + ( "-"*30 ) + "oOo" + ("-"*30 ) +"\n" )

def identifyDNA( DNA ):
    return DNA.replace( "AGA", "###" )

def trimDNA( DNA ):
    marker = "AGA"
    index1 = DNA.find( marker )
    index2 = DNA.find( marker, index1+1 )
    return DNA[index1:index2 + len( marker )]

def DNAProblem():
    DNA = "AAGACTAAAAAAGACTT" #input( "Enter DNA: " )
    print( "DNA =", DNA )
    
    # show where the markers are
    newDNA = identifyDNA( DNA )
    print( newDNA )

    # remove symbols on each side of the markers
    newDNA = trimDNA( DNA )
    print( newDNA )

def challenge4():
    sentence = "the quick red fox jumped" # input( "Sentence? " )
    print( "Sentence =", sentence )
    sum = 0
    words = sentence.split()
    for word in words:
        sum = sum + len( word )
    print( "average word length = {0:1.2f}".format( sum/len(words) ) )

def challenge5():
     students = ["Brown,Sarah", "McCharles,Charlie", "Hudson,Stanley", 
                "Smith,Margaret", "Brown,Ben", "Yang,Debbie"]
     
     # display the students
     for i in range( len( students ) ):
         print( i, students[i] )

     # get index from user
     index = 3 # input( "Which student? " )
     name = students[index]
     last, first = name.split( ',' )
     account = first[0] + last
     account = account.lower()
     print( name, account )

def main():
     newStickFigure()
     stickFigure1()
     snowMan()
     singHappy()
     singHappy2()
     DNAProblem()
     challenge4()
     challenge5()
     
main()

</showafterdate>