This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: ;; Question 1 ;; Part (a) ;; Some useful constants
(define penny—val 0.01)
(define nickeldval 0.05)
(define dime—val 0.1)
(define quarter—val 0.25)
(define loonie—val 1)
(define toonie—val 2) ;; Helper function
;; coin—amount: Symbol —> Num
;; Given a symbol coin, produce the value of that coin
;; Examples
(check— expect (coin— amount ‘penny) 0. 01)
(check— expect (coin— amount ‘lint) 0)
;; Definition
(define (coin—amount coin)
(cond [(equal? coin ‘penny) penny—val]
[(equal? coin ‘nickel) nickel—val] \\\\ /
[(equal? coin ‘dime) dime—val]
[(equal? coin ‘quarter) quarter—val] [(equal? coin ‘loonie) loonie—val] [(equal? coin ‘toonie) toonie—val]
[else 0] n _ Q ,, Tests g;/
(check—expect (coin—amount 'ezh el) 05)
(check—expect (coin—amount ‘dime) 0.1)—
(check—expect (coin—amount ‘quarter 0.25)
(check—expect (coin—amount ‘loonie) 1
(check—expect (coin—amount ‘toonie) ;; pocket—change: (listof Symbol) —> Num
;; Purpose: produce the total amount of all the coins
;; that are contained in changelist
;; Examples:
(check—expect (pocket—change (list ‘penny ‘nickel ‘dime
‘quarter ‘loonie 'toonie)) 3.41)
(check—expect (pocket—change empty) 0)
(check—expect (pocket—change (cons ‘penny (cons ‘penny empty))) 0.02)
;; Definition
(define (pocket—change changelist)
(cond
[(empty? changelist) 0]
[else (+ (coin—amount (first changelist))
(pocket—change (rest changelist)))])) ;; Tests:
(check—expect (pocket—change (cons ‘nickel (cons 'dime empty))) 0.15) This document is for the exclusive use of j65u. (checkexpect (pocketchange ‘(quarter)) 0.25) (check—expect (pocket—change ‘(loonie ‘toonie 'dime)))
3.1) (check—expect (pocket—change ‘(penny penny quarter lint quarter))
0.52) ;; Part (b)
(define—struct coin (name value))
;; A Coin=(make—coin Symbol Num[>=0])  Part (c)
(define (my—coin—fn coin)
;: ... (coin—name coin) 5
a. "u
a. ;: ... (coin—value coin) ...)
;; Part (d) ;; A (listof coin) is either
;: * empty 5
a. a. (define penny (make—coin 'penny 0.01))
(define dime (make—coin ‘dime 0.1))
(define lint (make— coin ‘lint 0))
(define fifty (make—coin 'fifty 50) <7 * (cons (make—coin Symbol Num[>=0]) (listof coin)
;' Part (e) \j
;; Some constants for testing 2/_\/ (define coin—list
(cons penny (cons dime (con) lin QQ:5L fifty empty))))) ;; Helper
;; one—coin: Symbol (listof Coin) —
;; Produce the value of the coin, if Nt is in the list coin—list;
;; otherwise produce 0
; Examples:
(check— expect (onecoin ‘dime coin—list) 0.10)
(check— expect (onecoin ‘harry coin—list) 0)
;; Definitions
(define (onecoin coin dict)
(cond
[(errpty? dict) 0]
[(equal? (coin—name (first diot)) coin) (coindvalue (first dict))]
[else (one—coin coin (rest dict))]))
;; Tests:
(check—expect (onecoin ‘penny coin—list) 0.01)
(check—expect (onecoin ‘lint coin—list) 0)
(check—expect (onecoin ‘fifty coin—list) 50) ;; strange—currency: (listof coin) (listof Symbol) ;; Purpose:
;; Produce the total value of all the coins in pocket This document is for the exclusive use of j65u. ;; Examples:
(check—expect (strange—currency coin—list ‘(penny))
0.01)
(check—expect (strange—currency coin—list
'(dime fifty))
50.10)
;; Definition
(define (strange—currency dict pocket)
(cond
[(empty? pocket) 0]
[else (+ (one—coin (first pocket) dict)
(strange—currency dict (rest pocket)))]))
;; Tests:
(check—expect (strange—currency coin—list empty) 0)
(check—expect (strange—currency coin—list ‘(lint lint)) 0)
(check—expect (strange—currency coin—list ‘(fifty penny)) 50.01) ;; Question 2 ;; Part (a) ;; occurrences: (listof Num) Num —> Nat ;; Given a list of numbers lon and a number val
;; produce the number of times val occurs i the
;; Example: (check— expect (occurrences (list 3 1 3 2) ) 2)
(check— expect (occurrences (list 3 1 2 4) ) (check—expect (occurrences empty 9.1) 0)
(define (occurrences lon val) ,> (cond <\ ' ME
[(ensty? lon) 0] '
[(equal? (first lon) val 1 ( ences (rest lon) val))]
[else (occurrences (rest lon) val)
;; Tests
(check—expect (occurrences (list 0 1 3444340404) ;; Part (b)
;; Helper functions
;; compute—grade: Nat Nat Nat —> Num
;; Compute the participation grade,
;; given the number of clickers answered
;; correctly (two), incorrectly (one) and not at all (zero)
; Examples:
(check— expect (compute— grade 10 5 4 1) (* 5 14/20))
(define (compute— grade numNeeded two one zero)
(cond
[(> two numNeeded) 5]
[(> (+ one two) numNeeded)
(* (/ (+ (* 2 two) (— numNeeded two)) (* numNeeded 2)) 5)]
[else (* (/ (+ (* 2 two) (* 1 one)) (* numNeeded 2)) 5)]))
;; Tests
(check—expect (compute—grade 8 9 5 2) 5) ;; case 1
(check—expect (compute—grade 8 8 4 1) 5) ;; boundary at case 1 This document is for the exclusive use of j65lu. (checkexpect (compute—grade 10 9 4 1) (* 5 19/20)) ;; boundary at case 2
(checkexpect (compute—grade 10 9 l 1) (* 5 19/20)) ;; boundary at case 3
(checkexpect (compute—grade 10 9 0 3) (* 5 18/20)) ;; case 3 ;; participation: (listof Num) —> Num ;; Given a list of clicker scores (0, 1, 2) in clicks, ;; produce the actual participation grade ;; Examples: (checkexpect (participation (list 0 l 2 1)) 20/6) (checkexpect (participation (list 2)) 5) (define (participation clicks) (compute—grade (ceiling (* 0.75 (length olicks))) (occurrences clicks 2)
(occurrences clicks 1)
(occurrences clicks 0))) ;; Tests (checkexpect (participation (list 2 2 2 2 2)) 5) (checkexpect (participation (list 1 l 1 1 l 1 1 l 1)) 2.5)
(checkexpect (participation (list 0 l 2 0 l 2 0 l 2)) (* 5 (/ 9 14)))
(checkexpect (participation (list 2 2 2 2 2 2 0 0)) ) (checkexpect (participation (list 1 l 1 1 2 2 2 2)) ( 5 10/12)) ;; Question 3 2 \ j
;; Data definition /f/ (define—struct card (suit rank)) :? ,, A Card=(make—card Symbol Nat) (  __
;; Some cards for testing > L) ;; Some constants —
(define 9clubs (makecard ‘clubs 9) (define 10spades (make—card 'spades 1 ))
(define Shearts (makecard ‘hearts 3) (define 4hearts (makecard ‘hearts 4))
(define Shearts (makecard ‘hearts 5)) ;; Part (a)
 A (listof Card) is either:
;: *empty
;: * (cons Card (listof Card)) 5
a. ;; the second condition could also be
;; * (cons (make—card Symbol Nat) (listof Card)) ;; Part (b) ;; Helper ;; same—suit: Symbol (listof Card) —> Boolean ;; Produce true if the given card suit is the only
;; suit given in the hand ;; Examples: This document is for the exclusive use of j65lu. (check—expect (same—suit 'hearts (list 3hearts 4hearts)) true)
(check—expect (same—suit 'hearts (list 3hearts 9clubs)) false)
;; Definition
(define (same—suit suit hand)
(cond
[(empty? hand) true]
[(not (symbol=9 suit (card—suit (first hand)))) false]
[else (same—suit suit (rest hand))]))
;; Tests:
(check—expect (same—suit 'diamonds empty) true)
(check—expect (same—suit 'hearts (list 9clubs 4hearts)) false) ;; is—flush?: (listof Card) —> Boolean
;; Produces true if all the cards in hand
;; have the same suit, and false otherwise
(check—expect (is—flush? (list 3hearts 4hearts)) true)
(check—expect (is—flush? (list 3hearts 9clubs)) false)
(define (is—flush? hand) (cond (check— expect (is— —flush9 empty) true) [(empty? hand) true]
[(empty? (rest hand)) true]
[else (same— suit (card—suit (first hand K42(rest\hand ]))
;; Tests
t ) t (check— expect (is— —flush9 (cons lOspades% e)/
;; Part (c) /\ ;; better—rank?: Card Card —> Bodiean E ;; Produces true if cardl has high r<rank than card2
;; Examples: (check—expect (better—rank? 9club 10s des) false) (check—expect (better—rank? lOspade 9clubs) true) (define (better—rank? cardl card2)
(> (card—rank cardl) (card—rank ca ;; Tests: (check—expect (better—rank? 9clubs 9clubs) false) H) ;; Part (d)
;; sort—hand—by—rank: (listof Card) —> (listof Card)
;; Produce a list of the cards of hand,
;; sorted by rank order
;; Examples:
(check— expect
(sort— —hand— —by— —rank (list 9clubs 3hearts 5hearts))
(list 3hearts Shearts 9clubs))
(check—expect (sort—hand—by—rank empty) empty)
;; Definition
(define (sort—hand—by—rank hand)
(cond [(empty? hand) empty]
[else (insert (first hand) (sort—hand—by—rank (rest hand)))])) This document is for the exclusive use of j65lu. ;; Tests:
(check—expect (sort—hand—by—rank (list 9clubs Shearts Shearts))
(list 3hearts Shearts 9clubs))
(check—expect (sort—hand—by—rank (list 3hearts Shearts 9clubs))
(list 3hearts Shearts 9clubs))
(check—expect (sort—hand—by—rank (list 3hearts 9clubs Shearts))
(list 3hearts Shearts 9clubs))
(check—expect (sort—hand—by—rank (list Shearts 3hearts 9clubs))
(list 3hearts Shearts 9clubs))
(check—expect (sort—hand—by—rank (list Shearts 9clubs Shearts))
(list 3hearts Shearts 9clubs))
(check—expect (sort—hand—by—rank (list Shearts 9clubs Shearts))
(list Shearts Shearts 9clubs)) ;; insert: Card (listof Card) —> (listof Card)
;; insert the given card into the hand, maintaining
;; the ordering by rank
”Example
(check— expect (insert 3hearts empty) (list 3hearts))
(check— expect (insert 3hearts (list Shearts)) (list earts Shearts))
(check—expect (insert Shearts (list 3hearts 9clubs)) (list 3hearts Shearts 9clubs) \\\\X/
;; Definition
(define (insert card hand)
(cond {{7\/
[(empty? hand) (cons card empty)] [(better—rank? card (first handi? (cons (first hand) (insert 'rd ( t __ )))]
[else (cons card hand)])) <;
;; Tests: (check— expect (insert Shearts (li t 3h rts)) (list 3hearts Shearts))
(check— expect (insert 9clubs (list earts Shearts)) (list 3hearts Shearts 9 lubs))
(check—expect (insert 3hearts (list 5 earts 9clubs)) (list 3hearts Shearts 9clubs))  Part (e)
 is—straight?: (listof Card) —> Boolean
 produce true if the given hand can be formed into a straight ;; Examples: ‘ ‘
x. x. ‘
\. (check expect
(check— expect
(check—expect
(check—expect
;; Definition (is—straight? (list 4hearts 3hearts Shearts)) true)
(is—straight? empty) true) (is—straight? (list 9clubs)) true) (is—straight? (list Shearts Shearts)) false) (define (is—straight? hand)
(is—increasing? (sort—hand—by—rank hand))) ;; Tests:
(check—expect (is—straight? (list 3hearts Shearts)) false) ;; is—increasing?: (listof Card) —> Boolean This document is for the exclusive use of j65lu. ;; Given a hand in sorted rank order, produce truee
;; if are all the cards distinct and in increasing order
;; Examples:
(check—expect (is—increasing? (list 3hearts)) true)
(check—expect (is—increasing? empty) true)
(check—expect (is—increasing? (list 3hearts 4hearts)) true)
(check—expect (is—increasing? (list 3hearts 5hearts)) false)
;; Definition
(define (is—increasing? hand)
(cond [(empty? hand) true] [(empty? (rest hand)) true] [else (and (= (+ l (card—rank (first hand))) (cardrrank (first (rest hand))))
(is—increasing? (rest hand)))])) ;; Tests:
(check—expect (is—increasing? (list 3hearts 3hearts)) false)
(check—expect (is—increasing? (list 3hearts 4hearts 9clubs)) false) ,, Question 4 ;; Part (a) ,, list—of—digits—>: (listof Nat) —> Nat \\ ;; Consume a list of digits, digit—list, and od yd)
;; the natural number formed from their its, 'n re rse
;; Examples r
(check—expect (list—cf—digits—>num (list 3 2 )) 1234)
(check—expect (list—cf—digits—>num (list _})) 0)
(check—expect (list—cf—digits—>n (li‘ 0)i_ ;; Definition
(define (list—cf—digits—>num;2i it—l )
(cond —
[(empty? digit—list) 0]
[else (+ (first digit—list)
(* 10 (list—of—digits—> (rest digit—list))))]))
;; Tests: (check—expect (list—cf—digits—>num (list 5 3 3)) 335)
(check—expect (list—cf—digits—>num (cons 4 empty)) 4) ;; Part (b) ;, (define (my—natural—fn n) ;; ... (cond ;, [(< natnum 10) ... n ...] ;, [else .... (my—natural—fn (floor (/ n 10))
;; (remainder n 10) ...])) ;; Part (c) ;; num—>list—of—digits: Nat —> (listof Nat) ;; Produce a list of the digits of n, in reverse order
(check—expect (num—>list—of—digits 533) (list 3 3 5))
(check—expect (num—>list—of—digits 4) (cons 4 empty)) This document is for the exclusive use of j65lu. (define (num—>list—of—digits n)
(cond [(< n 10) (cons n empty)]
[else (cons (remainder n 10)
(num—>list—of—digits (floor (/ n 10))))])) ,; Tests:
(check—expect (num—>list—of—digits 0) (list 0))
(check—expect (num—>list—of—digits 10) (cons 0 (cons 1 empty)))
(check—expect (num—>list—of—digits 1234) (list 4 3 2 1))
(check—expect (num—>list—of—digits 10000) (list 0 0 0 0 1)) This document is for the exclusive use of j65u. ...
View
Full Document
 Fall '07
 VASIGA

Click to edit the document details