100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Problem Set 10 - UBC - CPSC 110 $6.49   Add to cart

Class notes

Problem Set 10 - UBC - CPSC 110

 202 views  7 purchases
  • Course
  • Institution

Completed file for problem set 10. Remember to copy the pdf file to Dr Racket for a better view. This file should be exactly identical except that the file has answers for each problem.

Preview 2 out of 9  pages

  • December 20, 2022
  • 9
  • 2022/2023
  • Class notes
  • Gregor kiczales
  • All classes
avatar-seller
;; DO NOT PUT ANYTHING PERSONALLY IDENTIFYING BEYOND YOUR
CWL IN THIS FILE.
;; YOUR CWLs WILL BE SUFFICIENT TO IDENTIFY YOU AND, IF YOU HAVE
ONE, YOUR
;; PARTNER.
;;
(require spd/tags)

(@assignment psets/pset-10); Do not edit or remove this tag

;; If you are:
;; - A 110 or 107 student replace the first set of '???'s with your cwl.
;; For problem sets, If you have a partner, please replace the second
;; set of '???'s with their cwl. Remember this, it is what you will
;; do with these @cwl annotations for the whole course.
;; - A UBC Extended Learning student, replace the first set of ??? with
;; your email address as confirmed in the email you received from
;; extended learning. The handin password is also in that email.
;; Remember this, it is what you will do with these @cwl annotations
;; for the whole course.
;;
(@cwl ??? ???)


(@problem 1)
;;
;; Complete the design of the following function.
;;
;; Hint: sometimes for functions 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 times same string appears consecutively 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)
;;current-string is String; the previous first string of the list
;;count is Natural; the number of times that current has been seen so far
;;rsf is Natural; the highest count so far

(local [(define (fn-for-los los current-string count rsf)
(cond [(empty? los) rsf]
[else
(if (string=? (first los) current-string)
(if (>= (add1 count) rsf)
(fn-for-los (rest los) (first los)
(add1 count) (add1 count))
(fn-for-los (rest los) (first los) (add1 count) rsf))
(fn-for-los (rest los) (first los) 1 rsf))]))]
(if (empty? los)
0
(fn-for-los los (first los) 0 1))))




(@problem 2)
;;
;; Complete the design of the following function.
;;
;; Your solution MUST BE TAIL RECURSIVE.
;;

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 tranghane. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

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

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

73918 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
$6.49  7x  sold
  • (0)
  Add to cart