Quiz 3 Solutions
Solutions: You can find the file with solutions for all questions here.
Quiz submissions were graded automatically for correctness. Implementations did not need to be efficient, as long as they were correct.
In addition to the doctests provided to students, we also used extra doctests to check for corner cases. These extra test cases are highlighted below.
Warm-Up Question (Optional; 0 points)
Implement all
, which takes a predicate procedure f
and a list s
. It
returns whether (f x)
returns true for all elements x
in s
. Hint: The
filter
procedure from lab has a similar structure.
(define (all f s)
(cond ((null? s) #t)
((f (car s)) (all f (cdr s)))
(else #f))
)
Use OK to unlock and test your code:
python3 ok -q all -u
python3 ok -q all
Question 1
Implement every
, which takes a two-argument procedure g
and a list s
. It
returns a new list of every element x
in s
for which (g x y)
returns
True
for every y
in s
(including itself). Hint: You can use the
filter
procedure that you defined in Lab. You can also use the all
procedure from Question 0.
(define (every g s)
(filter (lambda (x) (all (lambda (y) (g x y)) s)) s)
)
Use OK to unlock and test your code:
python3 ok -q every -u
python3 ok -q every
Cucumber
Cucumber is a card game. In our implementation, cards are positive integers (i.e. there are no suits), and there may be more than one of the same card in play.
Play proceeds in a series of rounds, called tricks. In a trick, each player, in turn, plays a card. On your turn, you must either take control by playing a card as high or higher than the highest card played so far or play the lowest card in your hand. Whoever last took control wins the trick and can start the next trick with any card.
The goal of the game is not to win the last trick.
Question 2
One Cucumber strategy is the fimping strategy. On our turn, we play the lowest card in our hand that can take control; if we have no such card, we play the lowest card in our hand.Implement fimp
, which takes a non-empty list of cards (in any order) called
hand
and the highest
card played so far. The fimp
procedure returns the
card we should play under the fimping
strategy.
(define (fimp hand highest)
(define higher (filter (lambda (x) (>= x highest)) hand))
(if (null? higher)
(min hand)
(min higher))
)
Use OK to unlock and test your code:
python3 ok -q fimp -u
python3 ok -q fimp
Question 3
Write a legal
procedure that takes a list of cards (in any order) called
hand
and the highest
card played so far. It returns a list of pairs. The
first element of each pair is a card that can be legally played. The second
element is True
or False
and indicates whether or not this card takes
control. The cards must be returned in the order that they appear in hand
.
(define (legal hand highest)
(define least (min hand))
(define (result hand)
(if (null? hand) nil (begin
(define card (car hand))
(define rest (result (cdr hand)))
(cond ((>= card highest) (cons (cons card #t) rest))
((= card least) (cons (cons card #f) rest))
(else rest))
)))
(result hand))
Use OK to unlock and test your code:
python3 ok -q legal -u
python3 ok -q legal
Here are some examples:
scm> (legal '(5 2 4 3) 4)
((5 . True) (2 . False) (4 . True))
scm> (legal '(4 2 4 3) 4)
((4 . True) (2 . False) (4 . True))
scm> (legal '(5 2 4 4 3) 5)
((5 . True) (2 . False))
scm> (legal '(5 2 4 4 3) 6)
((2 . False))
scm> (legal '(5 4) 3)
((5 . True) (4 . True))