Difference between revisions of "CSC111 Lab 3 2018"

From dftwiki3
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 16:27, 11 February 2018 (EST)
 
[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 16:27, 11 February 2018 (EST)
 
----
 
----
 
+
<onlydft>
  
  
Line 561: Line 561:
 
<br />
 
<br />
  
<br />
+
<!-- br />
 
{| style="width:100%; background:silver"
 
{| style="width:100%; background:silver"
 
|-
 
|-
Line 583: Line 583:
 
main()
 
main()
 
</source>
 
</source>
<br />
+
<br / -->
 +
</onlydft>
 
<br />
 
<br />
 
<showafterdate after="20180218 9:00" before="20180601 00:00">
 
<showafterdate after="20180218 9:00" before="20180601 00:00">

Latest revision as of 12:49, 1 June 2018

D. Thiebaut (talk) 16:27, 11 February 2018 (EST)



...


<showafterdate after="20180218 9:00" before="20180601 00:00">





Solution Program


# lab3Solutions.py
# D. Thiebaut
# Solution programs for CSC111 Lab #3
#
# All the solutions are group into one large main program.

def main():

    # Challenge 0
    for x in [ 0, 1, 24.99, 25, 25.001, 30, 51, 79, 99]:
        result = int( x/25 )
        print( "x = ", x, " result = ", result )

    # Teller Machine
    # Takes some amount of $ and breaks it down in 20, 10, 5, and 1
    # dollar bills.

    # set the amount
    amount = 97

    # break it down
    no20s    = amount // 20
    leftOver = amount % 20
    no10s    = leftOver // 10
    leftOver = leftOver % 10
    no5s     = leftOver // 5
    leftOver = leftOver % 5
    no1s     = leftOver

    # printout the number of bills
    print()
    print( "You want to withdraw ${0:1}".format( amount) )
    print( "Lift the keyboard and find:" )
    print( "{0:4} $20-bill(s)". format( no20s ) )
    print( "{0:4} $10-bill(s)". format( no10s ) )
    print( "{0:4} $5-bill(s)". format( no5s ) )
    print( "{0:4} $1-bill(s)". format( no1s ) )


    # ==========================================================
    # Teller Machine with a different set of denominations
    # set the amount
    amount = 197

    # break it down
    no100s   = amount // 100
    leftOver = amount % 100
    no50s    = leftOver // 50
    leftOver = leftOver % 50
    no10s    = leftOver // 10
    leftOver = leftOver % 10
    no1s     = leftOver

    # printout the number of bills
    print()
    print( "You want to withdraw ${0:1}".format( amount) )
    print( "Lift the keyboard and find:" )
    print( "{0:4} $100-bill(s)". format( no20s ) )
    print( "{0:4} $50-bill(s)". format( no10s ) )
    print( "{0:4} $10-bill(s)". format( no5s ) )
    print( "{0:4} $1-bill(s)". format( no1s ) )

    # simple output
    print()
    name = "Snow White"
    print( "Hello {0:1}! How are you?".format( name ) )
    print( "Hello {0:5}! How are you?".format( name ) )
    print( "Hello {0:10}! How are you?".format( name ) )
    print( "Hello {0:15}! How are you?".format( name ) )
    print( "Hello {0:20}! How are you?".format( name ) )

    print( "Hello {0:>1}! How are you?".format( name ) )
    print( "Hello {0:>5}! How are you?".format( name ) )
    print( "Hello {0:>10}! How are you?".format( name ) )
    print( "Hello {0:>15}! How are you?".format( name ) )
    print( "Hello {0:>20}! How are you?".format( name ) )
    
    # Formatted output, Version 1
    print( )
    for friend in [ "Bashful", "Doc", "Dopey",
                "Happy", "Sleepy", "Sneezy", "Grumpy"]:
        print( "Hello {0:10}!".format( friend ) )

    # Swapping first and second strings
    print( )
    for friend in [ "Bashful", "Doc", "Dopey", "Happy", "Sleepy", "Sneezy", "Grumpy"]:
        print( "{1:1} wants to take {0:1} to the Valentine's ball."
           . format( friend, "Snow White" ) )

    for friend in [ "Bashful", "Doc", "Dopey", "Happy", "Sleepy", "Sneezy", "Grumpy"]:
        print( "{0:1} wants to take {1:1} to the Valentine's ball."
           . format( friend, "Snow White" ) )

    # Unformatted dwarf names and length of names
    for friend in [ "Bashful", "Doc", "Dopey",
                "Happy", "Sleepy", "Sneezy", "Grumpy"]:
        print( friend, len(friend) )
   
    # Formatted output, Version 2
    bar = "+-" + 10*'-' + "-+-" + 3*'-' + "-+" 
    print( bar )
    for friend in [ "Bashful", "Doc", "Dopey",
                "Happy", "Sleepy", "Sneezy", "Grumpy"]:
        print( "| {0:10} | {1:3} |".format( friend, len(friend) ) )
    print( bar )

    # print string and int
    name = "Valentine's Day" 
    day = 15
    print( "{0:>20} is on the {1:1}th of the month" . format( name, day ) )
    name = "My birthday"
    day  = 6
    print( "{0:>20} is on the {1:1}th of the month" . format( name, day ) )

    # printing floats
    print()
    print( "Printing PI" )
    pi = 3.14159
    print( "Pi={0:1.0f}!".format( pi ) )
    print( "Pi={0:2.0f}!".format( pi ) )
    print( "Pi={0:3.0f}!".format( pi ) )
    print( "Pi={0:4.0f}!".format( pi ) )
    print( "Pi={0:5.0f}!".format( pi ) )
    print( "Pi={0:6.0f}!".format( pi ) )
    print( "Pi={0:7.0f}!".format( pi ) )
    print( "Pi={0:8.0f}!".format( pi ) )
    print( "Pi={0:9.0f}!".format( pi ) )
    print( "-------------------------" )
    print( "Pi={0:9.1f}!".format( pi ) )
    print( "Pi={0:9.2f}!".format( pi ) )
    print( "Pi={0:9.3f}!".format( pi ) )
    print( "Pi={0:9.4f}!".format( pi ) )
    print( "Pi={0:9.5f}!".format( pi ) )
    print( "-------------------------" )
    print( "Pi={0:<9.1f}!".format( pi ) )
    print( "Pi={0:<9.2f}!".format( pi ) )
    print( "Pi={0:<9.3f}!".format( pi ) )
    print( "Pi={0:<9.4f}!".format( pi ) )
    print( "Pi={0:<9.5f}!".format( pi ) )
    
    
    # Print table of temperatures
    print()
    for Fahr in range( 20, 41, 2 ):
        print( Fahr, (Fahr-32.0)*5.0/9 )
        
    print()
    for Fahr in range( 20, 41, 2 ):
        print( "{0:10.2f}F = {1:10.2f}C".format( Fahr*1.0, (Fahr-32.0)*5.0/9 ) )


    # Print table of temperatures with bars around
    bar = "+-" + 10*'-'  + "--+-" + 10*'-' + "--+"
    print( bar )
    print( "| {0:>10}  | {1:>10}  |".format( "Fahrenheit", "Celsius" ) )
    print( bar )
    for Fahr in range( 20, 41, 2 ):
        print( "| {0:10.2f}F | {1:10.2f}C |".format( Fahr*1.0, (Fahr-32.0)*5.0/9 ) )
    print( bar )

def main2():
    # compute the sum a list of numbers
    # (note that there's a much simpler way to do this in Python, which
    # we'll see later during the semester.)
    print()
    sumAll = 0
    for n in [100, 10, -30, -2, 20, 5 ]:
        sumAll = sumAll + n
        print( "n = {0:3} sum = {1:3}" . format( n, sumAll ) )
    print( "sum = ", sumAll )

    # add-up the even numbers between 0 and 100.
    print()
    sumAll = 0
    for n in range( 0, 101, 2 ):
        sumAll = sumAll + n
    print( "the sum of all the even numbers from 0 to 100, included, is",
           sumAll )
    
    # count how many items are in the list
    print()
    count = 0
    for n in [ 100, 10, -30, -2, 20, 5 ]:
        count = count + 1
        print( "n = {0:3} count = {1:3}" . format( n, count ) )
    print( "there are", count, "items in the list" )
    

    # compute the sum and average of a list of numbers
    print()
    sumAll = 0
    count = 0
    for n in [1, 2, 2, 1, 2, 3, 3, 2, 1, 0]:
        sumAll = sumAll + n
        count = count + 1
        
    print( "sum = {0:1}  average = {1:1.2f}" . format( sumAll, sumAll*1.0/count ) )

    # compute the factorial of a number
    # factorial( 10 ) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
    print()
    n = 20
    fact = 1
    for i in range( 1, n+1 ):
        print( "computing fact * i = {0:1} * {1:1} = {2:1}"
               .format( fact, i, fact*i ) )
        fact = fact * i
    print( "factorial({0:1}) = {1:1}".format( n, fact ) )

    # create a string with all the names of the 7 dwarves
    print()
    line = ""
    for friend in [ "Bashful", "Doc", "Dopey", "Happy", "Sleepy", "Sneezy", "Grumpy"]:
        line = line + friend + ", "

    print( "line =", line )

    # Challenge
    print()
    line = ""
    char1 = "*"
    char2 = "-"
    for n in ( 2, 3, 5, 3, 2 ):
        line = line + n*char1
        char1, char2 = char2, char1

    print( "line = ", line )
main2()

</showafterdate>


...