CSC111 Lab 8 Solution Programs

From dftwiki3
Revision as of 13:49, 25 March 2010 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


movingBall5.py

# movingBall5.py
# D. Thiebaut
from graphics import *
import random

#--- the dimensions of the window ---
W = 300
H = 300

#--- the hole, at (50, 50), and 50 high, 50 wide...
HOLEX = 50
HOLEY = 50
HOLEW = 50
HOLEH = 50 

#----------------------------------------------------------------
def isInHole( c, dx, dy ):
    r      = c.getRadius()
    center = c.getCenter()
    x      = center.getX()
    y      = center.getY()
    if ( HOLEX + r <= x <= HOLEX + HOLEW - r ) \
            and ( HOLEY + r <= y <= HOLEY + HOLEH -r ):
        return True
    else:
        return False

#----------------------------------------------------------------
def simul( ballList ):
    for step in range( 300 ):
        for i in range( len( ballList ) ):
            c, dx, dy = ballList[ i ]
            if isInHole( c, dx, dy ):
                ballList[ i ] = [ c, 0, 0 ]
            else:
                radius = c.getRadius()
                x = c.getCenter().getX()
                y = c.getCenter().getY()
                if not ( radius <= x <= W-radius ):
                    dx = -dx
                if not ( radius <= y <= H-radius ):
                    dy = -dy
                c.move( dx, dy )
                ballList[ i ] = [ c, dx, dy ]

#----------------------------------------------------------------
def waitForClick( win, message ):
    """ waitForClick: stops the GUI and displays a message.  
    Returns when the user clicks the window. The message is erased."""

    # wait for user to click mouse to start
    startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message )
    startMsg.draw( win )    # display message
    win.getMouse()          # wait
    startMsg.undraw()       # erase

#----------------------------------------------------------------
def main():
    win = GraphWin( "moving ball", W, H )
    ballList = []
    N = input( "How many balls should move around? " )
    colors = ['LightSkyBlue1', 'LightSkyBlue2', 'LightSkyBlue3', 'LightSkyBlue4', 
              'light slate blue', 'LightSlateBlue', 'light slate gray', 'LightSlateGray', 
              'light slate grey' ]

    #--- create a hole ---
    R = Rectangle( Point( HOLEX, HOLEY ), Point( HOLEX+HOLEW, HOLEY+HOLEH ) )
    R.setFill( "white" )
    R.draw( win )

    #--- define a ball position and velocity ---
    for i in range( N ):
        c = Circle( Point( random.randrange( W/3, 2*W/3 ), 
                           random.randrange( H/3, 2*H/3 ) ), 15 )
        c.setFill( colors[ i % len(colors) ] )
        c.draw( win )
        ballList.append( [ c, 3-random.randrange( 6 ), 3-random.randrange( 6 )  ] )

    waitForClick( win, "Click to Start" )

    simul( ballList )

    waitForClick( win, "Click to End" )
    win.close()

main()


stats2.py

