CS 135 Winter 2016 Tutorial 6: Midterm Review

advertisement
CS 135 Winter 2016
Tutorial 6: Midterm Review
CS 135 Winter 2016
Tutorial 6: Midterm Review
1
th
Reminder: Monday February 29
• The midterm will be held on Monday February 29th at 7:00PM.
• Seating for the midterm will be posted on the course webpage
on Friday morning.
• The midterm will cover up to and including module 7 “Types of
Recursion”.
• There will be NO assignment due Tuesday March 1st .
CS 135 Winter 2016
Tutorial 6: Midterm Review
2
Midterm Practice
• Redo assignments
• Complete tutorial problems
• Complete the warmup and practise problems listed on assignments
• Make up practice questions for yourself
• Reread the course notes
• Every question in this tutorial was inspired by a point in a module
summary
CS 135 Winter 2016
Tutorial 6: Midterm Review
3
Group Problem - Module 2: Booleans
Rewrite the following expression without using cond:
(cond
[(p1? x) (not (p2? x))]
[else (p2? x)])
CS 135 Winter 2016
Tutorial 6: Midterm Review
4
Clicker Question - Module 2: Design Recipe
Consider a function dispatch for dispatching ambulances. It tracks the
location of a number of ambulances. When it receives a call, it will find the
locations of all ambulances within a specified distance away from the call.
You may assume the location of ambulances and the call are specified as
posns. Which of the following would be a correct contract for dispatch?
A ;; dispatch: (List Posn) Posn Num → (List Posn)
B ;; dispatch: (listof Posn) Posn Int → (listof Posn)
C ;; dispatch: (listof Posns) Posn Num → (listof Posns)
D ;; dispatch: (listof Posn) Posn Num → (listof Posn)
E ;; dispatch: (listof posn) posn num → (listof posn)
CS 135 Winter 2016
Tutorial 6: Midterm Review
5
Group Problem - Module 3: Steppers
Consider the definition of a function increment,
(define (increment start-num oper)
(cond
[(symbol=? oper ’add) (+ start-num 1)]
[(symbol=? oper ’sub) (− start-num 1)]))
Step through the following:
(increment 6 ’sub)
CS 135 Winter 2016
Tutorial 6: Midterm Review
6
Group Problem - Module 4: Structures
Write the structure and data definitions for an Automobile structure. An
Automobile has colour represented by a Symbol, and a year and price each
represented by Natural numbers.
Also include a template, my-listof-automobile-fn, for a function consuming a
list of Automobile structures.
CS 135 Winter 2016
Tutorial 6: Midterm Review
7
Group Problem - Module 5: Lists
Using the my-listof-automobile-fn template, define a function
impulse-buy-auto that consumes a list of Automobiles, a Natural number
representing a budget, and a Symbol representing a desired colour. The
function will output the first Automobile in the list that costs less than the
budget, and is the desired colour. If no Automobiles meet the requirements,
impulse-buy-auto produces false. Include a contract for the function.
(check-expect (impulse-buy-auto(list (make-automobile ’blue 1999 2500)
(make-automobile ’black 1998 2000)
(make-automobile ’blue 1965 1500))
2200 ’blue)
(make-automobile ’blue 1965 1500))
CS 135 Winter 2016
Tutorial 6: Midterm Review
8
Clicker Question - List Translation
Given this list:
(list (list) ’cons (list (list 2 ’green) 3))
Which of the following is equivalent to the given list?
A ’(empty cons (list 2 ’green) 3)
B ’(empty cons (2 ’green) 3)
C ’(empty cons (list (list 2 ’green) 3))
D ’(() cons ((2 green) 3))
E ’(() ’cons ((2 ’green) 3))
CS 135 Winter 2016
Tutorial 6: Midterm Review
9
Group Problem - anagram?
An anagram is a rearrangement (permutation) of the letters of a word. For
example, “ate” is an anagram of “tea”, and “foster” is an anagram of “forest”.
Write a predicate function called anagram? that determines if two strings
are anagrams of each other, producing true if they are. You may assume
that the input strings contain only lower-case letters (i.e., no spaces,
punctuation, or upper-case letters).
CS 135 Winter 2016
Tutorial 6: Midterm Review
10
Group Problem - Module 6: More Recursion
You will write a predicate called sub-sequence?. The function takes 2 lists of
Symbols as parameters and identifies if the first list has all the elements in
the second list, in the same order but not necessarily contiguously. The
design recipe is included below.
;; (sub-sequence? loSym subsequence) determines if loSym contains the sequence subsequence.
;; sub-sequence?: (listof Sym) (listof Sym) → Bool
;; Examples:
(check-expect (sub-sequence? ’(I knew you were trouble when you walked in trouble trouble trouble)
’(you were in trouble)) true)
(check-expect (sub-sequence? ’(a b c d) ’(b a)) false)
;; Tests:
(check-expect (sub-sequence? ’(red) ’()) true)
(check-expect (sub-sequence? ’(a b c d e) ’(a c e)) true)
(check-expect (sub-sequence? ’(a b c d e) ’(a e b)) false)
CS 135 Winter 2016
Tutorial 6: Midterm Review
11
Download