0% found this document useful (0 votes)
333 views35 pages

Cs 116 Midterm Collection

This document provides information about private tutoring services for CS courses at the University of Waterloo, including abstract list functions that may be useful for homework questions. It contains midterm exams and solutions from past terms. The tutor, Eagle Diao, provides their contact information and cautions other tutors not to use their materials without permission.

Uploaded by

Hamad Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
333 views35 pages

Cs 116 Midterm Collection

This document provides information about private tutoring services for CS courses at the University of Waterloo, including abstract list functions that may be useful for homework questions. It contains midterm exams and solutions from past terms. The tutor, Eagle Diao, provides their contact information and cautions other tutors not to use their materials without permission.

Uploaded by

Hamad Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!

com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
University of Waterloo CS116 Midterm Collection
2011 Oda_Eagle Production
CS11!116!12!1"#!1"!1"6!2#1!2#0!2#6!"#1!"#" $utor
eaglediao%gmail&com
T"e %ollo)ing notes are relevant %or all *uestions:
+e use t"e term ,Sc"eme- as a s"ort %or ,.ntermediate Student Sc"eme )it" /am0da!-
Su''l$ e&actl$ t"e 'arts o% design reci'e re*uested in eac" *uestion! 1or a "el'er %unction, su''l$ t"e 0od$ onl$!
T"ese a0stract list %unction ma$ 0e use%ul to $ou!
2 ma' func lst 3 a''lies %unc to all elements o% lst in turn!
2 filter func lst 3 'roduces t"e list o% all elements o% lst %or )"ic" %unc 'roduces true!
2 foldr func base 2 list x-1 x-n 3 3 'roduces
2 func x-1 2 func x-n base 3 3!
2 (uild)list n func 3 'roduces 2 list 2 func 0 3 4 2 func 2 sub1 n 3 3 3!
max determines t"e largest o% t)o or more num0ers
min determines t"e smallest o% t)o or more num0ers
sqrt determines t"e 'ositive s*uare root o% a num0er
string-length determines t"e lengt" o% a string
string-append com0ines t)o or more strings into a single string, in t"e order given
check-expect determines i% t)o values are e&actl$ t"e same, and is to 0e used %or tests )"en t"e$ are re*uested
( make-posn x y ) 'roduces a 'osn structure )it" %ield value & and $
(posn-x p ) and ( posn-y p ) 'roduce t"e %irst and second %ields o% a 'osn '
( substring s p1 p2 ) 'roduces a string containing t"e c"aracters %rom a %rom 'ostion '1 t"roug" '2 51
T"is Collection contains:
2((6 1all 7idterm 1
2((6 +inter 7idterm 1
2((6 1all 7idterm 2
2((6 +inter 7idterm 2
2((6 S'ring 7idterm 1
2((6 S'ring 7idterm 2
2(1( 1all 7idterm
2(11 +inter 7idterm
CS
1.
. !"#$!"%$&'()* ,-.*/0, 1&2-3(41&56
7. 89:;<=
>?@ABCD eaglediaogmail!com
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part 1& *(stract +ist ,unctions& -m'lement follo.ing /uestions .it0 contract and
at least one e1am'le and test& 2ou 0ave to use at least one a(stract list function and
use lam(da .0ere a''ro'riate&
,all 2003 4uestions, %inis" *uestion 1 = 2 )it" %ollo)ing structures!
2 define)struct meeting title time 3
>> title is a string
>> time is a meeting5time
2 define)struct meeting5time mont" da$ time 3
>> mont" is a nat
>> da$ is a nat
>> time is a nat
1! +rite a %unction meetings)on)to'ic t"at consumes a list o% meeting structures and a string, and
'roduces t"e list o% all t"e meetings )it" t"at string as t"e title!
2! )rite a %unction resc0edule)se' t"at consumes a list o% meeting structures and 'roduces a ne)
list, )it" t"e same set o% meetings, e&ce't t"at all meetings sc"eduled %or Se'tem0er 2 indicated
0$ mont" value ?Se' 3 are moved to 9cto0er 2 indicated 0$ a mont" value o% ?9ct 3
3! +rite a %unction .eird)fn )"ic" consumes a list o% integers, and 'roduces a ne) list in )"ic"
all 'ositive values in t"e original list "ave 0een multi'lied 0$ 51
all negative values "ave 0een multi'lied 0$ 52, and
all t"e @ero values re'laced 0$ t"e string ,"a''$-
1or e&am'le , 2 .eird)fn ?22 5 51 ( 1( ( 53 3 'roduces ?252 55 2 ,"a''$- 51( ,"a''$- 6 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Winter 2003 4uestions, %inis" *uestion 1 = 2 )it" %ollo)ing structures!
;n order is a structure 2 make-order na pr unt 3, )"ere na is a s$m0ol and pr and unt are 0ot"
num0ers! T"e structure de%inition is:
2 define)struct order 2 name price units 3 3!
T"e cost o% an order is t"e 'roduct o% t"e 'rice and t"e num0er o% units!
; photo is a structure 2 make-photo s h w 3, )"ere is a s$m0ol and h and w are 0ot" num0ers!
T"e structure de%inition is:
2 define)struct photo 2 subj ht wd 3 3!
T"e area o% a photo is t"e "eig"t 2"t3 times t"e )idt" 2)d3!
Aou ma$ assume t"ese de%inition "ave alread$ 0een entered:
2 define)sturct order 2 name price units 3 3
2 define silverorder 2 make-order 'silver 10 10 3 3
2 define goldorder 2 make-order 'gold 1000 1 3 3
2 define)struct photo 2 subj ht wd 3 3
2 define dogpic 2 make-photo 'dog 5 7 3 3
2 define catpic 2 make-photo 'cat 10 3 3
2 define towerpic 2 make-photo 'tower ! 3 3
1! +rite a Sc"eme %unction total-cost to com'ute t"e total cost o% a list aloo" eac" o% )"ose
mem0ers is an order!
2! +rite a %unction big-subjects t"at consumes a list alop eac" o% )"ose mem0ers is a photo, and a
single photo"sample# Aour %unction s"ould 'roduce a list o% t"e subj o% eac" '"oto in alop )it"
greater area t"an sample#
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
S'ring 2003 4uestions, %inis" *uestion 1 = 2 = 3 )it" %ollo)ing structures!
; student is a structure 2maBe5student name id course/ist3 )"ere name is a string, id is a
'ositive integer, and course/ist is a lists o% courses! ; course is a structure 2ma5e)course name
grade3 )"ere name is a string and grade is a non5negative integer!
1! +rite a %unction create)course)list )"ic" consumes a list o% strings and a list o% non5negative
integers and 'roduces a list o% courses 2)"ere t"e %irst name and %irst grade are in t"e %irst
course, t"e second name and second grade are in t"e second course, and so on3! Aou can assume
t"e t)o lists are t"e same lengt"!
2! <sing a0stract list %unctions and )it"out using local or lengt0 or more t"an one define, )rite a
%unction a(ove)t )"ic" consumes an integer t and a student, and returns true i% and onl$ i% t"at
student "as at least one course in )"ic" t"eir grade is greater t"an or e*ual to t> ot"er)ise,
return %alse! 1or %ull marBs, $ou must use an a0stract list %unction as 'art o% $our solution!
3! <sing a0stract list %unctions and )it"out using local or more t"an one define, )rite a %unction
remove)courses )"ic" consumes a list o% students and returns a list o% 'airs 2i!e!, eac" 'air
0eing a list containing t"e name and id3 %or eac" student!
#! <sing a0stract list %unctions and )it"out using local or more t"an one define, )rite t"e %unction
5)multi'les )"ic" consumes t)o 'ositive integers B and t and 'roduces a list )it" t elements o%
t"e %orm 2list ( B 2* 2 B3 !!! 2* 2t513 B33!
,all 2010 4uestions&
1! +rite t"e 0od$ o% t"e %unction sum)of)even)s/uares t"at consumes a list o% natural num0ers
and 'roduces t"e total sum o% t"e s*uares o% t"e num0ers in lst )"ic" are even!
2! :e)rite t"e %ollo)ing %unction 0$ using at most one de%ine statment2 i!e! use lam0da )"ere
a''ro'riate3!
>> some5sum: 2 listo% 2 listo% num 3 Clengt" DE2F 3 G 2 listo% 0ool 3
2 define 2 some5sum lol 3
C 2 define 2 increasingH / 3
2 cond
C 2 I 2 %irst / 3 2 second / 3 3 true F
C else %alse F 3 3
2 define 2 %irst5t)o5non@erH / 3
2 and 2 @eroH 2 %irst / 3 3 2 @eroH 2 second / 3 3 3 3 F
2 ma' %irst5t)o5non@eroH 2 %ilter increasingH lol 3 3 3 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Winter 2011 4uestions
2define)struct contestant 2 name contest5num class5name 3 3
1! use onl$ filter! T"e %unction class)tally consumes a string class5name, )"ic" is t"e name o% a
class name, and aloc, )"ic" is a list o% contestant structures! .t 'roduces a list o% all t"e elements
in aloc )"ose class is class5name!
2! use onl$ foldr! T"e %unction contest)total consumes cnum, )"ic" is a 'rogramming contest
num0er 2 1 ,2,3,# or 5 3 and aloc, )"ic" a list o% contestants, it 'roduces t"e num0er o% elements
in aloc 'artici'ating in t"e contest )it" num0er cnum
3! use onl$ (uild)list! <se 0uild5list e&'ression so t"at it evaluates to a list containing all t"e
natural num0er %rom ( to n51 in decreasing order! T"at is, t"e e&'ression evaluate to 2 list n 51 n
2 4 2 1 ( 3 assume t"at n DE 1
#! use onl$ (uild)list! <se 0uild5list e&'ression so t"at it evaluates to a list contatining n 0ollean
values as %ollo)s! .% )e inde& t"e list elements %rom ( to n51 t"en t"e Bt" element in t"e
'roduced list )ill 0e true i% B is divisi0le 0$ 2 or 3 and %alse ot"er)ise!
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part 2& Conce't 4uestions&
4ueston 1& ,all 2003
$uppose a programmer has written a function with the following contract -
%% fn-using-fn & ' (-)* + ' listof ( + , ' listof * +
2a3 Su''ose anot"er 'rogrammer is attem'ting to use %n5using5%n, and )rites t"e %ollo)ing ,
valid Sc"eme e&'ression:
6fn)using)fn string)lengt0 6 list 7good8 7luc58 7on8 7t0e8 7e1am8 9 9
.! in a sentence or t)o, e&'lain "o) t"is e&'ression meets t"e contract %or
t"e 'arameters to %n5using5%n
..! +"at )ill 0e t"e t$'e o% data 'roduced 0$ t"is call, 0ased on t"e
contractH 2 8ote: )e )ant t"e t$'e o% data 'roduced, not t"e actual value $ou
t"inB is 'roduced 3!
203 Su''ose $et anot"er 'rogrammer is attem'ting to use %n5using5%n and )rites t"e %ollo)ing
Sc"eme e&'ression! Does it meet t"e contractH .% so, indicate t"e t$'e o% t"e out'ut t"at )ill 0e
'roduced! .% not, 0rie%l$ e&'lain "o) t"e contract is violated!
6 fn)using)fn 6 lam(da 6.9 6 sym(ol:; . <WWW 9 9 6 list 10 20 0 "0 9 9
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion 2& 4uic5 sort= Winter 2003
Jere is t"e code given in class %or *uicBsort 2 )it" some 'arts o% t"e design reci'e removed 3!
1 >> *uicBsort5list : 2 listo% num 3 G 2 listo% num 3
2 2 define 2 *uicBsort5list lst 3
3 2 cond C 2 em'ty; lst 3 em'ty F
# C else 2 local C 2 define 'ivot 2 %irst lst 3 3
5 2 define less5t"an
6 2 filter 2 lam(da 2&3 2 I & 'ivot 3 3 lst 3 3
K 2 define more5t"an
L 2 filter 2 lam(da 2&3 2 D & 'ivot 3 3 lst 3 3 F
6 2 a''end 2 *uicBsort5list less5t"an 3
1( 2 list 'ivot 3
11 2 *uicBsort5list more5t"an 3 3 3 F 3 3 3
T"e correctness o% *uicBsort5list relies on t"e assum'tion t"at all t"e num0ers in lst are di%%erent
Jo) )ould $ou c"ange t"is code so t"at it )ould 0e correct even %or a list )it" du'licate
entriesH 1or e&am'le, 2 *uicBsort5list ?2 3 2 3 1 2 3 3 s"ould 'roduce ?21 2 2 3 3 3!
Me 'recise a0out )"ere c"anges )ould 0e made 2 %or e&am'le, ,a%ter line 2, insert!!!- or ,Delete
lines #55- 3!
4uestion "& ,all 2010
Consider t"e %ollo)ing Sc"eme 'rogram:
2define unBno)n
2lam(da 2 & $ @ 3
2 $ 2 N & @ 3 2 list @ 3 3 3 3
one ot"er use%ul 'iece o% in%ormation is t"at unBno)n 'roduces t"e same t$'e as its %irst
'arameter!
2a3 )rite t"e contract %or unBno)n! Me as s'eci%ic as 'ossi0le!
203 +rite t"e 'ur'ose %or unBno)n!
2c3 )rite t)o tests %or unBno)n!
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part "& Using function as 'arameters /uestions& Provide contract= and at least
one test= and funciton (ody for eac0 of t0e follo.ing /uestions&
4uestion 1& ,all 2003
+rite a %unction c"ecB5condition )"ic" consumes t"ree 'arameters: a list o% num0ers 2nums3,
and t)o %unctions, measure and veri%$, )it" t"e %ollo)ing 'ro'erties
measure consumes a non5em't$ list o% num0ers, and 'roduces a single num0er
veri%$ consumes a single num0er, and 'roduces true i% it satis%ies a 'articular condition,
and %alse ot"er)ise!
T"e %unction c"ecB5condition 'roduces t"e s$m0ol 'empt- %or an em't$ list, 'acceptable
i% veri%$ 'roduces true )"en called )it" 2measure nums3, and ?unacce'ta0le ot"er)ise!
E&am'les %or c"ecB5condition are included 0elo):
7easure 'roduces Oeri%$ c"ecBs i% t"is
value is
1or t"e list nums C"ecB5condition
s"ould 'roduce
7inimum o% list IE1(( Em't$ ?em't$
;verage o% list DE 5( ?2L( K5 5( 6(3 ?acce'ta0le
/engt" o% list DE 1( ?2L( 2( 3( #( ( 3 ?unacce'ta0le
4uestion 2& Winter 2003
+rite a %unction func-max t"at consumes a %unction func and a non5em't$ list lst# T"e %unction
func is a one5argument %unction t"at 'roduces a num0er! T"e %unction func-max 'roduces an
element elem o% lst suc" t"at 2 func elem 3 is t"e largest 'ossi0le! .% t)o or more values %rom lst
'roduce t"e same largest num0er )"en func is a''lied to t"em, func-max 'roduces t"e one
closest to t"e 0eginning o% t"e list! 1or e&am'le , 2 func-max s.r 2 list 1 52 5# # 3 3 'roduces 5#!
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion "& S'ring 2003
<sing a0stract list %unctions, )rite t"e %unction %irst50etter )"ic" consumes t)o ,a''lication-
%unctions, one ,com'arison- %unction and a list! T"e ,a''lication- %unctions eac" consume one
element o% t"e same t$'e! T"e ,a''lication- %unctions 'roduce t"e same t$'e o% element
2t"oug" not necessaril$ t"e same t$'e as t"e$ consume3! T"e ,com'arison- %unction consumes
t)o elements o% t"e same t$'e and 'roduces a 0oolean value! T"e %unction %irst50etter s"ould
'roduce a list o% t"ose elements %rom t"e original list %or )"ic" t"e ,com'arison- %unction
'roduces true )"en given t"e %irst ,a''lication- %unction a''lied on t"at element and t"e
second ,a''lication- %unction a''lied on t"at element!
1or e&am'le, 2first)(etter s*r s*rt DE 2list ( (!36 1 233 3 'roduce 2list ( 1 23
4uestion #& ,all 2010
T"e %unction f consumes a 0oolean %unction %1, and a %unction %2, and a list lst, t"e %unctions %1
and %2 eac" consume a single 'arameter! f 'roduces t"e list )"ere %2 "as 0een a''lied to all
elements e o% lst suc" t"at 2 %1 e 3 'roduces true! E&am'le: 2 f evenH add1 2 list 11 2 6 # 55 3 3 G
2 list 3 5 3
4uestion & Winter 2011
T"e %unction com0ine5lists consumes a %unction %, and t)o lists list1 and list2, )"ic" "ave t"e
same lengt"! .t 'roduces a list o0tained 0$ a''l$ing % to 'airs o% corres'onding elements o% list1
and list2 2 in order3! Jere are t)o e&am'les!
2com0ine5lists N 2 list 1 2 3 3 2 list # 5 6 3 3 G 2 list 5 K 6 3
2com0ine5lists
2 lam0da 2 s n 3 2 D 2 string5lengt" s 3 n 3 3
2 list ,a0cd- ,e%- ,g"iPB- 3
2 list 3 6 2 3 3 ED 2 list true %alse true 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part & Produce a function using lam(da&
4uestion 1& ,all 2003
+rite a %unction a''end)to)all )"ic" consumes a string s, and 'roduces a one5'arameter
%unction! T"e 'roduced %unction consumes a list o% string values, and 'roduces a ne) list, )it"
t"e string s a''ended to eac" string in t"e given list!
1or e&am'le, i% a''end)to)all is called )it" ,u)!ca-, and t"e 'roduced %unction is called
)it" t"e list ?2,lmcase- ,P2Kli- ,cs116- 3, it s"ould 'roduce t"e list ?2,lmcaseu)!ca-
,P2Kliu)!ca- ,cs116u)!ca- 3!
4uestion 2& Winter 2003
+rite a %unction make-list-adder t"at consumes a list o% num0ers lst and 'roduces a one5
argument %unction! T"at one5argument %unction consumes a num0er addend and 'roduces t"e
list %ormed 0$ adding addend to eac" element o% lst#
4uestion "& S'ring 2003
<sing a0stract list %unctions, )rite t"e %unction mult5list t"at consumes a list lst o% num0ers and
'roduces a one5argument %unction! +"en t"at %unction is called )it" a num0er m, t"e 'roduced
value is t"e list %ormed 0$ multi'l$ing m 0$ ever$ element o% lst!
4uestion #& Winter 2011
T"e %unction onl$5one consumes a non5em't$ string s and 'roduces a %unction! T"e 'roduced
%unction consumes a list o% strings and 'roduces a list Pust liBe it, e&ce't t"at
s is t"e %irst value in t"e 'roduced list,
all ot"er occurrences o% s, i% an$, "ave 0een removed!
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part & >ra.ing 4uestions& Su''ose t0e follo.ing Sc0eme e1'ressions 0ave (een
entered into t0e -nteractions .indo. in >rSc0eme= in t0e order t0at a''ears (elo.&
Using t0e memory model s0o.n in class= dra. a 'icture s0o.ing t0e assignment of
values to varia(les&
4uestion 1& ,all 2003
2a3 dra) memor$ model!
2 define 1 3 3
2 define y 1 9
2 define origin 2 maBe5'osn 5 K 3 3
2 define dst1 2 maBe5'osn origin ( 3 3
2 define dst2 dst1 9
2 define dst3 2 maBe5'osn 5 K 3 3
203 Su''ose t"e %ollo)ing e&'ressions are entered into t"e .nteractions )indo) a%ter
e&'ressions entered in 'art 2a3! 1or eac" e&'ression %ollo)ed 0$ an em't$ 0o&, %ill t"e
0o& )it" )"at t"e e&'ression 'roduces! T"e %irst 0o& "as 0een %illed in %or $ou!
& 3
2 setQ & 2 N & # 3 3
&
$
2 set5'osn5&Q origin dst3 3
2 'osn5& dst1 3
2 'osn5& dst2 3
2 'osn5& dst3 3
2 set5'osn5$Q dst1 $ 3
2 setQ $ 6 3
2 'osn5$ dst1 3
2 setQ dst3 2 maBe5'osn 3 6 3 3
2 'osn5& origin 3
2 e*ualH 2 'osn5$ 2 'osn5& 2 'osn5&
dst1 3 3 3 $ 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion 2& Winter 2003
2a3 Dra) memor$ model
2 define num1 # 3
2 define num2 5K 3
2 define num3 num1 3
2 define 't1 2 maBe5'osn 3 ( 3 3
2 define 't2 2 maBe5'osn 3 ( 3 3
2 define 't3 't1 3
203 Su''ose t"e %ollo)ing e&'ressions are entered into t"e .nteractions )indo) a%ter
e&'ressions entered in 'art 2a3! 1or eac" e&'ression %ollo)ed 0$ an em't$ 0o&, %ill t"e
0o& )it" )"at t"e e&'ression 'roduces! T"e %irst 0o& "as 0een %illed in %or $ou!
num1 #
2 setQ 8um1 L 3
num1
num3
2 set5'osn5&Q 't1 1 3
2 'osn5& 't1 3
2 'osn5& 't2 3
2 set5'osn5$Q 't2 num2 3
2 setQ num2 6 3
2 'osn5$ 't2 3
2 setQ 't3 2 maBe5'osn 3 ( 3 3
2 E 2 'osn5& 't1 3 2 'osn5& 't3 3 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion "& S'ring 2003
2a3 Dra) memor$ diagram!
2define num1 L3
2define num2 num13
2define 't1 2maBe5'osn 3 (33
2define 't2 2maBe5'osn 3 (33
2define 't3 't13
2define 't# 2maBe5'osn 't1 533
203 Su''ose t"e %ollo)ing e&'ressions are entered into t"e .nteractions )indo) a%ter
e&'ressions entered in 'art 2a3! 1or eac" e&'ression %ollo)ed 0$ an em't$ 0o&, %ill t"e
0o& )it" )"at t"e e&'ression 'roduces! T"e %irst 0o& "as 0een %illed in %or $ou!
num1 L
2setQ num1 2N num1 L33
num1
num2
2set5'osn5&Q 't1 13
2'osn5& 't13
2'osn5& 't23
2'osn5& 't33
2'osn5& 't#3
2set5'osn5$Q 't2 num23
2setQ num2 63
2'osn5$ 't23
2setQ 't3 2maBe5'osn 3 (33
2e*ualH 't1 't33
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion #& ,all 2010
2a3 dra) t"e memor$ diagram!
2define c1 2 maBe5card L ?"earts33
2define c2 2 maBe5card L ?"earts33
2define c3 c2 3
2define & 16KK 3
2define $ 2 setQ R 2 N & 2( 3 3
203 consider t"e %ollo)ing starting state, )rite code to trans%er %ollo)ing state to diagram
given!
2define num1 void3
2define num2 void3
2define num3 void3
2define 't1 void 3
2define 't2 void 3
2define 't3 void 3
89T 19: C977E:.C;/ <SE, tutor
# 5K #
3
3 (
num1 num2 num3
't1
't2
't3
maBe5R
'osn
'osn
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion & Winter 2011
2a3 dra) t"e memor$ diagram!
2 de%ine a 12 3
2 de%ine 0 a 3
2 de%ine c 2 maBe5'osn 3 a 3 3
2 setQ a K 3
2 de%ine d c 3
2 setQ c 2 maBe5'osn 1K 66 3 3
2 set5'osn5&Q d a 3
2 de%ine % 2 maBe5contestant ,/ennon- ,cs116- 1 33
2 de%ine g 2 contestant5class % 3 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part 6& *ccumulative recursion& ,or eac0 of t0e follo.ing /uestions= 'rovide a
contract and function (ody&
4uestion 1& ,all 2003
<se accumulative recursion to )rite a %unction count-sublists t"at consumes a list 2 )"ic" can
contain an$ Bind o% elements 3 and 'roduces a num0er indicating t"e num0er o% non5em't$
su0lists in t"e given list! .% t"e list is em't$, t"en return @ero! ;s an e&am'le, 2 count-sublists
?221 23 2 3 2 3 3 3 3 # 2 5 3 3 3 )ould 'roduce 3!
4uestion 2& Winter 2003
<se accumulative recursion to )rite a %unction add-digits t"at consumes a natural num0er n
and 'roduces t"e sum o% t"e digits o% t"at num0er! Aour solution must run in time )"ic" is
linear in t"e num0er o% digits o% n!
Contract?
>> add5digits: nat G nat
E1am'les?
>> 2 add5digits #2K 3 G 13
>> 2 add5digits 12 3 G 3
>> 2 add5digits ( 3 G (
4uestion "& Winter 2003
<se accumulative recursion to )rite a %unction smallest-sum )"ic" consumes a list o% natural
num0ers 2 )"ere t"ere are at least 2 num0ers in t"e list 3 and 'roduces t"e smallest 'air5)ise
sum! M$ ,'air5)ise- sum, )e mean t"e sum o% t)o adPacent elements in t"e list! ;s an e&am'le,
consider t"e list 2 list 1 # 2 3 L 3 : t"ere are # 'air5)ise sums! 2 1 N # E 5 , # N 2 E 6 , 2 N 3 E 5
and 3 N L E 11 3! .n t"is case, t"e smallest 'air5)ise sum )ould 0e 5 2 )"ic" "a''ens to occur
t)ice 3!
Contract?
>> smallest5sum 2 list o% nat 3 C lengt" DE 2 F G nat
E1am'les?
>> 2 smallest5sum ?21 2 3 # 5 3 3 G 3
>> 2 smallest5sum ?21 # 2 5 1 L 1 3 G 5
>> 2 smallest5sum ?2 1(( 1(( 3 3 G 2((
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion #& S'ring 2003
<se accumulative recursion to )rite a %unction 2nd5largest t"at consumes a
list o% lengt" at least 2 containing distinct num0ers and 'roduces t"e second largest num0er in
t"e list!
4uestion & S'ring 2003
<se accumulative recursion to )rite a %unction largest5difference )"ic"
consumes a list o% natural num0ers 2)"ere t"ere are at least 2 num0ers in t"e list3 and 'roduces
t"e largest 'air5)ise di%%erence! M$ ,'air5)ise- di%%erence, )e mean t"e a0solute di%%erence
0et)een t)o adPacent elements in t"e list! ;s an e&am'le, consider t"e list 2list 1 # 2 3 L3: t"ere
are # 'air5)ise di%%erences 2S1 T #S E 3, S# T 2S E 2, S2 T 3S E 1 and S3 T LS E 53! .n t"is case, t"e
largest 'air5)ise di%%erence )ould 0e 5! 8ote t"at 2a(s 5#3 3 # and 2a(s 23 3 2!
4uestion 6& ,all 2010
<sing accumulative recursion, )rite t"e %unction matc0ing5indices t"at consumes a natural
num0er n and list o% natural num0ers lst! T"e %unction 'roduces a list o% natural num0ers
corres'onding to t"e indices )"ere n a''ears in lst!
T"e values in t"e 'roduces list must 0e in increasing order
T"e %irst item in a list "as inde& (
Aou ma$ use reverse %or t"is *uestion
E&am'le:
2 matc"ing5indices 3 2 list # 2 1 3 3 G em't$
2 matc"ing5indices 1( 2 list 2( 1( 3( 3 3 G 2 list 1 3
2 matc"ing5indices K 2 list K 2 L K K 3 3 G 2 list ( 3 # 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part @& Efficiency= ,or eac0 of t0e follo.ing Sc0eme functions identify t0e
.orst)case running time as one of
/inear 2 denoted as 92n3 3
*uadratic 2 denoted as 92 n
2
3 3 or
e&'onential 2 denoted as 92 2
n
3 3
2a3 C"oose t"e )orst5case running time o% %unct5;!
2 de%ine 2 %unct5; m$list 3
2 cond C 2 em'ty; 2 rest m$list 3 3 2 first m$list 3 F
C else 2 N 2 first m$list 3
2 %unct5; 2 rest m$list 3 3 3 F 3 3
203 C"oose t"e )orst5case running o% %unct5M!
2 define 2 %unct5M m$list 3
2 cond C 2 em'ty; 2 rest m$list 3 3 em'ty F
C else 2 a''end 2 %unct5M 2 rest m$list 3 3
2 list 2 first m$list 3 3 3 F 3 3
2c3 C"oose t"e )orst5case running o% %unct5C!
2 define 2 %unct5C m$list 3
2 cond C 2 em'ty; 2 rest m$list 3 3 2 first m$list 3 F
C 2 I 2 first m$list 3 2 %unct5C 2 rest m$list 3 3 3
2 first m$list 3 F
C else 2 %unct5C 2 rest m$list 3 3 F 3 3
2d3 C"oose t"e )orst5case running time o% %unct5;!
2 define 2 %unct5; m$list 3
2 local 2 define 2 "el'er al 3
2 cond C 2 em'ty; ;l 3 ( F
C else 2 N 2 first al 3 2 "el'er 2 rest al 3 3 F 3 3 3 3
2 cond C 2 em'ty; m$list 3 em'ty F
C else 2 cons 2 "el'er m$list 3
2 %unct5; 2 rest m$list 3 3 3 3 3 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
2e3 C"oose t"e )orst5case running o% %unct5M!
2 define 2 %unct5M m$list 3
2 cond C 2 em'ty; 2 rest m$list 3 3 em'ty F
C else 2 cons 2 first m$list 3 2 %unct5M 2 rest m$list 3 3 F 3 3 3 3
2%3 C"oose t"e )orst5case running o% %unct5C!
2 define 2 %unct5C m$list 3
2 cond C 2 em'ty; 2 rest m$list 3 3 2 first m$list 3 F
C 2 I 2 first m$list 3 2 %unct5C 2 rest m$list 3 3 3
2 first m$list 3 F
C else 2 %unct5C 2 rest m$list 3 3 F 3 3
2g3 Circle t"e )orst5case running time o% %unct5;!
2define 2%unct5; m$list3
2cond C2em'tyH m$list3 em'tyF
Celse 2cons 2rest m$list3
2%unct5; 2rest m$list333F33
2"3 Circle t"e )orst5case running time o% %unct5M!
2define 2%unct5M m$list3
2local C2define 2"el'er al3
2cond C2em'tyH al3 (F
Celse 2N 2"el'er 2rest al33 2first al33F33F
2cond C2em'tyH m$list3 em'tyF
Celse 2cons 2"el'er m$list3
2%unct5M 2rest m$list333F333
2i3 Circle t"e )orst5case running time o% %unct5C!
2define 2%unct5C m$list3
2cond C2em'tyH 2rest m$list33 2first m$list3F
C2D 2first m$list3 2%unct5C 2rest m$list333
2first m$list3F
C2E 2first m$list3 2%unct5C 2rest m$list333
2first m$list3F
Celse 2%unct5C 2rest m$list33F33
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
2P3 2 de%ine 2 all5sameH nums 3
2 cond
C 2 em't$H nums3 true F
C 2 em't$H 2 rest nums 3 3 true F
C else 2 and 2 E 2 %irst nums 3 2 second nums 3 3
2 all5sameH 2 rest nums 3 3 3 F 3 3
2B3 2 de%ine 2 % n 3
2 cond
C 2 E n ( 3 5 F
C else 2 N 1 2 % 2 su01 n 3 3 2 % 2 su01 n 3 3 3 F 3 3
2l3 2 de%ine 2 sill$ nums 3
2 not 2 E 2 %irst nums 3 2 %i%t" nums 3 3 3 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part A& Mutation= 'rovide at least one test t0at differ from t0e e1am'le&
4uestion 1& ,all 2003
Su''ose $ou "ave t"e %ollo)ing state varia0les de%ined:
2 define m 3 3
2 define n 6 3
+rite a Sc"eme %unction my-mutate t"at consumes t)o %unctions %1 and %2! 11
consumes one 'arameter and 'roduces a num0er> %2 consumes t)o 'arameters o% t"e
same t$'e and 'roduces t"at same t$'e! .% t"e result o% a''l$ing %1 to m is larger t"an t"e
result o% a''l$ing %1 to n, t"en t"e %unction m$5mutate s"ould mutate m to t"e result o%
a''l$ing %2 to m and n 2 t"at is, m )ill 0e t"e %irst 'arameter o% %2, and n )ill 0e t"e
second 'arameter o% %2 3>
ot"er)ise, t"e %unction mutates n to t"e result o% a''l$ing %2 to n and m2 t"at is, n )ill
0e t"e %irst 'arameter o% %2, and m )ill 0e t"e second 'arameter o% %2 3!
;s an e&am'le, su''ose m is 3 and n is 6! %1 is t"e Sc"eme 0uild5in s*r %unction and %2
is t"e N %unction, since 2 s*r 3 3 I 2 s*r 6 3, t"e m$5mutate %unction )ill mutate n to 2 N 6
3 3, t"ere%ore, t"e value o% n 0ecomes 6 a%ter a''l$ing t"e %unction!
4uestion 2& Winter 2003
2 define & 1 3
2 define $ 1 3
+rite a Sc"eme %unction funct-mutation t"at consumes t)o %unctions %1 and %2, eac" o%
)"ic" consume t)o elements 2 0ot" o% t"e same t$'e 3 and 'roduce one element 2 )"ic"
"as t"e same t$'e as t"e consumed t$'e 3! T"e %unction funct-mutation s"ould alter & to
t"e result o% a''l$ing %1 to & and $! and it s"ould alter $ to t"e result o% a''l$ing %2 to &
and $!
E1am'les?
>> i% & "as t"e value 1 and $ "as t"e value o% 1, and )e evaluate
>> 2 %unct5mutation N *uotient 3 , t"en & "as t"e value 2 and
>> $ "as t"e value 1!
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion "& S'ring 2003
Su''ose $ou "ave t"e state varia0les & and $ de%ined, and t"at t"e$ are t"e same
t$'e! +rite a Sc"eme %unction funct-mutation t"at consumes t)o %unctions %1 and %2, eac" o%
)"ic" consume one element and 'roduce a num0er! T"e %unction funct-mutation s"ould mutate
& to t"e ma&imum o% a''l$ing %1 to & and a''l$ing %2 to $, and mutate $ to t"e sum o% a''l$ing
%2 to & and %1 to $!
4uestion #& ,all 2010
+rite t"e %unction s)a'5suit t"at consumes t)o cards c1 and c2 and 'roduces 2void3! T"e
%unction mutates t"e suit o% t"e t)o cards suc" t"at c1 "as t"e suit o% c2, and c2 "as t"e suit o%
c1!
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part 3& $racing
4uestion 1& ,all 2003
Consider t"e code:
>> m$ster$: natC DE 1 F G 2 listo% nat 3
2 define 2 m$ster$ n 3
2 local 2 2 define 2 acc5"el'er ne&t remain lst 3
2 cond
C 2 E remain 1 3 lst F
C 2 D ne&t remain 3 2 cons remain lst 3 F
C 2 BeroH 2 remainder remain ne&t 3 3
2 acc5"el'er ne&t
2 /uotient remain ne&t 3
2 cons ne&t lst 3 3 F
C else 2 acc5"el'er 2 add1 ne&t 3 remain lst 3 F 3 3 3
2 acc5"el'er 2 n em't$ 3 3 3
%or eac" o% t"e %ollo)ing test cases, give t"e out'ut o% m$ster$
2a3 2 m$ster$ 1 3 203 2 m$ster$ 2 3 2c3 2 m$ster$ 12 3 2d3 2 m$ster$ 26 3
4uestion 2& Winter 2003
>> m$ster$: 2 list o% nat2 DE 1 3 3 G nat
2 de%ine 2 m$ster$ alo'n 3
2 local
>> stri'5B: nat 2 list o% nat 2 DE1 3 3 G 2 list o% nat 2 DE1 3 3
>> Pur'ose: stri'5B removes t"e %irst B elements %rom t"e 'rovided list> i% t"ere are
>> B or %e)er elements in t"e list, t"e em't$ list is returned
>> E&am'les: 2 stri'5B ( 2 list 1 2 3 3 3 G 2 list 1 2 3 3
>> 2 stri'5B 2 2 list 1 2 3 3 3 G 2 list 3 3
2 de%ine 2 stri'5B B lst 3
2 cond 2 2 @eroH U 3 lst 3
2 2 em't$H /st 3 em't$ 3
2 else 2 stri'5B 2 su01 B 3 2 rest lst 3 3 3 3 3 3
2 cond 2 2 em't$H ;lo'n 3 ( 3
2 else 2 add1 2 m$ster$ 2 stri'5B 2 %irst alo'n 3 alo'n 3 3 3 3 3 3
%or eac" o% t"e %ollo)ing test cases, give t"e out'ut o% m$ster$
2a3 2 m$ster$ em't$ 3 203 2 m$ster$ 2 list 1 1( 6 L 3 3 2c3 2 m$ster$ 2 list 1 2 1( 3 1 1 1 3 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion "& S'ring 2003
>> m$ster$: natCD(F Q 2listo% nat3Clengt"E2F
2define 2m$ster$ n3
2local
C2define 2m$ster$5"el' num acc3
2cond
C2evenH num3 2m$ster$5"el' 2/ num 23 2add1 acc33F
Celse 2list num acc3F33F
2m$ster$5"el' n (333
1or eac" o% t"e %ollo)ing test cases, give t"e out'ut o% m$ster$!
2a3 2m$ster$ 63 3
203 2m$ster$ 1(3 3
2c3 2m$ster$ 363 3
2d3 2m$ster$ #L(3 3
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part 10& Casic Pyt0on
4uestion 1& ,all 2010
assume 0ase on %ollo)ing code:
a E 11
0 E 6
c E 6!2
/1 E Ca,0F
/2 E Ca,0,c,c,0,aF
s1 E ,ta00$-
Pyt0on statement Out'ut
'rint 2aN03,c
'rint int2c3
'rint a/0
'rint %loat2a/03
'rint aV0
'rint t$'e2a3 EE t$'e2c3
'rint len2/13
'rint /2C2:5F
'rint s1C1F
'rint s1C1:F
'rint 2aDE/1C1F3
'rint range23,K3
'rint range2#,51,513
'rint 3*s1
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part 11& Casic Pyt0on 'rogramming&
4uestion 1& ,all 2010
+rite t"e '$t"on %unction division )"ic" consumes t)o 'ositive integers numerator and
denominator 2 )it" numerator DE denominator 3 and 'rints out in%ormation a0out t"e
*uotient and remainder o% t"ese t)o num0ers! 1or e&am'le, division21(,33 s"ould 'rint
out
10/ is !ith remainder 1
and division212,63 s"ould 'rint out:
12/" is 2 !ith remainder 0
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part 12& Ot0er /uestions& Provide contract= e1am'le and test and function
(ody for eac0 of t0e follo.ing /uestions&
4uestion 1& S'ring 2003
+rite a %unction list5list5'icB )"ic" consumes a list o% list o% num0ers and a natural
num0er n 1 and 'roduces t"e nt" num0er in t"e list o% lists 2counting eac" num0er in
eac" list3, or %alse i% t"ere arenWt enoug" num0ers in t"e list o% lists!
4uestion 2& Winter 2011
; )ord is string o% onl$ lo)ercase letter!
; 'attern is a string o% onl$ lo)ercase letters and t"e letter ,R-!
; 'attern matc"es t"e )ord o0tained 0$ re'lacing ever$ instance o% ,R- in t"e 'attern
)it" 3 co'ies o% t"e letter ,&-! 1or e&am'le, t"e 'attern ,aRc- matc"es t"e )ord
,a&&&c-, t"e 'attern ,a0cR&R*- matc"es t"e )ord ,a0c&&&&&&&*-, t"e 'attern ,-
matc"es t"e )ord ,-, and t"e 'attern ,a0c&$@- matc"es t"e )ord ,a0c&$@-! Jo)ever,
t"e 'attern ,aRc- does not matc" t"e )ord ,a&c- and t"e 'attern ,aR- does not eac" t"e
)ord ,a&&&0cd-!
Com'lete t"e %unction matc"esH T"at consumes a )ord ) and a 'attern ' and 'roduces
true i% ' matc"es and %alse ot"er)ise!
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
Part 1"& Su'er du'er C0allenge 4uestions from Winter 2010 ta5e 0ome
midterm&
4uestion 1& Create a Sc"eme %unction vending5mac0ine t"at simulates t"e actions o% a vending
mac"ine! T"e 'rogram contains t"ree state varia0les: coin50o& 2re'resenting t"e total amount o%
mone$ inserted into t"e vending mac"ine3, num5c"i's 2re'resenting t"e num0er o% 0ags o% c"i's
in t"e mac"ine3 and num50ars 2re'resenting t"e num0er o% c"ocolate 0ars in t"e mac"ine3! T"e
'rogram also contains constants t"at indicate t"e 'rice o% c"i's and c"ocolate 0ars! T"e %unction
vending5mac"ine )ill consume a num0er e*ual to t"e amount o% a coin 2one o% (!(5, (!1(, (!25,
1!((, 2!((3 or a s$m0ol indicating a 'urc"ase 2one o% ?c"i's or ?0ar3! T"e %unction )ill 'roduce
t"e %ollo)ing:
.% t"e value entered is a num0er, not"ing is 'roduced, 0ut t"e coin50o& is u'dated
accordingl$!
.% t"e value entered is a s$m0ol re'resenting a %ood item )it" an inventor$ o% (, t"e
%unction 'roduces t"e string XSold outX regardless o% "o) muc" mone$ is in t"e coin5
0o&! 2T"e coin50o& )ill 0e una%%ected!3
.% t"e value entered is a s$m0ol and t"e coin50o& contains enoug" mone$ %or t"e
'urc"ase matc"ing t"e s$m0ol, t"e %unction )ill 'roduce t"e c"ange o)ed %or t"e
'urc"ase, t"e coin50o& s"ould 0e reset to ( and t"e a''ro'riate %ood state varia0le is
reduced 0$ 1!
.% t"e value entered is a s$m0ol and t"e coin50o& does not contain enoug" mone$, t"e
%unction )ill 'roduce t"e string X.nsu%%icient %undsX ! 2T"e coin50o& )ill 0e una%%ected
eit"er )a$!3
1or e&am'le, su''ose t"e %ollo)ing se*uence o% %unction calls occurred:
2setQ num50ars 53
2setQ coin50o& (3
2vending5mac"ine 2!((3
2vending5mac"ine ?0ar3
T"en t"e %inal %unction call )ould 'roduce (!5, value o% coin50o& )ould 0e (, and t"e
value o% num50ars )ould 0e #!
Aou ma$ assume t"at all in'ut to t"e %unction )ill 0e valid 2t"at is, all t"e num0ers )ill
0e greater t"an (, and an$ s$m0ol )ill 0e eit"er ?c"i's or ?0ar!3
Com'lete your solution .it0out adding any e1tra state varia(les& 2ou may use local
varia(les in t0e (ody of your function&
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion 2& T"is *uestion uses t"e structures at"lete and )inners, de%ined 0elo):
'define-struct athlete 'name countr- placement++
>> ;n at0lete is a structure 'make-athlete n c p+ )"ere
>> n is a string 2at"lete?s name3, c is a string 2countr$ t"e at"lete re'resents3
>> p is a natural num0er 2'lace t"e at"lete %inis"ed in t"e com'etition3 or
>> a s$m0ol 2eg! ?D81 indicating t"e at"lete did not %inis" t"e com'etition or
>> ?+D indicating t"e at"lete )it"dre) %rom t"e com'etition 0e%ore it started3
2Dote t0is is a slig0tly different definition t0an t0e one used on midterm one9
'define-struct winners 'description gold silver bron/e++
>> ; .inners is a structure 'make-winners d g s b+, )"ere
>> d is a string 2descri'tion or name o% t"e at"letic com'etition3
>> g"s"b are at"letes 2g, %or gold, is t"e at"lete )"o 'laced %irst,
>> s, %or silver, is t"e at"lete )"o 'laced second, and b, %or 0ron@e,
>> is t"e at"lete )"o 'laced t"ird in t"e com'etition3!
Create a Sc"eme %unction called remove5c0eater t"at consumes a string 2re'resenting t"e name
o% a c"eater3, a )inners structure 2re'resenting t"e medalists in t"e event3, and an at"lete
2re'resenting t"e %ourt" 'lace %inis"er in t"e event in )"ic" t"e )inners com'eted!3 .% t"e name
o% t"e c"eater is one o% t"e medalists, t"e 'lacement o% t"at at"lete )ill 0e c"anged to ?DY, and
ever$one else )ill move u' one 'lace in t"e standings! 1or e&am'le, i% t"e gold medalist is t"e
c"eater, t"en t"e 'lacement o% t"e %ormer gold medalist c"anges to ?DY, t"e silver medalist
moves to t"e gold 'osition 2and t"eir 'lacement c"anges to 13, t"e 0ron@e medalist moves to t"e
silver 'osition 2and t"eir 'lacement c"anges to 23, and t"e %ourt" 'lace %inis"er moves to t"e
0ron@e 'osition 2and t"eir 'lacement c"anges to 33! T"e gold, silver, and 0ron@e %ields o% t"e
)inners structure s"ould also 0e u'dated a''ro'riatel$!
.% t"e name o% t"e c"eater is neit"er a medalist nor t"e %ourt" 'lace %inis"er, not"ing )ill c"ange!
.n t"is case t"e %unction s"ould 'roduce t"e string X8o c"angeX! .n all ot"er cases t"e %unction
s"ould 'roduce 2void3!
Aou ma$ not use t"e maBe5)inners or maBe5at"lete %unctions in t"e 0od$ o% t"e remove5c"eater
%unction 2or an$ "el'er %unctions $ou )rite3! Jo)ever t"ese ma$ 0e used )it"in $our test cases!
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion "& 1or t"is *uestion, $ou )ill )rite a Sc"eme %unction "as5su0stringH! T"is %unction
consumes t)o strings, te&t and 'attern, and )ill 'roduce true i% 'attern is a su0string o% te&t and
%alse ot"er)ise!
+e can immediatel$ determine t"at 'attern is a su0string o% te&t i% t"e t)o strings are e*ual, and
)e can immediatel$ determine t"at 'attern is not a su0string o% te&t i%:
te&t is s"orter t"an 'attern, or
te&t is t"e same lengt" as 'attern 0ut t"e t)o strings are not e*ual!
9t"er)ise )e consider t"e %ollo)ing cases! /et le%t 0e t"e %irst "al% o% te&t and let rig"t 0e t"e
second "al% o% te&t 2i% t"e lengt" o% te&t is odd, t"en maBe le%t one c"aracter s"orter t"an rig"t3!
+e can determine t"at 'attern is a su0string o% te&t i%:
'attern is a su0string o% le%t, or
'attern is a su0string o% rig"t, or
'attern is a su0string o% te&t suc" t"at te&t "as its %irst c"aracter removed, or
'attern is a su0string o% te&t suc" t"at te&t "as its last c"aracter removed!
8ote t"at t"e last t)o cases are necessar$ 0ecause it is 'ossi0le %or 'attern to 0e a su0string o%
te&t 0ut 0e a su0string o% neit"er le%t nor rig"t 2%or e&am'le, )"en te&t is ,tele'"ones- and
'attern is ,le'"-3!
Aour solution must use generative recursion to im'lement t"e algorit"m e1actly as it is
descri(ed in t0is /uestion, even i% $ou can t"inB o% an alternate )a$ to solve t"e 'ro0lem!
E1am'les?
2"as5su0stringH ,a0c- ,a0c-3 ED true
2"as5su0stringH ,tele'"ones- ,le'"-3 ED true
2"as5su0stringH ,*uicB- ,&$-3 ED %alse
2"as5su0stringH ,(11(((1(11(1- ,111-3 ED %alse
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion #& Consider t"e %our %unctions reverse5searc", sum5o%5divisors, gro)5du'licates, and
mystery)sort! 1or each o% t"ese %unctions, state:
T"e (est case running time o% t"e %unction along )it" a 0rie% Pusti%ication, and
T"e .orst case running time o% t"e %unction along )it" a 0rie% Pusti%ication!
.% t"e 0est case running time and t"e )orst case running time %or a %unction are different, $ou
must also 'rovide:
;n in'ut value on )"ic" t"e %unction ac"ieves its 0est case running time, and
;n in'ut value on )"ic" t"e %unction ac"ieves its )orst case running time!
+"en giving t"e 0est case running time o% a %unction, $ou are not allo)ed to select t"e si@e o%
t"e in'ut! S'eci%icall$, do not maBe statements suc" as ,t"e 0est case is )"en t"e list is em't$-
or ,t"e 0est case is )"en t"e list "as lengt" one!- T"e si@e o% t"e in'ut, n, is al)a$s considered
to 0e an ar0itrar$ value!
T"e contract %or eac" %unction 2and local "el'er %unction3 a''ears in 0old! +e "ave omitted t"e
'ur'ose, e&am'les, and test cases %or all %unctions> t"is is to encourage $ou to t"oroug"l$ read
and trace t"e code itsel% to understand e&actl$ "o) t"e %unction )orBs! T"e a0ilit$ to trace a
%unction on a variet$ o% in'ut values is o% great 0ene%it )"en determining its 0est case and )orst
case running times! Aou ma$ maBe t"e %ollo)ing assum'tions %or t"is *uestion:
;ll o% t"e running times )ill 0e one o% t"e %ollo)ing:
Constant 2also e&'ressed as 92133
/inear 2also e&'ressed as 92n33
Yuadratic 2also e&'ressed as 92n233
E&'onential 2also e&'ressed as 922n33
T"e %unction reverse "as linear running time
E reverse)searc0? 6listof any9 any F (oolean
2de%ine 2reverse5searc" lst target3
2cond
C2em't$H lst3 %alseF
C2e*ualH 2%irst lst3 target3 trueF
Celse 2reverse5searc" 2reverse 2rest lst33 target3F33
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
E sum)of)divisors? nat F nat
2de%ine 2sum5o%5divisors n3
2%oldr N (
2%ilter 2lam0da 2$3 2@eroH 2remainder n $333
20uild5list n 2lam0da 2&3 2add1 &333333
E gro.)du'licates? 6listof G9 F 6listof G9
2de%ine 2gro)5du'licates lst3
2cond
C2em't$H lst3 em't$F
C2E 1 2lengt" lst33 em't$F
C2em't$H 2%ilter 2lam0da 2&3 2e*ualH & 2%irst lst333 2rest lst333
2gro)5du'licates 2rest lst33F
Celse 2a''end 2gro)5du'licates 2rest lst33
2list 2%irst lst33 2gro)5du'licates 2rest lst333F33
E mystery)sort? 6listof G9Hnonem'tyI F 6listof G9Hnonem'tyI
E $0e elements of ty'e G must (e com'ara(le .it0 J and K
2de%ine 2m$ster$5sort lst3
2local
E 'artition? 6listof G9 6listof 6listof G99 6listof G9Hnonem'tyI
E F 6listof 6listof G99Hnonem'tyI
E $0e elements of ty'e G must (e com'ara(le .it0 J and K
C2de%ine 2'artition lst( list5o%5lists ne&t5list3
2cond
C2em't$H lst(3
2reverse 2cons 2reverse ne&t5list3 list5o%5lists33F
C2D 2%irst lst(3 2%irst ne&t5list33
2'artition 2rest lst(3 list5o%5lists
2cons 2%irst lst(3 ne&t5list33F
Celse 2'artition 2rest lst(3
2cons 2reverse ne&t5list3 list5o%5lists3
2list 2%irst lst(333F33
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
E merge? 6listof G9 6listof G9 F 6listof G9
E $0e elements of ty'e G must (e com'ara(le .it0 J and K
2de%ine 2merge lst1 lst23
2cond
C2em't$H lst13 lst2F
C2em't$H lst23 lst1F
C2IE 2%irst lst13 2%irst lst233
2cons 2%irst lst13 2merge 2rest lst13 lst233F
Celse 2cons 2%irst lst23 2merge lst1 2rest lst2333F33F
2%oldr merge em't$ 2'artition 2rest lst3 em't$ 2list 2%irst lst333333
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion & Create a P$t"on %unction identi%$Ztriangle t"at 'rom'ts t"e user to enter t"ree
'ositive integer values in ascending order: a, 0, and c! T"ese values re'resent t"e lengt"s o% t"e
sides o% a triangle! .% t"e lengt" o% t"e longest side is greater t"an or e*ual to t"e sum o% t"e
lengt"s o% t"e t)o smaller sides, t"e triangle is considered invalid! ; valid triangle can 0e
categori@ed as one o% t"e %ollo)ing 0ased on t"e lengt"s o% its sides:
an e/uilateral triangle "as t"ree e*ual sides
an isosceles triangle "as t)o e*ual sides
a scalene triangle "as no e*ual sides
; triangle can also 0e categori@ed as one o% t"e %ollo)ing relating to t"e interior angles:
a rig0t)angled triangle "as sides )"ere c2 E a2 N 02 )"ere a, 0, and c are t"e lengt"s o%
t"e sides o% t"e triangle
an acute triangle "as sides )"ere c2 I a2 N 02 )"ere a, 0, and c are t"e lengt"s o% t"e
sides o% t"e triangle
an o(tuse triangle "as sides )"ere c2 D a2 N 02 )"ere a, 0, and c are t"e lengt"s o% t"e
sides o% t"e triangle
Aour %unction s"ould 'rint invalid i% t"e triangle is invalid! 9t"er)ise, it s"ould 'rint t"e
categor$ o% t"e triangle 0ased on its side lengt"s 2e*uilateral, isosceles, or scalene3 %ollo)ed 0$
t"e categor$ o% t"e triangle 0ased on its interior angles 2rig"t5angled, acute, or o0tuse3!
Aour solution s"ould use t"e 'rom'ts 'rovided in t"e starter %ile! ;lso, )"enever $ou are
)orBing )it" strings in $our 'rograms, it is ver$ im'ortant to 0e 'recise! ;ll strings t"at are
'rinted s"ould 0e lo)er case and )it"out an$ e&tra )"ites'ace! .% t"ere is more t"an one string
0eing 'rinted, eac" string s"ould a''ear on its o)n line! Do not 'rint out an$ e&tra 0lanB lines!
T"ree sam'le runs o% t"e 'rogram are s"o)n 0elo)! 8ote t"e use o% 0old to indicate t"e values
entered 0$ t"e user )"ile t"e 'rogram is e&ecuting! Aou ma$ assume t"at t"e user )ill al)a$s
enter 'ositive integer values!
"="=10 F invalid
==A F isosceles=o(tuse
"=#= F scalene= rig0t)angled
89T 19: C977E:.C;/ <SE, tutor
CS115/116/2**/3** Private Tutor Service, Please contact Eagle Diao, Email: eaglediaogmail!com,
more t"an # $ears o% e&'erience and around #(( students in t"e 'ast!
4uestion 6& >ecimal num(ers= or (ase 10 num(ers= are .ritten using 10 'ossi(le digits&
Dum(ers can
0e re'resented )it" an$ 0ase! 1or e&am'le 0inar$ num0ers, or 0ase 2 num0ers, are )ritten
using Pust t)o digits: ( and 1! T"e 0inar$ num0er 11(1 is e*uivalent to t"e decimal num0er 13!
T"e num0er 2(( in 0ase 5 is 13((! Create a P$t"on %unction called convertZ0ase t"at )ill
consume a non5negative integer and an integer 0et)een 2 and 6 inclusive, and 'roduce a string
re'resenting t"e num0er in t"e ne) 0ase! 8ote t"at ( is t"e same num0er in an$ 0ase!
To convert a num0er to a di%%erent 0ase, $ou need to continuousl$ divide 0$ t"e 0ase until $ou
reac" ( and record t"e remainder eac" time! T"en )rite t"e remainders in reverse order!
2our solution must use recursionE it may not use loo's&
E1am'le 1? Converting 13 to 0ase 2
13/2 E 6 remainder 1
6/2 E 3 remainder (
3/2 E 1 remainder 1
1/2 E ( remainder 1
/ooBing at t"e remainders in reverse, t"is 'roduces 11(1 in 0ase 2, and t"is is t"e
e*uivalent o% 13 in 0ase 1(!
E1am'le 2? Converting 5(( to 0ase K
5((/K E K1 remainder 3
K1/K E 1( remainder 1
1(/K E 1 remainder 3
1/K E ( remainder 1
/ooBing at t"e remainders in reverse, t"is 'roduces 1313 in 0ase K, and t"is is t"e
e*uivalent o% 5(( in 0ase 1(!
89T 19: C977E:.C;/ <SE, tutor

You might also like