Difference between revisions of "CSC111 Lab 9 Solutions 2011"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Graphics= <br /> <source lang="python"> from graphics import * def isInside( xc, yc, x1, y1, x2, y2 ): if x1 <= xc <= x2 and y1 <= yc <= y2: return 1...")
 
(Functions)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 15:51, 2 November 2011 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] 15:51, 2 November 2011 (EDT)
 
----
 
----
 +
=Functions=
 +
 +
<br />
 +
<source lang="python">
 +
 +
def boxIt( string ):
 +
    """
 +
    # unintelligent boxIt
 +
    noChars = len( string )
 +
    print( "-" * ( 2 + noChars  + 2 ) )
 +
    print( "| " + string + " |" )
 +
    print( "-" * ( 2 + noChars + 2 ) )
 +
    """
 +
    # intelligent boxIt
 +
    noChars = len( string )
 +
    if noChars==0:
 +
        return
 +
    print( "-" * ( 2 + noChars  + 2 ) )
 +
    print( "| " + string + " |" )
 +
    print( "-" * ( 2 + noChars + 2 ) )
 +
 +
def boxIt3( s1, s2, s3 ):
 +
    boxIt( s1 )
 +
    boxIt( s2 )
 +
    boxIt( s3 )
 +
 +
def multipleBoxes( L ):
 +
    for item in L:
 +
        boxIt( item )
 +
 +
def median3( a, b, c ):
 +
    L = [a, b, c]
 +
    L.sort()
 +
    return L[1]
 +
 +
def secondSmallest( L ):
 +
    if len( L ) < 2:
 +
        return None
 +
    L.sort()
 +
    return L[1]
 +
 +
def betterSecondSmallest( L ):
 +
    if len( L ) < 2:
 +
        return None
 +
    L2 = L[:] # make a copy of L into L2
 +
    L2.sort()
 +
    return L2[1]
 +
 +
def makeUserName( name ):
 +
    words = name.lower().split()
 +
    first = words[0]
 +
    last  = words[-1]
 +
    firstInitial = first[0]
 +
    return firstInitial+last
 +
   
 +
   
 +
def main():
 +
    """
 +
    boxIt( "hello!" )
 +
    boxIt( "This is a very long string!" )
 +
    boxIt( "" ) # empty string
 +
 +
    boxIt3( "hello", "there", "Smith College!" )
 +
    boxIt3( "hello", "", "previous one was empty" )
 +
    """
 +
 +
    """
 +
    multipleBoxes( [ "Doc", "Grumpy",  "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey" ] )
 +
    print( "\n\n---<<< >>>---\n\n" )
 +
   
 +
    multipleBoxes( [ "Hello", "", "", "There!" ] )
 +
    print( "\n\n---<<< >>>---\n\n" )
 +
 +
    multipleBoxes( "Hello Smith College!" . split() )
 +
    print( "\n\n---<<< >>>---\n\n" )
 +
    """
 +
 +
    print( median3( 10, 10, 20 ) )
 +
    print( median3( 1, 2, 3 ) )
 +
    print( median3( 6, 5, 7 ) )
 +
    print( median3( 8, 10, 9 ) )
 +
 +
    print( "\n\n---<<< >>>---\n\n" )
 +
 +
    L = [ 1, 10, 2, 3, 30, 40, 50 ]
 +
    ss = secondSmallest( L )
 +
    print( ss )
 +
   
 +
    print( "\n\n---<<< >>>---\n\n" )
 +
 +
    L = [ 30 ]
 +
    print( secondSmallest( L ) )
 +
 +
    print( "\n\n---<<< >>>---\n\n" )
 +
 +
    L = [ 1, 10, 2, 3, 30, 40, 50 ]
 +
    print( "L = ", L )
 +
    print( "second smallest = ", betterSecondSmallest( L ) )
 +
    print( "L = ", L )
 +
 +
    print( "\n\n---<<< >>>---\n\n" )
 +
 +
    user = makeUserName( "Alex Andra" )
 +
    print( user )
 +
 +
    print( makeUserName( "Mini Driver" ) )
 +
 +
    print( makeUserName( "Alan J. Smith" ) )
 +
   
 +
