Difference between revisions of "CSC111 Homework 6 Solution 2014"

From dftwiki3
Jump to: navigation, search
(Program 3)
(Program 3)
Line 175: Line 175:
 
# each square in the grid
 
# each square in the grid
  
# Makes number of rows in dimension
+
# Prints one row of the board.  visible defines whether
def printRow(visible):
+
# the row starts with white or black cell.  Dim is the
 +
# dimension of the board, controlling the # of cells on
 +
# a row.
 +
def printRow(visible, dim):
 
     for i in range (dim):
 
     for i in range (dim):
 
         if visible==True:
 
         if visible==True:
Line 183: Line 186:
 
             print("...", end="")
 
             print("...", end="")
 
         visible = not visible
 
         visible = not visible
     return
+
     print()
  
# Makes number of columns
+
# Prompts the user for a dimension, and then print the whole chessboard
 +
# on the screen.
 
def main(visibility):
 
def main(visibility):
  
Line 191: Line 195:
 
     dim = -1
 
     dim = -1
 
     while dim <= -1:
 
     while dim <= -1:
         dim = int( Input("Enter dimension of board: "))
+
         dim = int( input("Enter dimension of board: "))
  
 
     for i in range (dim):
 
     for i in range (dim):
       print(printRow(visibility))
+
       printRow(visibility, dim)
       print(printRow(visibility))
+
       printRow(visibility, dim)
       print(printRow(visibility))
+
       printRow(visibility, dim)
 
       visibility = not visibility
 
       visibility = not visibility
  

Revision as of 10:01, 26 March 2014

--D. Thiebaut (talk) 09:46, 26 March 2014 (EDT)


There were several good solution programs for Homework 6. You were not required to use a main() function, although some did. The program below is a good example of how Python programs should be organized, with a header first, possibly a few import statements, maybe a constant or two, then a long list of function definitons (def statements), then the definition of the main() function, and at the very end, one call to main() that starts the whole program.


Program 1


# Program:  hw6a.py
# Programmer: Yumeng Wang(cf)
# (slightly edited by D. Thiebaut)
# The last edited time: 03/11/2014

# The program asks the user for an input which needs
# to be an integer that is greater or equal to 0,
# and then displays a chessboard with that dimension
# while each cell of the chessboard is represented by a
# 3*3 block.

# --------------------define functions---------------------

# printElement( visibility )- shows how to print one 1*3
# element in a 3*3 block.
def printElement( visibility ):
    if visibility == True:
        print( "###", end="" )
    else:
        print( "...", end="" )

# printOneLine( visibility )- print the first line of each row
def printOneLine( dimension, visibility ):
    
    # it prints"###" and "..." one by one until
    # the number of them is equal to the dimension
    for i in range ( dimension ):
        printElement( visibility )
        visibility = not visibility
        
    # when dimension is odd, change the visibility after 
    # it prints a line so that we have the original visibility 
    # at the beginning of every new line.
    if dimension % 2 != 0:
           visibility = not visibility
    
         
# printThreeLine( visibility )- print one row by print 
# the same line three times.
def printThreeLine( dimension, visibility ):
    for i in range (3):
        printOneLine( dimension, visibility )        
        print() 
    
# printBoard( visibility)- print the whole chessboard
# by printing the required number(dimension) of rows.
def printBoard( dimension, visibility ):
    for i in range ( dimension ):
        printThreeLine ( dimension, visibility )
        visibility = not visibility


# ==================================================================  
#                                                           MAIN PROGRAM
# ==================================================================
# main()- set the visibility to be true and print the
# printBoard( visibility ) function.
def main( ):
    # get dimension from user 
    dimension = int(Input("Enter dimention of board: "))
    while dimension < 0:
        dimension = int(Input("Enter dimention of board: "))

    printBoard( dimension, True )



main()


Program 2


This program gets points for good documentation and style.

# hw6a.py
# Thuy Nguyen (ce)
#
# This program asks the user for an integer that will represent
# a chessboard with that dimension. The chessboard uses a 3x3
# block to represent each cell of the board.
# For example, if user enters 3, this chessboard will be displayed:
#                           ###...###
#                           ###...###
#                           ###...###
#                           ...###...
#                           ...###...
#                           ...###...
#                           ###...###
#                           ###...###
#                           ###...###
# -----------------------------------------------------------------


# Makes either black (###) or white (...) segment
# for each cell
def printBlock( visible ):
    
    if visible==True:
        print( "###", end="" )
    else:
        print( "...", end="" )


# Makes indicated number of 3x3 cell(s) and alternates
# black (###) and white (...) cells for each row
def printLine( visibility, isOdd, dimension ):

    for i in range( 3 ):
        for i in range( dimension//2 ):
            printBlock( visibility )
            printBlock( not visibility )
        
        if isOdd:
            printBlock( visibility )

        print()


# Alternates black (###) and white (...) cells the number of
# times that the user indicated for each column
def repeatLine( dimension, isOdd ):

    for i in range( dimension//2 ):
        printLine( True, isOdd, dimension )
        printLine( False, isOdd, dimension )

    if isOdd:
        printLine( True, isOdd, dimension )


def main():
    # Asks user for dimension of chessboard
    dimension = int( Input( "Enter dimension of board: " ) )

    # Asks user again for another input
    # if user enters negative number
    while dimension < 0:
        dimension = int( Input( "Invalid input. Please enter 0 or positive number: " ) )

    # Boolean variable when dimension is
    # odd for the for loops in functions
    isOdd = dimension % 2 == 1

    # prints chessboard
    repeatLine( dimension, isOdd )


main()


Program 3


This program was the shortest in the class. Very tight, concise, and works well. I prefer when the program contains more functions, but this one gets special reward for concise coding.

# Claire Dudek (ah)
# (slightly edited by D. Thiebaut)
# 3/9/2014
# Homework 6
# hw6a.py
# This program takes an input for the size of the grid
# and then outputs that grid using a 3x3 of # or . for
# each square in the grid

# Prints one row of the board.  visible defines whether 
# the row starts with white or black cell.  Dim is the
# dimension of the board, controlling the # of cells on
# a row.
def printRow(visible, dim):
    for i in range (dim):
        if visible==True:
             print("###", end="")
        if visible == False:
             print("...", end="")
        visible = not visible
    print()

# Prompts the user for a dimension, and then print the whole chessboard
# on the screen.
def main(visibility):

    # Input from user - cannot be less than 0
    dim = -1
    while dim <= -1:
        dim = int( input("Enter dimension of board: "))

    for i in range (dim):
       printRow(visibility, dim)
       printRow(visibility, dim)
       printRow(visibility, dim)
       visibility = not visibility


# Makes the grid
main(True)