Difference between revisions of "CSC111 Homework 6 Solution 2014"
(→Program 2) |
|||
(7 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"> | <source lang="python"> | ||
# hw6a.py | # hw6a.py | ||
Line 159: | Line 164: | ||
</source> | </source> | ||
<br /> | <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 /> | <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 /> | <br /> | ||
+ | </onlydft> | ||
+ | |||
<br /> | <br /> | ||
<br /> | <br /> |