main()
 +
</source>
 +
<br />
 +
 
=Graphics=
 
=Graphics=
 
<br />
 
<br />
 +
===With Coordinates===
 
<source lang="python">
 
<source lang="python">
  
Line 47: Line 161:
 
         else:
 
         else:
 
             # center outside rectangle
 
             # center outside rectangle
 +
            c.setFill( "yellow" )
 +
 +
        if win.checkMouse() != None:
 +
            break
 +
 +
    win.close()
 +
 +
main()
 +
   
 +
 +
</source>
 +
<br />
 +
<br />
 +
=== with points ===
 +
<source lang="python">
 +
 +
 +
from graphics import *
 +
 +
 +
def isInside( center, P1, P2 ):
 +
    if ( P1.getX() <= center.getX() <= P2.getX()
 +
    and P1.getY() <= center.getY() <= P2.getY() ):
 +
          return 1
 +
    return 0
 +
 +
def main():
 +
    w = 600
 +
    h = 400
 +
    x1 = 20
 +
    y1 = 30
 +
    x2 = w // 3
 +
    y2 = h - y1
 +
    win = GraphWin( "Click me to stop!", w, h )
 +
 +
 +
    c = Circle( Point( w//2, h//4 ), 20 )
 +
    c.setFill( "yellow" )
 +
    c.draw( win )
 +
 +
    P1 = Point( x1, y1 )
 +
    P2 = Point( x2, y2 )
 +
    r = Rectangle( P1, P2 )
 +
    r.setWidth(3)
 +
    r.draw( win )
 +
 +
    dirX = 5
 +
    dirY = 3
 +
   
 +
    while True:
 +
        c.move( dirX, dirY )
 +
        xc = c.getCenter().getX()
 +
        yc = c.getCenter().getY()
 +
        if not ( 0 <= xc <= w ): dirX = -dirX
 +
        if not ( 0 <= yc <= h ): dirY = -dirY
 +
       
 +
        if isInside( c.getCenter(), P1, P2 )== 1:
 +
            # center inside rectangle
 +
            c.setFill( "red" )
 +
        else:
 +
            # center outside rectangle
 +
            c.setFill( "yellow" )
 +
 +
        if win.checkMouse() != None:
 +
            break
 +
 +
    win.close()
 +
 +
main()
 +
   
 +
 +
</source>
 +
<br />
 +
===With Objects===
 +
<br />
 +
<source lang="python">
 +
 +
 +
from graphics import *
 +
 +
 +
def isInside( circle, rect ):
 +
    if ( rect.getP1().getX() <= circle.getCenter().getX() <= rect.getP2().getX()
 +
    and rect.getP1().getY() <= circle.getCenter().getY() <= rect.getP2().getY() ):
 +
          return 1
 +
    return 0
 +
 +
def main():
 +
    w = 600
 +
    h = 400
 +
    x1 = 20
 +
    y1 = 30
 +
    x2 = w // 3
 +
    y2 = h - y1
 +
    win = GraphWin( "Click me to stop!", w, h )
 +
 +
 +
    c = Circle( Point( w//2, h//4 ), 20 )
 +
    c.setFill( "yellow" )
 +
    c.draw( win )
 +
 +
    P1 = Point( x1, y1 )
 +
    P2 = Point( x2, y2 )
 +
    r = Rectangle( P1, P2 )
 +
    r.setWidth(3)
 +
    r.draw( win )
 +
 +
    P1 = Point( x1+300, y1 )
 +
    P2 = Point( x2+300, y2 )
 +
    r2 = Rectangle( P1, P2 )
 +
    r2.setWidth( 3 )
 +
    r2.draw( win )
 +
 +
 +
    dirX = 5
 +
    dirY = 3
 +
   
 +
    while True:
 +
        c.move( dirX, dirY )
 +
        xc = c.getCenter().getX()
 +
        yc = c.getCenter().getY()
 +
        if not ( 0 <= xc <= w ): dirX = -dirX
 +
        if not ( 0 <= yc <= h ): dirY = -dirY
 +
       
 +
        if isInside( c, r  )== 1:
 +
            # center inside rectangle
 +
            c.setFill( "red" )
 +
        elif isInside( c, r2 ) == 1:
 +
            c.setFill( "blue" )
 +
        else:
 +
            # center outside rectangles
 
             c.setFill( "yellow" )
 
             c.setFill( "yellow" )
  

Latest revision as of 08:31, 3 November 2011

--D. Thiebaut 15:51, 2 November 2011 (EDT)


Functions


def boxIt( string ):
    """
    # unintelligent boxIt
    noChars = len( string )
    print( "-" * ( 2 + noChars  + 2 ) )
    print( "| " + string + " |" )
    print( "-" * ( 2 + noChars + 2 ) )
    """
    # intelligent boxIt
    noChars = len( string )
    if noChars==0:
        return
    print( "-" * ( 2 + noChars  + 2 ) )
    print( "| " + string + " |" )
    print( "-" * ( 2 + noChars + 2 ) )

def boxIt3( s1, s2, s3 ):
    boxIt( s1 )
    boxIt( s2 )
    boxIt( s3 )

def multipleBoxes( L ):
    for item in L:
        boxIt( item )

def median3( a, b, c ):
    L = [a, b, c]
    L.sort()
    return L[1]

def secondSmallest( L ):
    if len( L ) < 2:
        return None
    L.sort()
    return L[1]

def betterSecondSmallest( L ):
    if len( L ) < 2:
        return None
    L2 = L[:] # make a copy of L into L2
    L2.sort()
    return L2[1]

def makeUserName( name ):
    words = name.lower().split()
    first = words[0]
    last  = words[-1]
    firstInitial = first[0]
    return firstInitial+last
    
    
def main():
    """
    boxIt( "hello!" )
    boxIt( "This is a very long string!" )
    boxIt( "" ) # empty string

    boxIt3( "hello", "there", "Smith College!" )
    boxIt3( "hello", "", "previous one was empty" )
    """

    """
    multipleBoxes( [ "Doc", "Grumpy",  "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey" ] )
    print( "\n\n---<<< >>>---\n\n" )
    
    multipleBoxes( [ "Hello", "", "", "There!" ] )
    print( "\n\n---<<< >>>---\n\n" )

    multipleBoxes( "Hello Smith College!" . split() )
    print( "\n\n---<<< >>>---\n\n" )
    """

    print( median3( 10, 10, 20 ) )
    print( median3( 1, 2, 3 ) )
    print( median3( 6, 5, 7 ) )
    print( median3( 8, 10, 9 ) )

    print( "\n\n---<<< >>>---\n\n" )
 
    L = [ 1, 10, 2, 3, 30, 40, 50 ]
    ss = secondSmallest( L ) 
    print( ss )
    
    print( "\n\n---<<< >>>---\n\n" )

    L = [ 30 ]
    print( secondSmallest( L ) )

    print( "\n\n---<<< >>>---\n\n" )

    L = [ 1, 10, 2, 3, 30, 40, 50 ]
    print( "L = ", L )
    print( "second smallest = ", betterSecondSmallest( L ) )
    print( "L = ", L )

    print( "\n\n---<<< >>>---\n\n" )

    user = makeUserName( "Alex Andra" )
    print( user )
 
    print( makeUserName( "Mini Driver" ) )
 
    print( makeUserName( "Alan J. Smith" ) )
    
main()


Graphics


With Coordinates

from graphics import *


def isInside( xc, yc, x1, y1, x2, y2 ):
    if x1 <= xc <= x2 and y1 <= yc <= y2:
        return 1
    return 0

def main():
    w = 600
    h = 400
    x1 = 20
    y1 = 30
    x2 = w // 3
    y2 = h - y1
    win = GraphWin( "Click me to stop!", w, h )


    c = Circle( Point( w//2, h//4 ), 20 )
    c.setFill( "yellow" )
    c.draw( win )

    r = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
    r.setWidth(3)
    r.draw( win )

    dirX = 5
    dirY = 3
    
    while True:
        c.move( dirX, dirY )
        xc = c.getCenter().getX()
        yc = c.getCenter().getY()
        if not ( 0 <= xc <= w ): dirX = -dirX
        if not ( 0 <= yc <= h ): dirY = -dirY
        
        if isInside( xc, yc, x1, y1, x2, y2 )== 1:
            # center inside rectangle
            c.setFill( "red" )
        else:
            # center outside rectangle
            c.setFill( "yellow" )

        if win.checkMouse() != None:
            break

    win.close()

main()



with points

from graphics import *


def isInside( center, P1, P2 ):
    if ( P1.getX() <= center.getX() <= P2.getX() 
    and P1.getY() <= center.getY() <= P2.getY() ):
          return 1
    return 0

def main():
    w = 600
    h = 400
    x1 = 20
    y1 = 30
    x2 = w // 3
    y2 = h - y1
    win = GraphWin( "Click me to stop!", w, h )


    c = Circle( Point( w//2, h//4 ), 20 )
    c.setFill( "yellow" )
    c.draw( win )

    P1 = Point( x1, y1 )
    P2 = Point( x2, y2 )
    r = Rectangle( P1, P2 )
    r.setWidth(3)
    r.draw( win )

    dirX = 5
    dirY = 3
    
    while True:
        c.move( dirX, dirY )
        xc = c.getCenter().getX()
        yc = c.getCenter().getY()
        if not ( 0 <= xc <= w ): dirX = -dirX
        if not ( 0 <= yc <= h ): dirY = -dirY
        
        if isInside( c.getCenter(), P1, P2 )== 1:
            # center inside rectangle
            c.setFill( "red" )
        else:
            # center outside rectangle
            c.setFill( "yellow" )

        if win.checkMouse() != None:
            break

    win.close()

main()


With Objects


from graphics import *


def isInside( circle, rect ):
    if ( rect.getP1().getX() <= circle.getCenter().getX() <= rect.getP2().getX() 
    and rect.getP1().getY() <= circle.getCenter().getY() <= rect.getP2().getY() ):
          return 1
    return 0

def main():
    w = 600
    h = 400
    x1 = 20
    y1 = 30
    x2 = w // 3
    y2 = h - y1
    win = GraphWin( "Click me to stop!", w, h )


    c = Circle( Point( w//2, h//4 ), 20 )
    c.setFill( "yellow" )
    c.draw( win )

    P1 = Point( x1, y1 )
    P2 = Point( x2, y2 )
    r = Rectangle( P1, P2 )
    r.setWidth(3)
    r.draw( win )

    P1 = Point( x1+300, y1 )
    P2 = Point( x2+300, y2 )
    r2 = Rectangle( P1, P2 )
    r2.setWidth( 3 )
    r2.draw( win )


    dirX = 5
    dirY = 3
    
    while True:
        c.move( dirX, dirY )
        xc = c.getCenter().getX()
        yc = c.getCenter().getY()
        if not ( 0 <= xc <= w ): dirX = -dirX
        if not ( 0 <= yc <= h ): dirY = -dirY
        
        if isInside( c, r  )== 1:
            # center inside rectangle
            c.setFill( "red" )
        elif isInside( c, r2 ) == 1:
            c.setFill( "blue" )
        else:
            # center outside rectangles
            c.setFill( "yellow" )

        if win.checkMouse() != None:
            break

    win.close()

main()