Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help in DrRacket #lang racket (require rackunit) A. Write a Racket function named candy-temperature-at that takes one number as an argument: elevation (in feet)

please help in DrRacket

#lang racket (require rackunit)

A. Write a Racket function named candy-temperature-at that takes one number as an argument: elevation (in feet) of a given location. candy-temperature-at returns a function that takes one argument, a temperature (in degrees Fahrenheit) to use for a candy. The returned function returns as its value the temperature to use for that candy at the specified elevation. For example:

> ((candy-temperature-at 5280) 302) 291.44

> (define temp-in-cf (candy-temperature-at 959))

> (temp-in-cf 240) ;; fudge! 238.082

I have provided check-equal? expressions for these examples. Write at least two more check-equal? expressions to test your solution.

(define candy-temperature-at (lambda (elevation) 'YOUR-CODE-HERE ))

; ----- tests -------

(define temp-in-cf (candy-temperature-at 959)) (check-equal? ((candy-temperature-at 5280) 302) 291.44) (check-equal? (temp-in-cf 240) 238.082)

------------------- B. Write a Racket function named in-range-of that takes one number as an argument: the epsilon to use as a tolerance. in-range-of returns as its value a function that takes two number arguments. The returned function returns true if the difference between its arguments is less than epsilon, and false otherwise. For example:

> ((in-range-of? 0.1) 4.95 5.0) #t

> ((in-range-of? 0.1) 5.0 4.95) ;; works both ways #t

> (define within-0.01? (in-range-of? 0.01))

> (within-0.01? 4.95 5.0) ;; not anymore! #f

> (within-0.01? 5.0 4.99) #t

I have provided check-equal? expressions for these examples. You do not have to write any more tests for this problem.

(define in-range-of? (lambda (epsilon) 'YOUR-CODE-HERE ))

; ----- tests -------

(check-true ((in-range-of? 0.1) 4.95 5.0)) (check-false ((in-range-of? 0.1) 5.0 4.85))

(define within-0.01? (in-range-of? 0.01)) (check-false (within-0.01? 4.95 5.0)) ;; not anymore! (check-true (within-0.01? 5.0 4.99))

; -------------------

C. Suppose that we have a list containing height/weight pairs for a group of people, in inches and pounds, respectively:

( (76 . 195) (81 . 212) (79 . 225) (78 . 206) ... )

We would like to know the average weight of the people in the group.

Use higher-order functions such as map and apply to define a Racket function named average-weight. This function takes a list of height/weight pairs as its only argument and returns the the average weight of the group. For example:

> (average-weight '((79 . 225))) 225.0

> (average-weight '((70 . 150) (62 . 100))) 125.0

Assume that we have already written a variable-arity function named average. You may write other helper functions if you like, but you do not have to.

I have provided check-equal? expressions for these examples. Write at least two more check-equal? expressions to test your solution.

(define average-weight (lambda (height-weight-list) 'YOUR-CODE-HERE ))

; ----- tests -------

(check-equal? (average-weight '((79 . 225))) 225.0) (check-equal? (average-weight '((70 . 150) (62 . 100))) 125.0) (check-equal? (average-weight '((76 . 195) (81 . 212) (79 . 225) (78 . 206))) 209.5)

-------------------

D. A buddy of mine wrote a program that predicts the results of college basketball games (pretty well, I might add). At the end of a week, he would like to know how far his program's predictions were from the actual results. His program has generated a list of predicted difference/actual difference pairs:

( (2 -7) (-4 -20) (7 8) (-13 2) ... )

The first item in this list says that his program predicted Team 1 would win by 2 points, but Team 2 won by 7 points. For that game, his program was off by abs(2 - (-7)) == abs(9) == 9 points. The third item says that his program predicted that Team 1 would win by 7 points and that it won by 8 points, so his program was off by abs(7 - 8) == abs(-1) == 1 point. The list can contain any number of these pairs.

(This, too, is not much of a supposition... He wrote his program in Lisp, an ancestor of Racket, and he can surely write a simple expression to produce such a list from his database!)

Write a Racket function named total-error that takes one argument, a list of this form. The function returns the total of all the differences in the list. For example:

> (define example '((2 -7) (-4 -20) (7 8) (-13 2))) > (total-error example) 41

I have provided a check-equal? expression for this example. Write at least three more check-equal? expressions to test your solution.

(define total-error (lambda (list-of-games) 'YOUR-CODE-HERE ))

; ----- tests -------

(check-equal? (total-error '((2 -7) (-4 -20) (7 8) (-13 2))) 41)

-------------------

E. To monitor enrollments each semester, I have a spreadsheet that contains a list of courses with names, enrollments, and capacities. I read the spreadsheet data into a Racket list that looks like this:

'(("Dept" "Number" "Section" "Class Nbr" "Capacity" "Enrollment") ("CS" "1000" "1" "11546" "30" "30") ("CS" "1025" "1" "11547" "30" "30") ("CS" "1120" "1" "11557" "30" "15") ("CS" "1130" "1" "11548" "30" "18") ... )

The dean and provost frequently ask me for various summary data, such as total enrollments or remaining capacity.

Write a Racket function named max-open-seats that takes such as a list as its only argument. It returns the maximum number of open seats available in any section. The number of open seats is the difference between capacity and enrollment. For example:

> (define example '(...)) ; the data shown above > (max-open-seats example) 15

CS 1120 has 30-15 = 15 open seats. The other classes have 0, 0, and 12 open seats each.

I have provided a check-equal? expression for this example. Write at least three more check-equal? expressions to test your solution.

(define max-open-seats (lambda (sections) 'YOUR-CODE-HERE ))

; ----- tests -------

(define example '(("Dept" "Number" "Section" "Class Nbr" "Capacity" "Enrollment") ("CS" "1000" "1" "11546" "30" "30") ("CS" "1025" "1" "11547" "30" "30") ("CS" "1120" "1" "11557" "30" "15") ("CS" "1130" "1" "11548" "30" "18"))) (check-equal? (max-open-seats example) 15)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

2nd Edition

1597499471, 978-1597499477

More Books

Students also viewed these Databases questions