100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Exam (elaborations)

University of British Columbia CPSC 110 LAB 8

Rating
3.3
(3)
Sold
4
Pages
5
Grade
A+
Uploaded on
15-10-2021
Written in
2021/2022

University of British Columbia CPSC 110 LAB 8

Institution
Course









Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

Uploaded on
October 15, 2021
Number of pages
5
Written in
2021/2022
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

Content preview

(@problem 1)

;; The first four problems use the following data definition,
;; which represents a path through a binary search tree.

(@htdd Path)
;; Path is one of:
;; - empty
;; - (cons "L" Path)
;; - (cons "R" Path)
;; interp.
;; A sequence of left and right 'turns' down through a binary tree
;; (list "L" "R" "R") means take the left child of the root, then
;; the right child of that node, and the right child again.
;; empty means you have arrived at the destination.




m
er as
(define P1 empty)
(define P2 (list "L" "R"))




co
eH w
(define P3 empty)
(define P4 (cons "L" (cons "R" empty)))




o.
rs e
(define P5 (cons "L" (cons "R" (cons "R" empty))))
ou urc
(@dd-template-rules one-of atomic-distinct self-ref self-ref)
(define (fn-for-path p)
o

(cond [(empty? p) (...)]
aC s

[(string=? (first p) "L") (... (fn-for-path (rest p)))]
vi y re


[(string=? (first p) "R") (... (fn-for-path (rest p)))]))
ed d




;; Design an abstract function (including signature, purpose, and tests)
ar stu




;; called num-lr to simplify the lefts-minus-rights and rights-minus-lefts
;; functions defined below.
;;
is




;; Then re-define the original lefts-minus-rights and rights-minus-lefts
;; functions to use your abstract function. Remember, the signature and tests
Th




;; should not change from the original functions. For simplicity, assume
;; that all numbers throughout this problem have type Integer.
sh




(@htdf lefts-minus-rights)
(@signature Path -> Integer)
;; produce the difference between left turns and right turns

(check-expect (lefts-minus-rights empty) 0)


This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 21:14:32 GMT -05:00


https://www.coursehero.com/file/82100404/LAB8docx/

, (check-expect (lefts-minus-rights (list "R" "L" "R")) -1)
(check-expect (lefts-minus-rights (list "L" "R" "L")) 1)

(@template use-abstract-fn)
(define (lefts-minus-rights p)
(num-lr add1 sub1 p))

(@htdf rights-minus-lefts)
(@signature Path -> Integer)
;; produce the difference between right turns and left turns

(check-expect (rights-minus-lefts empty) 0)
(check-expect (rights-minus-lefts (list "R" "L" "R")) 1)
(check-expect (rights-minus-lefts (list "L" "R" "L")) -1)




m
er as
(@template use-abstract-fn)
(define (rights-minus-lefts p)




co
eH w
(num-lr sub1 add1 p))




o.
rs e
;; Use the space below to design the abstract function for Problem 1:
ou urc
(@htdf num-lr)
(@signature (Integer -> Integer) (Integer -> Integer) Path -> Integer)
o

;; produce the difference between left and right turns
aC s
vi y re


(check-expect (num-lr add1 add1 empty) 0)
(check-expect (num-lr add1 add1 empty) 0)
(check-expect (num-lr add1 sub1 (list "L" "R" "L")) 1)
(check-expect (num-lr sub1 add1 (list "R" "L" "R")) 1)
ed d




(check-expect (num-lr add1 sub1 (list "R" "L" "R")) -1)
ar stu




(check-expect (num-lr sub1 add1 (list "L" "R" "L")) -1)

(@template Path)
is




(define (num-lr f1 f2 p)
Th




(cond [(empty? p) 0]
[(string=? (first p) "L") (f1 (num-lr f1 f2 (rest p)))]
[(string=? (first p) "R") (f2 (num-lr f1 f2 (rest p)))]))
sh




(@problem 2)
;;
;; Use your abstract function from the previous problem to design a function
;; called path-length that determines the length of a given path.


This study source was downloaded by 100000805705997 from CourseHero.com on 10-14-2021 21:14:32 GMT -05:00


https://www.coursehero.com/file/82100404/LAB8docx/
$3.49
Get access to the full document:
Purchased by 4 students

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached


Also available in package deal

Reviews from verified buyers

Showing all 3 reviews
3 year ago

3 year ago

3 year ago

why did you give bad rating?

4 year ago

4 year ago

Thanks for rating. Please let me know if you need more help

3.3

3 reviews

5
1
4
1
3
0
2
0
1
1
Trustworthy reviews on Stuvia

All reviews are made by real Stuvia users after verified purchases.

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.
StuviaGuides West Virgina University
Follow You need to be logged in order to follow users or courses
Sold
15169
Member since
6 year
Number of followers
8354
Documents
5312
Last sold
1 day ago
Accounting, Finance, Statistics, Computer Science, Nursing, Chemistry, Biology & More — A+ Test Banks, Study Guides & Solutions

As a Top 1st Seller on Stuvia and a nursing professional, my mission is to be your light in the dark during nursing school and beyond. I know how stressful exams and assignments can be, which is why I’ve created clear, reliable, and well-structured resources to help you succeed. I offer test banks, study guides, and solution manuals for all subjects — including specialized test banks and solution manuals for business books. My materials have already supported countless students in achieving higher grades, and I want them to be the guide that makes your academic journey easier too. I’m passionate, approachable, and always focused on quality — because I believe every student deserves the chance to excel.

Read more Read less
4.3

2134 reviews

5
1471
4
281
3
168
2
69
1
145

Recently viewed by you

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

Frequently asked questions