Difference between revisions of "CSC111 Programs for Week 12 2015"

From dftwiki3
Jump to: navigation, search
Line 56: Line 56:
 
</source>
 
</source>
 
<br />
 
<br />
 +
=Largest, Smallest, Factorial=
 
<br />
 
<br />
 +
<source lang="python">
 +
# Find largest item recursively.  Find the smallest one, recursively.
 +
# Compute the factorial of a number.
 +
 +
def findLargestR( L ):
 +
    """given a list of numbers, returns the largest one."""
 +
    print( "findLargest(", L, ")" )
 +
   
 +
    if len( L ) == 1:
 +
        print( "Stopping condition. largest =", L[0] )
 +
        return L[0]
 +
 +
    first = L[0]
 +
    remainingL = L[1: ]
 +
    print( "first =", first, " remainingL=", remainingL )
 +
 +
    largestOfRemainingL = findLargestR( remainingL )
 +
 +
    print( "largest of remainingL is", largestOfRemainingL )
 +
   
 +
    if first > largestOfRemainingL:
 +
        return first
 +
    else:
 +
        return largestOfRemainingL
 +
 +
 +
def findSmallestR( L ):
 +
    """given a list of numbers, returns the smallest one."""
 +
    print( "findSmallestR(", L, ")" )
 +
   
 +
    if len( L ) == 1:
 +
        print( "Stopping condition. Smallest =", L[0] )
 +
        return L[0]
 +
 +
    first = L[0]
 +
    remainingL = L[1: ]
 +
    print( "first =", first, " remainingL=", remainingL )
 +
 +
    smallestOfRemainingL = findSmallestR( remainingL )
 +
 +
    print( "smallest of remainingL is", smallestOfRemainingL )
 +
   
 +
    if first < smallestOfRemainingL:
 +
        return first
 +
    else:
 +
        return smallestOfRemainingL
 +
 +
def fact( n ):
 +
    """ compute the factorial of a number."""
 +
 +
    # stopping condition
 +
    if n == 1:
 +
        return 1
 +
   
 +
    # recursive step
 +
    return n * fact( n-1 )
 +
 +
 +
def main():
 +
    """
 +
    L = [ 10, 3, 101, 23 ]
 +
 +
    smallest = findSmallestR( L )
 +
    print( "largest = ", smallest )
 +
    """
 +
 +
    while True:
 +
        n = int( input( "> " ) )
 +
        print( "factorial of", n, "=", fact(n) )
 +
 +
 +
       
 +
main()
 +
 +
</source>
 
<br />
 
<br />
 
<br />
 
<br />

Revision as of 11:46, 20 April 2015

--D. Thiebaut (talk) 07:11, 20 April 2015 (EDT)


Display Maze Recursively


  • Here's a skeleton program to start with...


# drawChessboardRecursive.py
# D. Thiebaut
# A recursive way of drawing a chessboard


from graphics import *
WIDTH  = 600
HEIGHT = 600
NOCELLS = 8
SIDE = WIDTH // NOCELLS

drawnCells = []   # keep track of cells already drawn

def drawCell( row, col, color, win ):
    """draws a square cell of the given color on the given row
    and column"""
    x1 = col * SIDE
    y1 = row * SIDE
    x2 = x1 + SIDE
    y2 = y1 + SIDE
    cell = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
    cell.setFill( color )
    cell.draw( win )

def opposite( color ):
    """return the opposite color of a given color, assuming
    that the opposite of white is black, and conversely"""
    if color=="white":
        return "black"
    else:
        return "white"

def recursiveDraw( row, col, color, win ):
    """recursively draw a chessboard"""
    return
    
def main():
    win = GraphWin( "Chessboard", WIDTH, HEIGHT )

    recursiveDraw( 0, 0, "white", win )


    win.getMouse()
    win.close()

main()


Largest, Smallest, Factorial


# Find largest item recursively.  Find the smallest one, recursively.
# Compute the factorial of a number.

def findLargestR( L ):
    """given a list of numbers, returns the largest one."""
    print( "findLargest(", L, ")" )
    
    if len( L ) == 1:
        print( "Stopping condition. largest =", L[0] )
        return L[0]

    first = L[0]
    remainingL = L[1: ]
    print( "first =", first, " remainingL=", remainingL )

    largestOfRemainingL = findLargestR( remainingL )

    print( "largest of remainingL is", largestOfRemainingL )
    
    if first > largestOfRemainingL:
        return first
    else:
        return largestOfRemainingL


def findSmallestR( L ):
    """given a list of numbers, returns the smallest one."""
    print( "findSmallestR(", L, ")" )
    
    if len( L ) == 1:
        print( "Stopping condition. Smallest =", L[0] )
        return L[0]

    first = L[0]
    remainingL = L[1: ]
    print( "first =", first, " remainingL=", remainingL )

    smallestOfRemainingL = findSmallestR( remainingL )

    print( "smallest of remainingL is", smallestOfRemainingL )
    
    if first < smallestOfRemainingL:
        return first
    else:
        return smallestOfRemainingL

def fact( n ):
    """ compute the factorial of a number."""

    # stopping condition
    if n == 1:
        return 1
    
    # recursive step
    return n * fact( n-1 )


def main():
    """
    L = [ 10, 3, 101, 23 ]

    smallest = findSmallestR( L )
    print( "largest = ", smallest )
    """

    while True:
        n = int( input( "> " ) )
        print( "factorial of", n, "=", fact(n) )


        
main()