Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
CS1112Fall2016Test3
Page 1 of 9
Name:
E-mailID:
Onmyhonor,IpledgethatIhaveneithergivennorreceivedhelponthistest.
Signature:
Testrulesandinformation
• Printyourname,id,andpledgeasrequested.
• Theonlypaperyoucanhaveinfrontofyouduringtheexamisthetestitself(whichincludesonescrappieceofpaper),andonepageofnotes.
• TheonlylibraryallowedtobeimportedisImageforquestions5and6.Thereisnoneedtomakeuseofget.pybecauseofnoneofyoursolutionsinvolvegettinguserinputorwebresources.
• Allsubmissionsmustbemadeduringthetest.Noforgottensubmissionswillbeacceptedafterthefact.
• Thispledgedexamisclosedtextbook.Theonlydeviceyoumayaccessduringthetestisyourownlaptop.
• Youarenotallowedtoaccessclassexamplesoryourownpastassignmentsduringthetest;i.e.,theonlyPythoncodeyoumayaccessorviewareonesthatyoudevelopforthistest.
• TheonlywindowsthatcanbeopenonyourcomputerarePyCharmandasinglebrowserwithtabsonlyopentotheclasswebsite.
• Codeshouldcompileanddemonstrateproperprogrammingstyle;e.g.,whitespace,identifiernaming,etc.
• Noneofthefunctionsyoudevelopshouldprintanyoutput.
• Eachattemptedprogramthatdoesnotgenerateanerrorwhenrunisworthfivepoints.Eachprogramthatpassestestingisworthfouradditionalpoints.
• Eachattemptedmodule thatdoesnotgenerateanerrorwhen its functionsare invoked isworth fivepoints.
• Eachattemptedfunctionisworthfourpointsexceptforfunctionschange()andreport(),whichareworthsixpoints.Anattemptedfunctionthatprintsoutputisincorrect.Theexpectedgradingrubricis
o Onepointforattemptingthefunction;o For functionsother thanchange()and report() thereare threepoints forgettingall testcases
correct.o Forfunctionschange()andreport()therearefivepointsforgettingalltestcasescorrect.
• Ourtestingofthefunctionswillinvolvedifferenttestcasesthantheonesusedforelaborativepurposesintheproblemdescriptions.
CS1112Fall2016Test3
Page 2 of 9
1. (9points)Developaprogramq01.py.Theprogramprintsthestring'Ipledgefortest3todomyhonestbest.'andnothingelse.Thus,aprogramrunshouldproduceoutput
I pledge for test 3 to do my honest best.
2. (9points)Developaprogramq02.py.Theprogrammakesasingleinputrequestforthreeintegervalues.Thevaluesarerespectivelyv1,v2,andt,wherev1andv2aretwosuccessivespeedreadingsofacarandt is an amount of time that elapsed between the readings. The program calculates and prints theaccelerationthecarunderwent.Theformulaforaccelerationis(v2–v1)/t.Nootheroutputthantheaccelerationshouldbeprinted.Belowarethreesampleruns.
Enter three integers: 50 65 2 7.5
Enter three integers: 25 63 4 9.5
Enter three integers: 90 55 5 -7.0
3. (11points)Developmoduleq03.py.Themoduledefinesasinglefunctionchange().
• Functionchange()hasasinglestringparameters.Thefunctionreturnsanewstringequaltosexceptthecaseofalllettersisswitched(e.g.,'e'becomes'E'andvice-versa).Forexample,thefollowingcodesegment
x = 'sAw'; y = q03.change( x ); print( y ) x = 'toPS'; y = q03.change( x ); print( y ) x = 'Act 1'; y = q03.change( x ); print( y )
shouldproduceoutput
SaW TOps aCT 1
• Programq03-tester.pyconsistsoftheprecedingcodesegment.
4. (13points)Developmoduleq04.py.Themoduledefinestwofunctionseval()andflip().
• Functioneval()hastwoparametersdandv,wheredisadictandvisofanytype.Thefunctionreturnsthelistofkeysindwhosevalueisv.Forexample,thefollowingcodesegment
CS1112Fall2016Test3
Page 3 of 9
d = { 0: 'z', 1 : 'o', 2: 'e', 3: 'o', 4: 'e', 5: 'o' }
ks = q04.eval( d, 'e' ); print( ks ) ks = q04.eval( d, 'i' ); print( ks ) ks = q04.eval( d, 'o' ); print( ks )
shouldproduceoutput
[2, 4] [] [1, 3, 5]
• Functionflip()hasoneparameterdoftypedict.Thefunctionreturnsanewdictidwhosekeysarethevaluesofdictd.Foreveryvaluevisind.values(),thereisamappingindictidthatmapsvtoallofthekeysindthatmaptoit.Forexample,thefollowingcodesegment
d = { 0: 'z', 1 : 'o', 2: 'e', 3: 'o', 4: 'e', 5: 'o' }
id = q04.flip( d ); print( id )
shouldproduceoutput(theorderofyourkey-valuemappingsmaybedifferent,butthemappingsthemselvesshouldbethesame)
{'z': [0], 'o': [1, 3, 5], 'e': [2, 4]}
Hint:Iexpecteval()tobehelpfulinbuildingthereturndictid.
• Program q04-tester.pyconsistsoftheprecedingcodesegments.
5. (9points)Developmoduleq05.py.Themoduledefinesafunctionnegative().
• Functionnegative()hasoneparameterdrawing,wheredrawingisanImage.Thefunctionreturnsanewimageofthesamedimensionsasdrawing.
Thepixelsinthenewimagearethecolornegativeofthecorrespondingpixelsindrawing;thatis,ifcoordinate(x,y)ofdrawinghasRGBvalue(r,g,b),thenthenewimageatcoordinate(x,y)hasRGBcolor(255–r,255–g,255–b).Forexample,thetwobelowimagesarecolornegativesofeachother.
• Programq05-tester.pytestsfunctionnegative()andshouldproducetheaboveimageontheright.
CS1112Fall2016Test3
Page 4 of 9
6. (9points)Developmoduleq06.py.Themoduledefinesafunctionaverage().
• Functionaverage()hasoneparameterdrawing,wheredrawingisanImage.Thefunctionreturnsatriple(ar,ag,ab),wherear,ag,andabarerespectivelytheintegeraveragesofthered,green,andbluecomponentsoftheRGBvaluesforthepixels indrawing.Hint:computethesumsofthereds,greens,andbluesoftheRGBvaluesforallthepixelsindrawinganddivideeachsumbythenumberofpixelsinthedrawing(i.e.,drawingwidthbydrawingheight).
• Programq06-tester.pytestsfunctionaverage()oncolorversionsoftheabovetwoimagesandshouldproduceoutput
(96, 73, 45) (75, 61, 42)
7. (11points)Developmoduleq07.py.Themoduledefinesafunctionreport().
• Functionreport()hasthreeparameters,words,misspellings,andw.
o Parameterwordsisalistofcorrectlyspelledwords,o Parametermisspellingsisadatasetofwordsandhowtheyaresometimesmisspelled.Each
rowinthedatasetisalistofstrings.Thefirststringinthelistisacorrectlyspelledword,theremainingstringsaremisspelledvariantsofthatword.Forexample,onerowinthedatasetcould be the list ['humorous', 'humerous', 'humourous']; i.e., the strings 'humerous' and'humourous'aremisspellingsof'humorous'.
o Parameterwisastring.
Thefunctionreport()returnvalueisasfollows:
o Ifwisinwords,thefunctionreturnsw;o If insteadw isanelementofoneoftherowsofmisspellings,thefunctionreturnsthefirst
elementofthatrow(i.e.,thecorrectspellingofw);o Otherwise,thefunctionreturnsthestring'*'+w+'*'.
Forexample,thefollowingcodesegment
WORDS_URL = 'http://www.cs1112.org/datasets/common_words.txt' MISSPELLINGS_URL = 'http://www.cs1112.org/datasets/common_misspellings.csv'
common = get.strings_from_url( WORDS_URL )
CS1112Fall2016Test3
Page 5 of 9
corrections = get.csv_sheet_from_url( MISSPELLINGS_URL)
s = 'mispell'; t = q07.report( common, corrections, s ); print( t ) s = 'weird'; t = q07.report( common, corrections, s ); print( t ) s = 'lol'; t = q07.report( common, corrections, s ); print( t )
shouldproduceoutput
misspell weird *lol*
• Programq07-tester.pyconsistsoftheprecedingcodesegment.
8. (25pts)Developmoduleq08.pytosupportDNAanalysesandmanipulations.
ADNAmoleculeisastrand(sequence)ofnucleotides.Thefourtypesofnucleotidesareadenine,cytosine,guanine,andthymine.Nucleotidesarenormallyrepresentedbytheirfirstletterinuppercaseformat;i.e.,A,C,G,andT.AstrandisrepresentedasasequenceofA’s,C’s,G’,andT’s;e.g.,GGAACCATGACATAG.Acell interprets itsDNA strandasprogram forwhatbiological function to carryout.An individualDNAinstructionisathree-lettersequencecalledacodon.ForDNAstrandGGAACCATGACATAG,thecodonsareGGA,ACC,ATG,ACA,andTAG.
Themoduledefinesfunctionslength(),canonical(),slice(),splice(),andinsert().Thefunctionsalldealwithstrandsofnucleotides.
• Functionlength()hasonestringparameters,wheresisastringofnucleotides.Thefunctionreturnsthenumberofnucleotidesins.Forexample,thefollowingcodesegment
n = q08.length( 'GGAACCATGACATAG' ); print( n )
shouldproduceoutput
15
• Function canonical() has one string parameter s, where s is a string of nucleotides. The functionreturnsanewupper-caseversionofs.Forexample,thefollowingcodesegment
s = q08.canonical( 'acgTgCa' ); print( s )
shouldproduceoutput
ACGTGCA
• Functionslice()hasthreeparameterss,a,andb,wheresisastringofnucleotides,andaandbareindicesintos.Thefunctionreturnsanewstringofnucleotidesthatequalsthenucleotidesofsstartingwiththenucleotideatindexaanduptobutnotincludingthenucleotideatindexb.Forexample,thefollowingcodesegment
CS1112Fall2016Test3
Page 6 of 9
s = q08.slice( 'GGAACCAT', 2, 5 ); print( s )
shouldproduceoutput
AAC
• Function splice() has two parameters s and t, where s and t are both strings of nucleotides. Thefunction returns a new string of nucleotides that equals the nucleotides of s followed by thenucleotidesoft.Forexample,thefollowingcodesegment
s = q08.splice( 'CAT', 'ACT' ); print( s )
shouldproduceoutput
CATACT
• Functioninsert()hasthreeparameterss,t,anda,wheresandtarebothstringsofnucleotidesandaisanindex.Thefunctionreturnsanewstringofnucleotidesthatequalsthenucleotidesofsstartingwith its initial nucleotide and up to but not including the nucleotide at indexa, followed by thenucleotidesoft,followedtheremainingnucleotidesofs.Forexample,thefollowingcode
s = q08.insert( 'CCCGGG', 'ATTA', 3 ); print( s )
shouldproduceoutput
CCCATTAGGG
• Programq08-tester.pyconsistsoftheprecedingcodesegments.
9. (9pts) Developmoduleq09.py tofurthersupportDNAanalysisbydefiningfunctionis_legal().
• Function is_legal()hasonestringparameters.ThefunctionreturnsaBooleanvaluewhethers isastringofupper-casenucleotides.Forexample,thefollowingcodesegment
b = q09.is_legal( 'ACTGGTCA' ); print( b ) b = q09.is_legal( 'xyz' ); print( b ) b = q09.is_legal( 'acgtactg' ); print( b )
shouldproduceoutput
True False False
• Programq09-tester.pyconsistsoftheprecedingcodesegment.
CS1112Fall2016Test3
Page 7 of 9
10. (9points) Developmoduleq10.pytofurthersupportDNAanalysisbydefiningafunctioncodons().
• Functioncodons()hasonestringparameters,wheresisastringofnucleotides.Thefunctionreturnsthelistofcodons(nucleotidetriplets)makingups.Youcanassumethelengthofs isamultipleofthree.Forexample,thefollowingcodesegment
c = q10.codons( 'ACGGAACCATGACATAGG' )
print( c )
shouldproduceoutput
['ACG', 'GAA', 'CCA', 'TGA', 'CAT', 'AGG']
• Programq10-tester.pyconsistsoftheprecedingcodesegment.11. (21points)Developmoduleq11.py to support theplayingof the two-persongamesticks.Thegame
startsoffwiththreerowsofsticks,wherethefirstrowhas1stick,themiddlerowhas3sticks,andthelastrowhas5sticks.Avisualrepresentationwouldbe
| |||
||||| Amoveofthegameistheremovalofoneortwosticksfromasinglerow.Thegameisoverwhentherearenomoresticks.
Themoduleimplementsfourfunctionssetup(),is_over(),is_valid(),andcrossoff().
• Functionsetup()hasnoparameters.Thefunctionreturnsanewthree-elementlistofintegerswhosevaluesarerespectively1,3,and5.Forexample,thefollowingcodesegment
g = q11.setup();
print( g )
shouldproduceoutput
[1, 3, 5]
• Function is_over() has one parameter sticks, where sticks is a list of three integers. The functionreturnsaBooleanvaluewhetherallofthestickselementsarezero.Forexample,thefollowingcodesegment
s1 = [ 0, 1, 0 ]; b = q11.is_over( s1 ); print( b ) s2 = [ 0, 0, 3 ]; b = q11.is_over( s2 ); print( b ) s3 = [ 0, 0, 0 ]; b = q11.is_over( s3 ); print( b )
shouldproduceoutput
False False True
CS1112Fall2016Test3
Page 8 of 9
• Functionis_valid()hasthreeparameterssticks,r,andn,wheresticksisalistofthreeintegers,andrandnareintegers.ThefunctionreturnsaBooleanvaluewhetherthedecrementingrowrofsticksbynisavalidmove:
o Ifrisnotavalidindexintosticks,thefunctionreturnsFalse.o Ifinsteadnisnotequalto1or2,thefunctionreturnsFalse.o Ifinsteadtherthelementofsticksislessthann,thefunctionreturnsFalse.o Otherwise,thefunctionreturnsTrue.
Forexample,thefollowingcodesegment
s = [ 0, 2, 1 ] ; b = q11.is_valid( s, 3, 1 ); print( b ) s = [ 0, 2, 1 ] ; b = q11.is_valid( s, 0, 1 ); print( b ) s = [ 0, 2, 1 ] ; b = q11.is_valid( s, 2, 1 ); print( b ) s = [ 0, 2, 1 ] ; b = q11.is_valid( s, 1, 2 ); print( b ) s = [ 0, 2, 1 ] ; b = q11.is_valid( s, 1, 1 ); print( b ) s = [ 0, 2, 1 ] ; b = q11.is_valid( s, 1, 0 ); print( b )
shouldproduceoutput
False False True True True False
• Functioncross_off()hasthreeparameterssticks,r,andn,wheresticksisalistofthreeintegers,andrandnareintegers.Thefunctiondoesnotreturnavalueorprintanyoutput.Ifdecrementingrowrofsticksbyn isa legalmove, then thatdecrementingofsticks isperformed;otherwise,noaction istaken.Forexample,thefollowingcodesegment
s = [ 0, 2, 1 ]; q11.cross_off( s, 3, 1 ); print( s ) s = [ 0, 2, 1 ]; q11.cross_off( s, 0, 1 ); print( s ) s = [ 0, 2, 1 ]; q11.cross_off( s, 2, 1 ); print( s ) s = [ 0, 2, 1 ]; q11.cross_off( s, 1, 2 ); print( s ) s = [ 0, 2, 1 ]; q11.cross_off( s, 1, 1 ); print( s ) s = [ 0, 2, 1 ]; q11.cross_off( s, 1, 0 ); print( s )
shouldproduceoutput
[0, 2, 1] [0, 2, 1] [0, 2, 0] [0, 0, 1] [0, 1, 1] [0, 2, 1]
• Programq11-tester.pyconsistsoftheprecedingcodesegments.
CS1112Fall2016Test3
Page 9 of 9
Scratch