statistics = [['Afghanistan', 28710000, 'NA', 'NA', 1], ['Albania', 3580000,
12000, 'NA', 10], ['Algeria', 32810000, 180000, 'NA', 2],
['Andorra', 69150, 24500, 'NA', 1], ['Angola', 10760000,
60000, 'NA', 1], ['Anguilla', 12738, 919, 'NA', 16],
['Antigua_and_Barbuda', 67897, 5000, 'NA', 16], ['Argentina',
38740000, 4650000, 'NA', 33], ['Armenia', 3320000, 30000, 'NA',
9], ['Aruba', 70844, 24000, 'NA', 'NA'], ['Australia', 19730000,
13010000, 9020000, 571], ['Austria', 8180000, 4650000,
1300000, 37], ['Azerbaijan', 7830000, 25000, 'NA', 2],
['Bahrain', 667238, 140200, 'NA', 1], ['Bangladesh',
138440000, 150000, 'NA', 10], ['Barbados', 277264, 6000, 'NA',
19], ['Belarus', 10330000, 422000, 'NA', 23], ['Belgium',
10280000, 4870000, 1600000, 61], ['Belize', 266440, 18000,
'NA', 2], ['Benin', 7040000, 25000, 'NA', 4], ['Bhutan',
2130000, 2500, 'NA', 'NA'], ['Bolivia', 8580000, 78000, 'NA', 9],
['Bosnia_and_Herzegovian', 3980000, 45000, 'NA', 3],
['Botswana', 1570000, 33000, 'NA', 11], ['Brazil', 182030000,
22320000, 10860000, 50], ['Brunei', 358098, 35000, 'NA', 2],
['Bulgaria', 7530000, 1610000, 'NA', 200], ['Burkina_Faso',
13220000, 25000, 'NA', 1], ['Burma', 42510000, 10000, 'NA', 1],
['Burundi', 6090000, 6000, 'NA', 1], ['Cambodia', 13120000,
10000, 'NA', 2], ['Cameroon', 15740000, 45000, 'NA', 1] ]

def main():
    popCount = 0
    userCount = 0
    for country, pop, users, active, isp in statistics:
        print "%30s (%d habitants)" % (country, pop ),
        print "Users=", users, " Active=", active, " ISP=", isp
        popCount += pop
        try:
            userCount += users
        except TypeError:
            pass # don't do anything

    print "total population = ", popCount
    print "total users      = ", userCount

main()



stats3.py

 
statistics = [['Afghanistan', 28710000, 'NA', 'NA', 1], ['Albania', 3580000,
                12000, 'NA', 10], ['Algeria', 32810000, 180000, 'NA'],
                ['Andorra', 69150, 24500, 'NA', 1], ['Angola', 10760000,
                60000, 'NA', 1], ['Anguilla', 12738, 919, 'NA', 16],
                ['Antigua_and_Barbuda', 67897, 5000, 'NA', 16], ['Argentina',
                38740000, 4650000, 'NA', 33], ['Armenia', 3320000, 30000, 'NA',
                9], ['Aruba', 70844, 24000, 'NA' ], ['Australia', 19730000,
                13010000, 9020000, 571], ['Austria', 8180000, 4650000,
                1300000, 37], ['Azerbaijan', 7830000, 25000, 'NA'],
                ['Bahrain', 667238, 140200, 'NA', 1], ['Bangladesh',
                138440000, 150000, 'NA', 10], ['Barbados', 277264, 6000, 'NA',
                19], ['Belarus', 10330000, 422000, 'NA', 23], ['Belgium',
                10280000, 4870000, 1600000, 61], ['Belize', 266440, 18000,
                'NA', 2], ['Benin', 7040000, 25000, 'NA', 4], ['Bhutan',
                2130000, 2500, 'NA', 'NA'], ['Bolivia', 8580000, 78000, 'NA', 9],
                ['Bosnia_and_Herzegovian', 3980000, 45000, 'NA', 3],
                ['Botswana', 1570000, 33000, 'NA', 11], ['Brazil', 182030000,
                22320000, 10860000, 50], ['Brunei', 358098, 35000, 'NA', 2],
                ['Bulgaria', 7530000, 1610000, 'NA', 200], ['Burkina_Faso', 
                13220000, 25000, 'NA'], ['Burma', 42510000, 10000, 'NA', 1],
                ['Burundi', 6090000, 6000, 'NA', 1], ['Cambodia', 13120000,
               10000, 'NA', 2], ['Cameroon', 15740000, 45000, 'NA', 1] ]

def main():
    popCount = 0
    for item in statistics:
        try:
            country, pop, users, active, isp = item
        except ValueError:
            country, pop, users, active = item
        
        print "%30s (%d habitants)" % (country, pop ),
        print "Users=", users, " Active=", active, " ISP=", isp
        popCount += pop

    print "total population = ", popCount

main()