Difference between revisions of "CSC111 Homework 6 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Problem #1= <br /> Study the solution programs for the chessboard section of Lab 6. Make sure you under...")
 
(Submission)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 19:11, 3 March 2014 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 19:11, 3 March 2014 (EST)
 
----
 
----
 
+
<br />
 +
__TOC__
 +
<br />
 +
<bluebox>
 +
This assignment can be done individually or in pair.  The deadline is on Thursday 3/13/2014 evening at midnight.  Because you'll be preparing for the midterm that same week, you have only one program to write this time.
 +
</bluebox>
 +
<br />
 
=Problem #1=
 
=Problem #1=
 +
[[File:Chess1.jpg|right|200px]]
 
<br />
 
<br />
 
Study the [[CSC111_Lab_6_Solutions_2014| solution programs]] for the chessboard section of  [[CSC111 Lab 6 2014| Lab 6]].  Make sure you understand the program(s).  Remember, it's ok to play with programs, to insert additional print statements here and there to see what happens, how the programs behave under different tests.
 
Study the [[CSC111_Lab_6_Solutions_2014| solution programs]] for the chessboard section of  [[CSC111 Lab 6 2014| Lab 6]].  Make sure you understand the program(s).  Remember, it's ok to play with programs, to insert additional print statements here and there to see what happens, how the programs behave under different tests.
Line 8: Line 15:
 
Your assignment is write a program that asks the user for an integer that is greater than or equal to 0, and that displays a chessboard  with that dimension.  The chessboard uses a 3x3 block to represent each cell of the board.
 
Your assignment is write a program that asks the user for an integer that is greater than or equal to 0, and that displays a chessboard  with that dimension.  The chessboard uses a 3x3 block to represent each cell of the board.
  
Below is an example of how your program should behave.  The user input is in boldface.
+
Below is an example of how your program should behave if the user enters 8 for the dimension (the user input is in boldface):
  
 
  Enter dimension of board: '''8'''
 
  Enter dimension of board: '''8'''
  ###....###....###....###....
+
  ###...###...###...###...
  ###....###....###....###....
+
  ###...###...###...###...
  ###....###....###....###....
+
  ###...###...###...###...
  ....###....###....###....###
+
  ...###...###...###...###
  ....###....###....###....###
+
  ...###...###...###...###
  ....###....###....###....###
+
  ...###...###...###...###
  ###....###....###....###....
+
  ###...###...###...###...
  ###....###....###....###....
+
  ###...###...###...###...
  ###....###....###....###....
+
  ###...###...###...###...
  ....###....###....###....###
+
  ...###...###...###...###
  ....###....###....###....###
+
  ...###...###...###...###
  ....###....###....###....###
+
  ...###...###...###...###
  ###....###....###....###....
+
  ###...###...###...###...
  ###....###....###....###....
+
  ###...###...###...###...
  ###....###....###....###....
+
  ###...###...###...###...
  ....###....###....###....###
+
  ...###...###...###...###
  ....###....###....###....###
+
  ...###...###...###...###
  ....###....###....###....###  
+
  ...###...###...###...###
  ###....###....###....###....
+
  ###...###...###...###...
  ###....###....###....###....
+
  ###...###...###...###...
  ###....###....###....###....
+
  ###...###...###...###...
  ....###....###....###....###
+
  ...###...###...###...###
  ....###....###....###....###
+
...###...###...###...###
  ....###....###....###....###
+
  ...###...###...###...###
 +
 
 +
Here is another example where the user enters 1:
 +
 
 +
Enter dimension of board: '''1'''
 +
###
 +
###
 +
  ###
 +
 
 +
Here is still another example where the user enters '''0''':
 +
 
 +
Enter dimension of board: '''0'''
 +
 +
The program does not output anything for a board, which is the correct output for dimension 0.
 +
<br />
 +
==Requirements==
 +
<br />
 +
* Call your program '''hw6a.py'''
 +
