Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary CPSC 110 - PSET 10 SOLUTIONS -2022

Rating
-
Sold
1
Pages
5
Uploaded on
28-08-2022
Written in
2022/2023

Solutions to CPSC 110 Pset 10 2022

Institution
Course

Content preview

;; The first three lines of this file were inserted by DrRacket. They record
metadata
;; about the language level of this file in a form that our tools can easily
process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname pset-10-solution)
(read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-
decimal #f #t none #f () #t)))
;; 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



(@problem 1)

(@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)

(@template (listof String) accumulator)

(define (max-num-repeats los0)
;; prev is String: the previous first string in list
;; curr is Natural: number of times prev has been seen so far
;; rsf is Natural: maximum number of times a string has been seen so far
(local [(define (fn-for-los los prev curr rsf)
(cond [(empty? los) (max curr rsf)]
[else
(if (string=? (first los) prev)
(fn-for-los (rest los) (first los) (add1 curr) rsf)
(fn-for-los (rest los) (first los) 1 (max curr rsf)))]))]
(if (empty? los0)
0
(fn-for-los (rest los0) (first los0) 1 0))))

(@problem 2)

(@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 (listof Integer) accumulator)

(define (list-range loi)
;; max-rsf is Integer: maximum integer seen so far
;; min-rsf is Integer: minimum integer seen so far
(local [(define (fn-for-loi loi max-rsf min-rsf)
(cond [(empty? loi) (- max-rsf min-rsf)]
[else
(fn-for-loi (rest loi)
(max (first loi) max-rsf)
(min (first loi) min-rsf))]))]
(fn-for-loi (rest loi) (first loi) (first loi))))



(@problem 3)

(@htdf in-alphabetical-order?)
(@signature (listof String) -> Boolean)
;; produce true if list is sorted in order according to string-ci<=?
(check-expect (in-alphabetical-order? empty) true)
(check-expect (in-alphabetical-order? (list "a")) true)
(check-expect (in-alphabetical-order? (list "All" "bees" "Feel" "HAPPY")) true)
(check-expect (in-alphabetical-order? (list "aaaaa" "bb" "dd" "ccc")) false)

(@template (listof String) accumulator)

(define (in-alphabetical-order? los0)
;; prev is String: previous string in los
(local [(define (fn-for-los los prev)
(cond [(empty? los) true]
[else
(if (string-ci<=? prev (first los))
(fn-for-los (rest los) (first los))
false)]))]
(if (empty? los0)
true
(fn-for-los (rest los0) (first los0)))))



;;
;; Please read through the data definition introduced in Problem Set 6
;; for a Course.
;;

(@htdd Course)
(define-struct course (number credits dependents))
;; Course is (make-course Natural Natural ListOfCourse)
;; interp. a course with a course number,
;; the number of credits the course is worth,
;; a list of courses that have this course as a pre-requisite

Written for

Course

Document information

Uploaded on
August 28, 2022
Number of pages
5
Written in
2022/2023
Type
SUMMARY

Subjects

$18.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
travissmith1 UBC
Follow You need to be logged in order to follow users or courses
Sold
97
Member since
5 year
Number of followers
61
Documents
36
Last sold
8 months ago

3.6

16 reviews

5
6
4
6
3
0
2
0
1
4

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions