100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
University of British Columbia CPSC 110 LAB 8 $3.49   Add to cart

Exam (elaborations)

University of British Columbia CPSC 110 LAB 8

3 reviews
 281 views  4 purchases
  • Course
  • Institution

University of British Columbia CPSC 110 LAB 8

Preview 2 out of 5  pages

  • October 15, 2021
  • 5
  • 2021/2022
  • Exam (elaborations)
  • Questions & answers

3  reviews

review-writer-avatar

By: andychen3 • 1 year ago

review-writer-avatar

By: dfvdvfdvfdvfrvr • 2 year ago

reply-writer-avatar

By: solutions • 2 year ago

why did you give bad rating?

review-writer-avatar

By: cuci1129 • 2 year ago

reply-writer-avatar

By: solutions • 2 year ago

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

avatar-seller
(@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/

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

Will I be stuck with a subscription?

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

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

76449 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
$3.49  4x  sold
  • (3)
  Add to cart