DOC PREVIEW
TAMU CSCE 110 - key
Type Miscellaneous
Pages 6

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110: Programming IExam #2 — Answer KeyNovember 4, 2011Your final score is circled in orange. Below are the solutions for Versions A and B of Exam #2along with the regrading policy.1 Regrading PolicyIf there are any grading errors related to your exam, you must notify me in writing by Monday,November 14, 2011. After November 14th, no changes to exam grades will be considered. Below,are the steps that you must follow if you want your exam regraded.1. Write a formal statement that specifies clearly the error in question.2. Attach your statement to your exam.3. During class or office hours, give me your statement along with your exam to reconsider.Finally, if your grading error is related to wanting to receive more partial credit (on question 7 forexample), then your exam will be returned back to you. However, if there is an actual error (e.g.,a correct solution is marked incorrect, your exam score is not tallied correctly), then please followthe above steps to have your exam regraded.2 Version A1.a) 16b) 65c) 6d) 20e) -652.a) def, return, printb) 1, 12, 7, 8, 9, 3, 4, 5, 9, 10, 12, 13c) The value of result is 25.d) ne) NONE3.a) 2b) 2c) set([’tulip’, ’lily’, 7]) # set elements can be in any orderd) set([7])e) set([’lily’])4.a) [1, 2, 3, 4, 5]012 3time (hrs)12345speedExam #2 plotline Ab)5.a) floatb) stringc) 4.9d) [’5’, ’1’, ’7’, ’2’, ’4’, ’9’]e) [’5.1’, ’7.2’, ’4.9’]6.a) 12b) 22c) 4d) 162e) 27.Listing 1: q7.py1 ’’’ Computes the number of times 5 dice have two pairs . ’’’23 import random45 def roll_d i c e ():6 ’ Returns the rolls of two dice ’7 dice = []8 for i in range (5):9 dice += [ random . randint (1 ,6)]10 return dice111213 def two_pair ( dice ):14 ’ Return True if dice has two pair property . Returns False otherwise ’1516 ’’’ Use a variable count to keep track of the number of times17 dice values appear .18 * count [0]: not used and stays 0 ,19 * count [1]: nu mber of times the die value 1 appears ,20 * count [2]: nu mber of times the die value 2 appears ,21 etc .22 ’’’23 count = [0] * 72425 if len( set ( dice )) == 3:26 for die in dice :27 count [ die ] += 128 count = sorted ( count )2930 ’’’ in sorted order , the last 3 values in count31 must be 1, 2, 2, (in that order ) for the dice to have32 the two pair property .33 ’’’34 if count [4:] == [1, 2 , 2]:35 return True3637 return False383940 def main ():41 two_p a ir_coun t = 042 print ’** Dice simula t i on to count the number of two pairs that occur . ** ’43 for i in range (1000):44 if two_pair ( roll_dice ()):45 two _ pair_co u nt += 146 print ’ Two pairs appeared %d out of %d rolls of five dice .’ % (47 two_pair_count , 1000)4849 main () 33 Version B1.a) 15b) 80c) 15d) 27e) -802.a) def, return, printb) 1, 12, 8, 9, 3, 4, 5, 6, 9, 10, 12, 13c) The value of result is 29.d) NONEe) n3.a) 2b) 3c) set([9, ’pear’, ’apple’, 7]) # set elements can be in any orderd) set([9, 7]) # set elements can be in any ordere) set([’pear’])4.a) [0, 1, 2, 3, 4, 5]012 3week012345dollars($)A plot for Exam #2line Bb)45.a) floatb) stringc) 8.1d) [’4’, ’5’, ’5’, ’3’, ’8’, ’1’]e) [’4.5’, ’5.3’, ’8.1’]6.a) 15b) 555c) 6d) 36e) 437.Listing 2: q7.py1 ’’’ Computes the number of times 5 dice have two pairs . ’’’23 import random45 def roll_d i c e ():6 ’ Returns the rolls of two dice ’7 dice = []8 for i in range (5):9 dice += [ random . randint (1 ,6)]10 return dice111213 def two_pair ( dice ):14 ’ Return True if dice has two pair property . Returns False otherwis e ’1516 ’’’ Use a variable count to keep track of the number of times17 dice values appear .18 * count [0]: not used and stays 0 ,19 * count [1]: number of times the die value 1 appears ,20 * count [2]: number of times the die value 2 appears ,21 etc .22 ’’’23 count = [0] * 72425 if len( set ( dice )) == 3:26 for die in dice :27 count [ die ] += 128 count = sorted ( count )2930 ’’’ in sorted order , the last 3 values in count31 must be 1, 2, 2, (in that order ) for the dice to have32 the two pair property .33 ’’’34 if count [4:] == [1, 2 , 2]:535 return True3637 return False383940 def main ():41 two_p a ir_coun t = 042 print ’** Dice simula t i on to count the number of two pairs that occur . ** ’43 for i in range (1000):44 if two_pair ( roll_dice ()):45 two _ pair_co u nt += 146 print ’ Two pairs appeared %d out of %d rolls of five dice .’ % (47 two_pair_count , 1000)4849 main ()


View Full Document

TAMU CSCE 110 - key

Type: Miscellaneous
Pages: 6
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

21-OOP

21-OOP

8 pages

Load more
Download key
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view key and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view key 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?