* Make sure your program keeps on prompting the user as long as the number entered is negative. Only 0 and positive numbers should be accepted.
 +
* The blocks of the board should always be 3x3 characters.   I used dots to represent white blocks, but you can use spaces if you prefer.
 +
* 20 points out of 100 will be dedicated to documentation, which include the header at the top of the program as well as a small header for each function, and comments throughout the code. The listing below is a good example of a well documented program. Use a similar approach for your own program.
 +
<br />
 +
<source lang="python">
 +
.
 +
 
 +
#hw4a.py
 +
#111c-ap
 +
#Alecia M. D'Entremont
 +
#(Edited by D. Thiebaut)
 +
#Last edited 2.25.10
 +
#This program calculates the final
 +
#grade for a student based on
 +
#grades and the weighting of those grades
 +
 
 +
### get hw grades from user, add to list, sort, and return
 +
def getGrades( num ):
 +
    print "Please enter the grades of the homework assignments",
 +
    print "below, one per line."
 +
    print
 +
    group = []
 +
    for i in range( num ):
 +
        grade = input("> ")
 +
        group.append( grade )
 +
        group.sort()
 +
    return group
 +
 
 +
### print number of assignments, find lowest   
 +
def assign(group,num):
 +
    print "Number of assignments:  ", num
 +
    print "Grade of low assignment: ", group[0]
 +
 
 +
###sum and average and weighted average of assignments (sans lowest   
 +
def summing( group1, group2, var1, var2 , num ):
 +
    summ = 0
 +
    for grade in group1[1:]:
 +
        summ = summ + grade
 +
    print "sum of all assignments (without lowest):", summ
 +
    avg = summ / ( len(group1) - 1 )
 +
 
 +
    print "Average of homework assignments:", avg
 +
    wtavg = avg*group2[0] + var1*group2[1] + var2*group2[2]
 +
    print "Weighted average of all homework assignments and exams:", wtavg
 +
 
 +
###get the weights of hw and exams
 +
def weighting( ):
 +
    group = []
 +
    for i in ["What is the overall weight of the homework assignments? ",
 +
              "What is the weight of the midterm? ",
 +
              "What is the weight of the final exam? "]:
 +
        group.append(input( i ))
 +
    return group
 +
   
 +
###put it all together!
 +
def main():
 +
    # get number of hw
 +
    num1    = input( "How many homework assignments this semester? ")
 +
 
 +
    # get weights
 +
    weights = weighting( )
 +
 
 +
    # get the grades
 +
    grades  = getGrades( num1)
 
   
 
   
>>>
+
    # get midterm and final grades
 +
    print "Please enter the midterm grade,", \
 +
            " followed by the final grade below, one per line:"
 +
    midterm = input( "> " )
 +
    final = input( "> ")
 +
 
 +
    # compute final grade
 +
    assign(grades, num1)
 +
    summing(grades, weights, midterm, final, num1)
 +
 
 +
 
 +
main()
 +
.
 +
</source>
 +
<br />
 +
 
 +
==Submission==
 +
<br />
 +
Submit your program to [http://cs.smith.edu/~thiebaut/111b/submit6.php  cs.smith.edu/~thiebaut/111b/submit6.php].
 +
<br />
 +
<!--
 +
=Solution=
 +
<br />
 +
<source lang="python">
 +
def printBlock2( visible ):
 +
    if visible==True:
 +
        print( "###", end="" )
 +
    else:
 +
        print( "...", end="" )
 +
 
 +
def printLine( visibility, dim ):
 +
    for i in range( dim ):
 +
        printBlock2( visibility )
 +
        visibility = not visibility
 +
    print()
 +
 
 +
 
 +
dim = int( input( "Enter dimension of board: " ) )
 +
firstBlack = True
 +
for i in range( dim ):
 +
    print( " ", end="" )
 +
    printLine( firstBlack, dim )
 +
    print( " ", end="" )
 +
    printLine( firstBlack, dim )
 +
    print( " ", end="" )
 +
    printLine( firstBlack, dim )
 +
    firstBlack = not firstBlack
 +
 
 +
</source>
 +
<br />
 +
-->
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC111]][[Category:Homework]][[Category:Python]]

