Difference between revisions of "CSC111 Lab 6 2015b"

From dftwiki3
Jump to: navigation, search
 
(9 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
<br />
 
<br />
 
<showafterdate after="20151014 12:10" before="20151231 00:00">
 
<showafterdate after="20151014 12:10" before="20151231 00:00">
<code><pre>
+
 
Problem #1:
+
==Problem #1:==
==========
+
<br />
  
 
What is the output of the following python program?
 
What is the output of the following python program?
  
</pre></code>
 
 
<br />
 
<br />
<source lang="python">
+
::<source lang="python">
 
a = 3
 
a = 3
 
b = 4
 
b = 4
Line 23: Line 22:
 
</source>
 
</source>
 
<br />
 
<br />
<code><pre>
 
  
Problem #2:
+
 
==========
+
==Problem #2:==
 +
<br />
  
 
What is the output of the following program?
 
What is the output of the following program?
 
(tricky)
 
(tricky)
  
</pre></code>
+
<br />
<source lang="python">
+
::<source lang="python">
 
print( "you win: " )
 
print( "you win: " )
 
for i in range( -3, 0, -1 ):
 
for i in range( -3, 0, -1 ):
Line 40: Line 39:
 
<br />
 
<br />
  
<code><pre>
 
  
Problem #3:
+
==Problem #3a:==
==========
+
<br />
  
 
Write the code necessary to print the first, third, fifth, seventh,... words
 
Write the code necessary to print the first, third, fifth, seventh,... words
 
of the following list of words.
 
of the following list of words.
  
</pre></code>
 
 
<br />
 
<br />
  
<source lang="python">
+
::<source lang="python">
 
L = [ "the", "quick", "red", "fox", 'jumped', 'over',
 
L = [ "the", "quick", "red", "fox", 'jumped', 'over',
 
       'the', 'lazy', 'brown', 'sleeping', 'dog' ]
 
       'the', 'lazy', 'brown', 'sleeping', 'dog' ]
 
</source>
 
</source>
 
<br />
 
<br />
<code><pre>
+
 
  
 
In other words, the output for this list of words should be:
 
In other words, the output for this list of words should be:
  
the red jumped the brown dog
+
the red jumped the brown dog
  
 
Make sure your code works with any list of words, not just the one given here.
 
Make sure your code works with any list of words, not just the one given here.
Line 66: Line 63:
 
in the list.
 
in the list.
  
 +
<br />
 +
==Problem #3b:==
 +
<br />
 +
::<source lang="python">
 +
L = [ "the", "quick", "red", "fox", 'jumped', 'over',
 +
      'the', 'lazy', 'brown', 'sleeping', 'dog' ]
 +
</source>
 +
<br />
 +
Write the code necessary to print the items in the list in reverse order.
 +
<br />
 +
 +
dog sleeping brown lazy the over jumped fox red quick the
 +
 +
Make sure your code works with any list of words, not just the one given here.
 +
In particular, you shouldn't use the number 11, which is the number of words
 +
in the list.
  
Problem #4:
+
<br />
==========
+
==Problem #4:==
 +
<br />
  
 
When we run the program below, it outputs 18 10.  
 
When we run the program below, it outputs 18 10.  
 
What were the 2 numbers entered by the user?
 
What were the 2 numbers entered by the user?
  
</pre></code>
 
 
<br />
 
<br />
<source lang="python">
+
::<source lang="python">
 
def f1( a ):
 
def f1( a ):
 
     b = 5
 
     b = 5
Line 94: Line 107:
 
</source>
 
</source>
 
<br />
 
<br />
<code><pre>
+
==Problem #5:==
 
+
<br />
Problem #5:
 
==========
 
  
 
Write a python program (you do not need to use functions, but you can if
 
Write a python program (you do not need to use functions, but you can if
Line 108: Line 119:
  
  
python problem8.py
+
::<source lang="text">
 +
What is your name? Alan Turing
  
What is your name? Alan Turing
 
</pre></code>
 
<br />
 
::<source lang="text">
 
 
*****************
 
*****************
 
*              *
 
*              *
Line 119: Line 127:
 
*              *
 
*              *
 
*****************
 
*****************
</source>
 
<code><pre>
 
 
Here is another example:
 
 
python problem8.py
 
  
 
What is your name? Nabucodonosor, King of Babylon
 
What is your name? Nabucodonosor, King of Babylon
  
</pre></code>
 
::<source lang="text">
 
 
************************************
 
************************************
 
*                                  *
 
*                                  *
Line 137: Line 137:
 
</source>
 
</source>
 
<br />
 
<br />
 +
==Problem #6:==
 +
<br />
 +
::<source lang="python">
 +
s = "name: {?:?} box #: {?:?} balance: ${?:?}"
 +
 +
a = "Sophie"
 +
bx = 1123
 +
amount = 139.50
 +
 +
print( "12345678901234567890123456789012345678901234567890" )
 +
print( s.format( a, bx, amount ) )
 +
print( s.format( "Maggie", "1111", 0 ) )
 +
 +
</source>
 +
<br />
 +
In the code above, replace the question marks by appropriate expressions, so that the output of the print statements is the following:
 +
<br />
 +
::<source lang="text">
 +
12345678901234567890123456789012345678901234567890
 +
name:    Sophie box #:      1123 balance: $139.50
 +
name:    Maggie box #: 1111      balance: $0.00
 +
</source>
 +
 +
<br />
 +
==Problem #7:==
 +
<br />
 +
What is the output of the following statements?  Will the program crash when it runs?
 +
<br />
 +
::<source lang="python">
 +
print( "{0:1}-{2:1}:{1:1}!".format( 31, 100, 1000 ) )
 +
print( "{0:1}-{0:>10}:{0:<5}!".format( "Smith", "College", 1871 )
 +
</source>
 
</showafterdate>
 
</showafterdate>
  
 
<br />
 
<br />
 +
<showafterdate after="20151015 20:00" before="20151231 00:00">
 +
=Solution Program=
 +
<source lang="python">
 +
 +
# answers to Lab #6
 +
# D. Thiebaut
 +
# CSC111 Fall 2015
 +
 +
def Question1():
 +
    print( "Question 1: Have python give you the answer!" )
 +
 +
def Question2():
 +
    print( "Question 2: Have python give you the answer!" )
 +
 +
def Question3a():
 +
    L = [ "the", "quick", "red", "fox", 'jumped', 'over',
 +
      'the', 'lazy', 'brown', 'sleeping', 'dog' ]
 +
    print( "Question 3: ", end=' ' )
 +
    for i in range( 0, len( L ), 2 ):
 +
        print( L[i], end=" " )
 +
    print()
 +
 +
def Question3b():
 +
    L = [ "the", "quick", "red", "fox", 'jumped', 'over',
 +
      'the', 'lazy', 'brown', 'sleeping', 'dog' ]
 +
    print( "Question 4: ", end=' ' )
 +
    for i in range( len( L )-1, -1, -1 ):
 +
        print( L[i], end=" " )
 +
    print()
 +
 +
def f1( a ):
 +
    b = 5
 +
    return a*2
 +
 +
def f2( b ):
 +
    a = 3
 +
    return b+a
 +
 +
 +
def Question4():
 +
    a = 7 #input( "Enter first number? " )
 +
    b = 9 #input( "Enter second number? " )
 +
    print( "Question 4: output should be 18 10" )
 +
    print(  f1( b ), f2( a ) )
 +
 +
def Question5():
 +
    name = input("Question 5\nName? " )
 +
    stars = "*" * (len( name ) + 6 )
 +
    print( stars )          #  ..
 +
    print( "*", name, "*", sep="  " )
 +
    print( stars )
 +
 +
def Question6():
 +
    #s = "name: {?:?} box #: {?:?} balance: ${?:?}"
 +
    s = "name: {0:>10} box #: {1:10} balance: ${2:1.2f}"
 +
    a = "Sophie"
 +
    bx = 1123
 +
    amount = 139.50
 +
   
 +
    print( "Question 6" )
 +
    print( "12345678901234567890123456789012345678901234567890" )
 +
    print( s.format( a, bx, amount ) )
 +
    print( s.format( "Maggie", "1111", 0 ) )
 +
 +
def Question7():
 +
    print( "Question 7: Have python give you the answer!" )
 +
 +
 +
def main():
 +
    Question1()
 +
    Question2()
 +
    Question3a()
 +
    Question3b()
 +
    Question4()
 +
    Question5()
 +
    Question6()
 +
    Question7()
 +
 +
   
 +
main()
 +
 +
   
 +
 +
</source>
 +
</showafterdate>
 
<br /><br /><br /><br /><br />
 
<br /><br /><br /><br /><br />
 
<br /><br /><br /><br /><br />
 
<br /><br /><br /><br /><br />

Latest revision as of 17:02, 14 October 2015

--D. Thiebaut (talk) 17:20, 12 October 2015 (EDT)




The lab is an exploration of various questions and concepts dealing with Python, and is a preparation for Friday's midterm exam. The questions will be given out in class, and explored on the whiteboard.


<showafterdate after="20151014 12:10" before="20151231 00:00">

Problem #1:


What is the output of the following python program?


a = 3
b = 4
for a in range( b ):
    print( a, a+b )



Problem #2:


What is the output of the following program? (tricky)


print( "you win: " )
for i in range( -3, 0, -1 ):
    print( "hip,", sep='-' )
print( "hourrah!" )



Problem #3a:


Write the code necessary to print the first, third, fifth, seventh,... words of the following list of words.


L = [ "the", "quick", "red", "fox", 'jumped', 'over',
      'the', 'lazy', 'brown', 'sleeping', 'dog' ]



In other words, the output for this list of words should be:

the red jumped the brown dog

Make sure your code works with any list of words, not just the one given here. In particular, you shouldn't use the number 11, which is the number of words in the list.


Problem #3b:


L = [ "the", "quick", "red", "fox", 'jumped', 'over',
      'the', 'lazy', 'brown', 'sleeping', 'dog' ]


Write the code necessary to print the items in the list in reverse order.

dog sleeping brown lazy the over jumped fox red quick the

Make sure your code works with any list of words, not just the one given here. In particular, you shouldn't use the number 11, which is the number of words in the list.


Problem #4:


When we run the program below, it outputs 18 10. What were the 2 numbers entered by the user?


def f1( a ):
    b = 5
    return a*2

def f2( b ):
    a = 3
    return b+a


def main():
    a = input( "Enter first number? " )
    b = input( "Enter second number? " )
    print(  f1( b ), f2( a ) )


main()


Problem #5:


Write a python program (you do not need to use functions, but you can if you prefer) that asks the user for her full name (first and last name), and outputs the name on the screen with a box of stars around it, such that the user's name is centered in the box. The box should be exactly 6 characters wider than the name of the user.

Here's an example output:


What is your name? Alan Turing

*****************
*               *
*  Alan Turing  *
*               *
*****************

What is your name? Nabucodonosor, King of Babylon

************************************
*                                  *
*  Nabucodonosor, King of Babylon  *
*                                  *
************************************


Problem #6:


s = "name: {?:?} box #: {?:?} balance: ${?:?}"

a = "Sophie"
bx = 1123
amount = 139.50

print( "12345678901234567890123456789012345678901234567890" )
print( s.format( a, bx, amount ) )
print( s.format( "Maggie", "1111", 0 ) )


In the code above, replace the question marks by appropriate expressions, so that the output of the print statements is the following:

12345678901234567890123456789012345678901234567890
name:     Sophie box #:       1123 balance: $139.50
name:     Maggie box #: 1111       balance: $0.00


Problem #7:


What is the output of the following statements? Will the program crash when it runs?

print( "{0:1}-{2:1}:{1:1}!".format( 31, 100, 1000 ) )
print( "{0:1}-{0:>10}:{0:<5}!".format( "Smith", "College", 1871 )

</showafterdate>


<showafterdate after="20151015 20:00" before="20151231 00:00">

Solution Program

# answers to Lab #6
# D. Thiebaut
# CSC111 Fall 2015

def Question1():
    print( "Question 1: Have python give you the answer!" )

def Question2():
    print( "Question 2: Have python give you the answer!" )

def Question3a():
    L = [ "the", "quick", "red", "fox", 'jumped', 'over',
      'the', 'lazy', 'brown', 'sleeping', 'dog' ]
    print( "Question 3: ", end=' ' )
    for i in range( 0, len( L ), 2 ):
        print( L[i], end=" " )
    print()

def Question3b():
    L = [ "the", "quick", "red", "fox", 'jumped', 'over',
      'the', 'lazy', 'brown', 'sleeping', 'dog' ]
    print( "Question 4: ", end=' ' )
    for i in range( len( L )-1, -1, -1 ):
        print( L[i], end=" " )
    print()

def f1( a ):
    b = 5
    return a*2

def f2( b ):
    a = 3
    return b+a


def Question4():
    a = 7 #input( "Enter first number? " )
    b = 9 #input( "Enter second number? " )
    print( "Question 4: output should be 18 10" )
    print(  f1( b ), f2( a ) )

def Question5():
    name = input("Question 5\nName? " )
    stars = "*" * (len( name ) + 6 )
    print( stars )           #  ..
    print( "*", name, "*", sep="  " )
    print( stars )

def Question6():
    #s = "name: {?:?} box #: {?:?} balance: ${?:?}"
    s = "name: {0:>10} box #: {1:10} balance: ${2:1.2f}"
    a = "Sophie"
    bx = 1123
    amount = 139.50
    
    print( "Question 6" )
    print( "12345678901234567890123456789012345678901234567890" )
    print( s.format( a, bx, amount ) )
    print( s.format( "Maggie", "1111", 0 ) )

def Question7():
    print( "Question 7: Have python give you the answer!" )


def main():
    Question1()
    Question2()
    Question3a()
    Question3b()
    Question4()
    Question5()
    Question6()
    Question7()

    
main()

</showafterdate>