程序代写代做代考 DrRacket #lang racket

#lang racket

(provide (all-defined-out))

(require csc151)
(require rackunit)
(require rackunit/text-ui)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; CSC 151 (Fall 2020, Term 1) ;;;
;;; Debugging ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Side-B

#|
In this lab, you and your partner will explore using the two main
debugging tools we present in this course: print debugging and
the DrRacket debugger. You will also get an opportunity to try out
the hypothesis-driven debugging process.
|#

;;; B{{{

;;;;; Definitions for exercises 2 and 3 ;;;;;

(define numbers ‘(3 7 8 6 16 19 12 0 4 5 17 15 11 13 1 9 14 2 10 18))

(define sum (section reduce + <>))
(define smallest (section reduce min <>))
(define largest (section reduce max <>))

(define second-smallest
(lambda (numbers)
(list-ref (sort numbers <) 2))) (define second-largest (lambda (numbers) (list-ref (sort numbers >) 2)))

;;; Procedure:
;;; average-w/o-extremes
;;; Parameters:
;;; numbers, a nonempty list of real numbers
;;; Purpose:
;;; Computes the average of numbers after dropping the two highest and two lowest
;;; Produces:
;;; avg, a real number
;;; Preconditions:
;;; * (length numbers) is greater than 4
;;; Postconditions:
;;; * If max1, max2, min1, min2, are the largest, second largest,
;;; smallest, and second smallest values in numbers, respectively,
;;; then avg is the average of all the values in numbers excluding
;;; max1, max2, min1, min2
;;; * If all values in numbers are exact, then avg is exact
;;; * If at least one value in numbers is inexact, then avg is inexact
(define average-w/o-extremes
(lambda (numbers)
(/ (- (sum numbers)
(smallest numbers)
(second-smallest numbers)
(largest numbers)
(second-largest numbers))
(- (length numbers) 4))))

;;; Procedure:
;;; drop-to-first-zero
;;; Parameters:
;;; lst, a list of numbers
;;; Purpose:
;;; Removes all of the elements up to and including the first zero.
;;; Produces:
;;; newlst, a list of numbers
;;; Preconditions:
;;; The list contains at least one zero.
;;; Postconditions:
;;; Suppose the first zero is at index z.
;;; (length newlst) = (- (length lst) z 1)
;;; For all i s.t. z < i < (length lst) ;;; (list-ref newlst (- i z 1)) = (list-ref lst z) (define drop-to-first-zero (lambda (lst) (drop lst (index-of 0 lst)))) ;;; Procedure: ;;; remove-below ;;; Parameters: ;;; num-lst, a list of real numbers ;;; threshold, a real number ;;; Purpose: ;;; Remove all numbers strictly below threshold from the list. ;;; Produces: ;;; newlst, a list of real numbers ;;; Preconditions: ;;; [No additional] ;;; Postconditions: ;;; The numbers below threshold have been dropped. That is, newlst contains ;;; no number x such that x < threshold. ;;; All other numbers have been retained. That is, if x appears k times in ;;; lst and x >= threshold, then x appears k times in newlst.
;;; No additional numbers are in newlst. That is, if x appears k times in
;;; newlst then x appears k times in lst.
(define remove-below
(lambda (num-lst threshold)
(map (section + <> threshold)
(drop-to-first-zero
(map (section – <> threshold)
(sort (append (list threshold) num-lst) <)))))) ;;; Procedure: ;;; remove-above ;;; Parameters: ;;; num-lst, a list of real numbers ;;; threshold, a real number ;;; Purpose: ;;; Remove all numbers strictly above threshold from the list. ;;; Produces: ;;; newlst, a list of real numbers ;;; Preconditions: ;;; [No additional] ;;; Postconditions: ;;; The numbers above threshold have been dropped. That is, newlst contains ;;; no number x such that x > threshold.
;;; All other numbers have been retained. That is, if x appears k times in
;;; lst and x <= threshold, then x appears k times in newlst. ;;; No additional numbers are in newlst. That is, if x appears k times in ;;; newlst then x appears k times in lst. (define remove-above (lambda (num-lst threshold) (map (section + <> threshold)
(drop-to-first-zero
(map (section – <> threshold)
(sort (append (list threshold) numbers) >))))))

;;; Procedure:
;;; filter-range
;;; Parameters:
;;; num-lst, a list of real numbers
;;; lower, a real number
;;; upper, a real number
;;; Purpose:
;;; Removes all numbers strictly below lower and strictly above upper from the list.
;;; Produces:
;;; newlst, a list of real numbers
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; The numbers outside the range have been dropped. That is, newlst contains
;;; no number x such that x < lower or x > upper.
;;; All other numbers have been retained. That is, if x appears k times in
;;; lst and lower <= x <= upper, then x appears k times in newlst. ;;; No additional numbers are in newlst. That is, if x appears k times in ;;; newlst then x appears k times in lst. (define filter-range (lambda (num-lst lower upper) (remove-below (remove-above num-lst upper) lower))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Exercise 2: Identifying potential problems ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #| (a) Write a test suite for `average-w/o-extremes`. Your test suite should be sufficiently robust that it is likely to identify an error in most incorrect implementations of `average-w/o-extremes`. Here is a test suite to get you started: |# (define average-w/o-extremes-tests (test-suite "tests of average-w/o-extremes" (test-case "lists of size 7" (check-= (average-w/o-extremes (list 1 2 2 3 4 4 5)) 3 0) (check-= (average-w/o-extremes (list 2 2 2 5 8 8 8)) 5 0)) #| TODO: add more tests! |# )) #| (b) Run your test suite on the code provided above: > (run-tests average-w/o-extremes-tests)

If your test suite passes all the tests, add the following check to
your test suite and run it again.

(check-= (average-w/o-extremes (list 1 2 5 5 5 6 7)) 5 0)
|#

#|
(c) At this point, you should have encountered an error. Use
hypothesis-driven debugging to gather data and assumptions,
formulate a hypothesis as what is wrong, and use your debugging
tools to discover and ultimately fix the problem. Force yourself
to use both kinds of debugging tools to practice their usage.

In space below, describe the problem with the function and how you
ultimately fixed it:


|#

;;; }}}B

;;; B{{{

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Exercise 3: Other types of problems ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#|
(a) Familiarize yourself with the documentation for
`drop-to-first-zero`, `remove-below`, `remove-above`, and
`filter-range`.

Execute `(filter-range numbers 5 15)` in the interactions pane to
verify that it filters out all numbers outside of the range 5 to 15.
Does it give you the output you expect?
|#

#|
(b) Notice that `filter-range` duplicated the `lower` and `upper`
values in the list of numbers. Use hypothesis-driven debugging
to discover and fix the error. The debugger might be useful here
to help step through the different functions that are called.

Describe the problem you found and the solution in the space below:


|#

#|
(c) Write a few more tests for `filter-range` using your own lists and bounds and run them.

If you did not find any errors, consider the case:

(filter-range (list) 15 20)

Use hypothesis-driven debugging to discover the problem and fix it.
Describe the problem you found and the solution in the space below:


|#