Review for Final Exam - GitHub Pages · Final Exam Extra Review Session Friday, June 7th 1:00 –...
Transcript of Review for Final Exam - GitHub Pages · Final Exam Extra Review Session Friday, June 7th 1:00 –...
ReviewforFinalExamCS8:IntroductiontoComputerScience,Spring2019
Lecture#17
ZiadMatni,Ph.D.Dept.ofComputerScience,UCSB
Administrative• Homework8duetoday!• Lab6duetoday!
6/6/19 Matni,CS8,Sp19 2
FinalExamExtraReviewSessionFriday,June7th1:00–3:00PMPHELP2510
(thisisoptional)
6/6/19 Matni,CS8,Sp19 3
FinalsWeek• Dr.Matniwillhaveofficehoursonfinalsweek
Monday 1:00pm–2:30pm
6/6/19 Matni,CS8,Sp19 4
• Material:Everything!• Homework,Labs,Lectures,Textbook• Tuesday,6/11inthisclassroom• Startsat4:00PM**SHARP**• BringyourUCSBIDsandarrive10-15minutesearly• Duration:3hourslong(butreallydesignedfor1.5–2hours)• Closedbook:nocalculators,nophones,nocomputers• Allowed:1sheet(double-sidedok)ofwrittennotes
– Mustbenobiggerthan8.5”x11”– Youhavetoturnitinwiththeexam
• Youwillwriteyouranswersontheexamsheetitself.6/6/19 Matni,CS8,Sp19 5
ExampleofRecursiveFunctions:LinearSeries
• MathematicalLinearSeriesExample:
S(n)=0,1,4,13,40,… forn=0to∞What’sthepattern?Linearseries: Sn+1=A.Sn+B whereA&BareconstantsIntheexampleabove: A=3andB=1Whatisourbase-case? Whatisourrecursion?
6/6/19 Matni,CS8,Sp19 6
Example:LinearSeries• MathematicalLinearSeriesExample:
S(n)=0,1,4,13,40,… forn=0to∞Linearseries: Sn+1=3.Sn+1 and S0=0defseries(n):
ifn<=0: return0return(3*series(n-1)+1)
6/6/19 Matni,CS8,Sp19 7
recursion basecase
Example:RecursiveDrawing
6/6/19 Matni,CS8,Sp19 8
defdrawS(aTurtle,side):foriinrange(4):aTurtle.forward(side)aTurtle.right(90)defnestedBox(aTurtle,side):ifside>=1:drawS(aTurtle,side)nestedBox(aTurtle,side-5)
#Howcomethere’sno“base-case”??importturtlejim=turtle.Turtle()nestedBox(jim,100)
b/csidewilleventuallybe<1andtoosmalltodraw
MorePracticeQuestions!J
6/6/19 Matni,CS8,Sp19 9
ExerciseWhathappenswhenweusethemultiplyoperatoronalist,like:[1,2,3]*2?
① Iget[2,4,6]② Iget[1,2,3,1,2,3]③ Iget[[1,2,3],[1,2,3]]④ Igetanerrormessage
6/6/19 Matni,CS8,Sp19 10
ExerciseHowmanytimeswillthecharacter“x”printinthiscode?
① 7times② 6times③ 5times④ 4times⑤ Infinitetimes
6/6/19 Matni,CS8,Sp19 11
forminrange(3,9,2):print("x")n=mwhile(n<7):print("x")n+=7
Whatistheexactoutput?ucsb_classes=['CS8','CS16','CS24','ECON1','COMM88','MATH3A','CHEM6A']
l=[]
#Notethat:chr(65)=‘A’
forcinucsb_classes:
ifc[0]==chr(67):
l.append(c.lower()+"!")
print(l)
6/6/19 Matni,CS8,Sp19 12
ExerciseWriteaPythonfunction,AddG(s)thattakesastringsasaparameterandreturnsastringwith“g”aftereachcharacterintheoriginalstring.Forinstance:ifs="abcd"then,AddG(s)becomes"agbgcgdg”,orift="aog"then,AddG(t)becomes"agoggg”,
6/6/19 Matni,CS8,Sp19 13
Exercise• Givenadictionary: D={1:0.1,2:0.2,3:0.3,4:0.4}• Whatdoesthiscodedo?D2={}L=list(D.values())k=5forpinL: D2[k]=1+p k+=2print(D2)
6/6/19 Matni,CS8,Sp19 14
Itprints:{5:1.1,7:1.2,9:1.3,11:1.4}
6/6/19 Matni,CS8,Sp19 15
Example:ReversingaString• RecursioninstringsExample:Reverseastring
Givenastring(e.g.“hello”),youwouldneedtoreturn“olleh”Whatdoesarecursivealgorithmlooklike?Whatismybase-case?
Hints:ifs=‘hello’,whatiss[1:]?defrevStr(s):iflen(s)==0:returnsreturnrevStr(s[1:])+s[0]
6/6/19 Matni,CS8,Sp19 16