Latest revision as of 21:56, 6 March 2014

--D. Thiebaut (talk) 19:11, 3 March 2014 (EST)




This assignment can be done individually or in pair. The deadline is on Thursday 3/13/2014 evening at midnight. Because you'll be preparing for the midterm that same week, you have only one program to write this time.


Problem #1

Chess1.jpg


Study the solution programs for the chessboard section of Lab 6. Make sure you understand the program(s). Remember, it's ok to play with programs, to insert additional print statements here and there to see what happens, how the programs behave under different tests.

Your assignment is write a program that asks the user for an integer that is greater than or equal to 0, and that displays a chessboard with that dimension. The chessboard uses a 3x3 block to represent each cell of the board.

Below is an example of how your program should behave if the user enters 8 for the dimension (the user input is in boldface):

Enter dimension of board: 8
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###
###...###...###...###...
###...###...###...###...
###...###...###...###...
...###...###...###...###
...###...###...###...###
...###...###...###...###

Here is another example where the user enters 1:

Enter dimension of board: 1
###
###
###

Here is still another example where the user enters 0:

Enter dimension of board: 0

The program does not output anything for a board, which is the correct output for dimension 0.

Requirements


  • Call your program hw6a.py
  • Make sure your program keeps on prompting the user as long as the number entered is negative. Only 0 and positive numbers should be accepted.
  • The blocks of the board should always be 3x3 characters. I used dots to represent white blocks, but you can use spaces if you prefer.
  • 20 points out of 100 will be dedicated to documentation, which include the header at the top of the program as well as a small header for each function, and comments throughout the code. The listing below is a good example of a well documented program. Use a similar approach for your own program.


.

#hw4a.py
#111c-ap
#Alecia M. D'Entremont
#(Edited by D. Thiebaut)
#Last edited 2.25.10
#This program calculates the final
#grade for a student based on
#grades and the weighting of those grades

### get hw grades from user, add to list, sort, and return
def getGrades( num ):
    print "Please enter the grades of the homework assignments",
    print "below, one per line."
    print
    group = []
    for i in range( num ):
        grade = input("> ")
        group.append( grade )
        group.sort()
    return group

### print number of assignments, find lowest    
def assign(group,num):
    print "Number of assignments:   ", num
    print "Grade of low assignment: ", group[0]

###sum and average and weighted average of assignments (sans lowest    
def summing( group1, group2, var1, var2 , num ):
    summ = 0
    for grade in group1[1:]:
        summ = summ + grade
    print "sum of all assignments (without lowest):", summ
    avg = summ / ( len(group1) - 1 )

    print "Average of homework assignments:", avg
    wtavg = avg*group2[0] + var1*group2[1] + var2*group2[2]
    print "Weighted average of all homework assignments and exams:", wtavg

###get the weights of hw and exams
def weighting( ):
    group = []
    for i in ["What is the overall weight of the homework assignments? ", 
              "What is the weight of the midterm? ", 
              "What is the weight of the final exam? "]:
        group.append(input( i ))
    return group
    
###put it all together!
def main():
    # get number of hw
    num1    = input( "How many homework assignments this semester? ")

    # get weights
    weights = weighting( )

    # get the grades
    grades  = getGrades( num1)
 
    # get midterm and final grades
    print "Please enter the midterm grade,", \
            " followed by the final grade below, one per line:"
    midterm = input( "> " )
    final = input( "> ")

    # compute final grade
    assign(grades, num1)
    summing(grades, weights, midterm, final, num1)


main()
.


Submission


Submit your program to cs.smith.edu/~thiebaut/111b/submit6.php.