Difference between revisions of "CSC111 Homework 6 Solution 2014"
(→Program 1) |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 09:46, 26 March 2014 (EDT) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 09:46, 26 March 2014 (EDT) | ||
---- | ---- | ||
− | There were several good solution programs for [[CSC111 Homework 6 Solution 2014| Homework 6]]. You were not required to use a '''main()''' function, although some did. The | + | <onlydft> |
+ | |||
+ | There were several good solution programs for [[CSC111 Homework 6 Solution 2014| Homework 6]]. You were not required to use a '''main()''' function, although some did. The programs below were selected because they presented good features (not necessarily the same). | ||
<br /> | <br /> | ||
=Program 1= | =Program 1= | ||
+ | <br /> | ||
+ | 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. | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
Line 60: | Line 64: | ||
# ================================================================== | # ================================================================== | ||
− | # | + | # MAIN PROGRAM |
# ================================================================== | # ================================================================== | ||
# main()- set the visibility to be true and print the | # main()- set the visibility to be true and print the | ||
Line 82: | Line 86: | ||
=Program 2= | =Program 2= | ||
<br /> | <br /> | ||
+ | This program gets points for good documentation and style. | ||
+ | <source lang="python"> | ||
+ | # 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() | ||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | =Program 3= | ||
+ | <br /> | ||
+ | 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. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # 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) | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | </onlydft> | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC111]][[Category:Python]][[Category:Homework]] |