HtDP Solution Set | Section 2 |
Problem 1 (Solution): ;rect-area: Number Number -> Number
; compute the area of a rectangle given its length and width
#|
(define (rect-area length width) ...)
(rect-area 0 0)
"should be" 0
(rect-area 1 2)
"should be" 2
(rect-area 3 3)
"should be" 9
|#
Problem 2 (Solution): ;distance: Number Number Number -> Number
; - compute time for boat to cross a river
(define (distance width v-boat v-river)
(sqrt (+ (expt width 2) (expt (/ (* v-river width) v-boat) 2))))
(distance 10 10 0)
"should be" 10
(distance 4 3 9/4)
"should be" 5
Problem 3 (Solution): ;t-elapsed: Number Number Number -> Number
; - calculate how long it will take the two trains to meet
(define (t-elapsed v-train1 v-train2 dist)
(/ dist (+ v-train1 v-train2)))
(t-elapsed 50 50 100)
"should be" 1
(t-elapsed 25 50 100)
"should be" 4/3
Problem 4 (Solution): ;balance : Number Number Number -> Number
; - compute balance with simple interest
(define (balance principal rate time)
(+ principal (* principal (/ (* rate .01) 12) time)))
(balance 1000 10 0)
"should be" 1000
(balance 1000 10 12)
"should be" 1100
Problem 5 (Solution): ;tiles : Number Number -> Number
; - compute number of full times necessary for floor
(define (tiles f-length f-width t-length t-width)
(* (ceiling (/ f-width t-width))
(ceiling (/ f-length t-length))))
(tiles 10 10 1 1)
"should be" 100
(tiles 24 37 1.5 1.5)
"should be" 400
Problem 6 (Solution): ;poly-area : Number Number -> Number
; - return area of polygon
(define (poly-area sides length)
(* 1/4 sides (expt length 2) (/ 1 (tan (/ pi sides)))))
(poly-area 3 2)
"should be" (sqrt 3)
(poly-area 4 4)
"should be" 16
Problem 7 (Solution): ; mean : Number Number Number Number Number -> Number
; - return mean of 5 scores
(define (mean-5 e1 e2 e3 e4 e5)
(/ (+ e1 e2 e3 e4 e5) 5))
(mean-5 0 0 0 0 0)
"should be" 0
(mean-5 1 2 3 4 5)
"should be" 3
Problem 8 (Solution): ;perim : Number -> Number
; - given area of square, return its perimeter
(define (perim area)
(* 4 (sqrt area)))
(perim 0)
"should be" 0
(perim 4)
"should be" 8
Problem 9 (Solution): ;height : Number -> Number
; - given height of ball, compute how far it travels for three bounces
(define (height h)
(+ h (* 2 2/3 h) (* 2 2/3 2/3 h)))
(height 36)
"should be" 116
Problem 10 (Solution): ; given length in feet and inches, return total number of inches
(define (total-inches ft in)
(+ (* ft 12) in))
(total-inches 0 0)
"should be" 0
(total-inches 1 1)
"should be" 13
Problem 11 (Solution): ;net-pay : Number -> Number
; given salary, produce net pay based on progressive tax rate
(define (net-pay salary)
(* (- 1 (tax-rate salary)) salary))
(define (tax-rate salary)
; given salary, return appropriate tax rate
(* (/ salary 1000) .005))
(net-pay 0)
"should be" 0
(net-pay 40000)
"should be" 32000
"An income over 200,000 requires more to be paid than actually earned."
Problem 12 (Solution): ;semester-grade : Number Number -> Number
; given homework and final exam grades, compute semester grade
(define (semester-grade hw fexam)
(+ (* .8 hw) (* .2 fexam)))
(semester-grade 0 0)
"should be" 0
(semester-grade 100 100)
"should be" 100
Problem 13 (Solution): (define (winning-average wins losses)
(* 1000 (/ wins (+ wins losses))))
(winning-average 0 1)
"should be" 0
(winning-average 10 10)
"should be" 500
Problem 14 (Solution): ;;fuel-efficiency : Number Number -> Number
;; given miles driven and gallons of gas consumed, compute fuel efficiency in mpg
;;annual-groceries : Number -> Number
;; given amount spent per week, compute annual amount
(define (annual-groceries weekly) ...)
;(annual-groceries 0) "should be" 0
;(annual-groceries 100) "should be" 5200
;;slope : Number Number -> Number
;; compute the slope of a line from the the change in y and the change in x
;; volume : Number Number Number -> Number
;; compute the volume of a tank for given width, height, and depth
;(volume 0 0 0) "should be" 0
;(volume 1 1 1) "should be" 1
;(volume 10 3 2) "should be" 60
;; quiz-average : Number Number Number Number Number -> Number
;; consumes five quiz grades and produces the quiz average
; (quiz 80 80 90 100 100) "should be" 90
; (quiz 80 80 80 80 80) "should be" 80
(define (quiz-average q1 q2 q3 q4 q5) ...)
;;annual-groceries : Number -> Number
;; given amount spent per week, compute annual amount
(define (annual-groceries weekly) ...)
;(annual-groceries 0) "should be" 0
;(annual-groceries 100) "should be" 5200
Problem 15 (Solution): ;; minutes : Number -> Number
;; how many minutes does it take Sis Y. Fuss to push a rock up a hill (30 up, 4 down)
;; (minutes 20) "should be" 1, because she reaches 30 and then doesn't slide back
;; (minutes 30) "should be" 2, because she goes to 30, slides down to 26, and has to push again
(define (minutes d)
(ceiling (/ d (- 30 4))))