100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
CPSC110 problem set 10 solution $8.48
Add to cart

Other

CPSC110 problem set 10 solution

 1 view  0 purchase
  • Course
  • Institution

completed solution for problem set 10

Preview 2 out of 6  pages

  • January 5, 2024
  • 6
  • 2023/2024
  • Other
  • Unknown
avatar-seller
(@problem 1)
;;
;; Complete the design of the following func:on.
;;
;; Hint: some:mes for func:ons with accumulators it is useful for
;; the trampoline to deal with the special case of the empty
;; list or empty tree using an if expression.
;;

(@htdf max-num-repeats)
(@signature (listof String) -> Natural)
;; produce maximum number of :mes same string appears consecu:vely in los0
(check-expect (max-num-repeats empty) 0)
(check-expect (max-num-repeats (list "cat")) 1)
(check-expect (max-num-repeats (list "cat" "bird" "dog")) 1)
(check-expect (max-num-repeats (list "cat" "cat" "bird" "dog")) 2)
(check-expect (max-num-repeats (list "cat" "cat" "bird" "dog" "dog" "dog"))
3)
(check-expect (max-num-repeats (list "cat" "cat" "cat"
"bird"
"boy" "boy" "boy"
"toy" "toy" "toy" "toy" "toy"
"trick"
"zebra" "zebra" "zebra" "zebra"))
5)
(check-expect (max-num-repeats (list "dog" "cat" "bird"
"dog" "dog" "dog"
"moose" "dog"))
3)

(@template-origin (listof String) accumulator)

;(define (max-num-repeats los) 0)

(define (max-num-repeats los0)
(local [(define (max-num-repeats los rsf prev)
(cond [(empty? los) (if (< rsf prev) prev rsf)]
[else
(if (member (first los) (rest los))
(max-num-repeats (rest los) (add1 rsf) prev)
(if (< prev rsf)
(max-num-repeats (rest los) 1 rsf)
(max-num-repeats (rest los) 1 prev)))]))]
(max-num-repeats los0

, (if (empty? los0)
0
1)
0)))


(@problem 2)
;;
;; Complete the design of the following func:on.
;;
;; Your solu:on MUST BE TAIL RECURSIVE.
;;

(@htdf list-range)
(@signature (listof Integer) -> Natural)
;; produce the difference between the max and min integer in the list
;; CONSTRAINT: loi0 has at least one element
(check-expect (list-range (list 100)) 0)
(check-expect (list-range (list 2 -5 -10 50 80)) 90)
(check-expect (list-range (list 5000 -5 -100 50 0)) 5100)
(check-expect (list-range (list 3 8 1 2 9 4 2 3 -5)) 14)
(check-expect (list-range (list -5000 3 2 2 4 5000 4 2 3)) 10000)
(check-expect (list-range (list 400 500 500 400)) 100)

(@template-origin (listof Integer) accumulator)

;(define (list-range loi) 0)

(define (list-range loi0)
(local [(define (list-range loi acc max min)
(cond [(empty? loi) (- max min)]
[else
(list-range (rest loi) (- max min)
(if (> (first loi) max)
(first loi)
max)
(if (< (first loi) min)
(first loi)
min))]))]
(list-range loi0 0 -inf.0 +inf.0)))

(@problem 3)
;;
;; Complete the design of the following func:on.

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller skye3. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $8.48. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

53068 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$8.48
  • (0)
Add to cart
Added