Skip to main content

University of British Columbia CPSC 110 Assignment 7 Phase 1

Page 1

;; Problem Set 7 Phase 1 Solution (THIS IS A TWO-PHASE PEER GRADED PROBLEM)

NOTE! The following applies to each of the problems in phase one of the problem set: You are required to design each function as a 2-one-of problem. Provide signature, purpose, stub, cross-product of types table (filled using your examples (see next paragraph)), and examples. DO NOT IMPLEMENT THE FUNCTION. You must include a properly formed cross-product of types comment table to receive credit for each problem. You must render it as text in a comment box. It should come after the purpose. You may find it helpful to draw your cross-product on paper for your design and then use a tool like http://www.asciiflow.com/#Draw to help you render it. Use your examples to fill in the cross product of types table. Remember, the content of the table does not need to be fully-formed code. See the videos and the 2-One-Of practice problem solutions for examples of what is acceptable. After you have filled in the table, you should simplify it. As part of the simplification, assign a number to each subclass that produces different answers. Consult the Coursera practice problem solutions for examples of this. Problem 1: Suppose you have rosters for players on two opposing tennis teams, and each roster is ordered by team rank, with the best player listed first. When both teams play, the best players of each team play one another, and the second-best players play one another, and so on down the line. When one team has more players than the other, the lowest ranking players on the larger team do not play. In this problem you will begin to design a function that takes two lists of players and produces a list of matches, according to the approach described above. DO NOT IMPLEMENT THE FUNCTION. ;; Player is String ;; interp. the name of a tennis player. (define P0 "Maria") (define P2 "Serena") #; (define (fn-for-player p) (... p))

;; Roster is one of: ;; - empty ;; - (cons Player Roster) ;; interp. a team roster, ordered from best player to worst. (define R0 empty) (define R1 (list "Eugenie" "Gabriela" "Sharon" "Aleksandra")) (define R2 (list "Maria" "Nadia" "Elena" "Anastasia" "Svetlana")) #; (define (fn-for-roster r) (cond [(empty? r) (...)] [else (... (fn-for-player (first r))


(fn-for-roster (rest r)))]))

(define-struct match (p1 p2)) ;; Match is (make-match Player Player) ;; interp. a match between player p1 and player p2, with same team rank (define M0 (make-match "Eugenie" "Maria")) (define M1 (make-match "Gabriela" "Nadia")) #; (define (fn-for-match m) (... (match-p1 m) (match-p2 m)))

;; ListOfMatch is one of: ;; - empty ;; - (cons Match ListOfMatch) ;; interp. a list of matches between one team and another. (define LOM0 empty) (define LOM1 (list (make-match "Eugenie" "Maria") (make-match "Gabriela" "Nadia"))) #; (define (fn-for-lom lom) (cond [(empty? lom) (...)] [else (... (fn-for-match (first lom)) (fn-for-lom (rest lom)))]))

Solution: ;; Roster Roster -> ListOfMatch ;; given two team rosters produce a list of matches for all pairwise ranks.


There are three acceptable cross-product table solutions: ╔════════════════════╦═══════════╦══════════════════════════════════╗ ║ ║ ║ ║ ║ r2║ empty ║ (cons Player Roster) ║ ║ r1 ║ ║ ║ ╠════════════════════╬═══════════╩══════════════════════════════════╣ ║ ║ ║ ║ ║ ║ ║ empty ║ empty (** 1 **) ║ ║ ║ ║ ║ ║ ║ ╠════════════════════╣ ╔══════════════════════════════════╣ ║ ║ ║ ║ ║ ║ ║ (** 2 **) ║ ║(cons Player Roster)║ ║ (cons (make-match <firsts>) ║ ║ ║ ║ (set-matches <rests>)) ║ ║ ║ ║ ║ ║ ║ ║ ║ ╚════════════════════╩═══════════╩══════════════════════════════════╝ ╔════════════════════╦═══════════╦══════════════════════════════════╗ ║ ║ ║ ║ ║ r2║ empty ║ (cons Player Roster) ║ ║ r1 ║ ║ ║ ╠════════════════════╬═══════════╬══════════════════════════════════╣ ║ ║ ║ ║ ║ ║ ║ (** 2 **) ║ ║ empty ║ empty ║ empty ║ ║ ║ (** 1 **) ║ ║ ║ ║ ║ ║ ╠════════════════════╣ ╠══════════════════════════════════╣ ║ ║ ║ ║ ║ ║ ║ (** 3 **) ║ ║(cons Player Roster)║ ║ (cons (make-match <firsts>) ║ ║ ║ ║ (set-matches <rests>)) ║ ║ ║ ║ ║ ║ ║ ║ ║ ╚════════════════════╩═══════════╩══════════════════════════════════╝ ╔════════════════════╦═══════════╦══════════════════════════════════╗ ║ ║ ║ ║ ║ r2║ empty ║ (cons Player Roster) ║ ║ r1 ║ ║ ║ ╠════════════════════╬═══════════╩══════════════════════════════════╣ ║ ║ ║ ║ ║ ║ ║ empty ║ empty (** 1 **) ║ ║ ║ ║ ║ ║ ║ ╠════════════════════╬═══════════╦══════════════════════════════════╣ ║ ║ ║ ║ ║ ║ (** 2 **) ║ (** 3 **) ║ ║(cons Player Roster)║ empty ║ (cons (make-match <firsts>) ║ ║ ║ ║ (set-matches <rests>)) ║ ║ ║ ║ ║ ║ ║ ║ ║ ╚════════════════════╩═══════════╩══════════════════════════════════╝

(check-expect (set-matches empty empty) empty) (check-expect (set-matches (list "Eugenie") empty) empty) (check-expect (set-matches empty (list "Maria")) empty) (check-expect (set-matches (list "Eugenie") (list "Maria")) (list (make-match "Eugenie" "Maria")))


(check-expect (set-matches (list "Eugenie" "Gabriela") (list "Maria" "Nadia")) (list (make-match "Eugenie" "Maria") (make-match "Gabriela" "Nadia"))) (check-expect (set-matches (list "Eugenie" "Gabriela" "Sharon") (list "Maria" "Nadia")) (list (make-match "Eugenie" "Maria") (make-match "Gabriela" "Nadia"))) (check-expect (set-matches (list "Eugenie" "Gabriela") (list "Maria" "Nadia" "Elena")) (list (make-match "Eugenie" "Maria") (make-match "Gabriela" "Nadia"))) (define (set-matches r1 r2) empty) ; stub Problem 2: In the game 20 questions, there are two players. The first player (the "answerer") thinks of an object. Then the second player (the "questioner") asks a series yes-no questions, and the answerer answers each of them. After the questioner thinks that she has asked enough questions, she guesses the identity of the object that the answerer had in mind. We can model this scenario using a binary tree. Here is such a tree from a second-year computer science class

http://www.cs.duke.edu/courses/cps100/fall12/assignments/20q/ The nodes at the bottom of the tree (the "leaves") represent guesses to the identity of the object. The other nodes correspond to questions. The left child of a question node describes what the questioner will do if the answerer responds "yes" to the question: the right child describes the response to a "no" answer. In general, a tree of questions-and-answers is called a decision tree. Begin to design a function that takes a decision tree and a number and determines whether there is a path in the tree that arrives at an answer after exactly n questions. DO NOT IMPLEMENT THE FUNCTION. (define-struct yntree (s y n)) ;; YesNoTree is one of:


;; - String ;; - (make-yntree String YesNoTree YesNoTree) ;; interp. a yes-no question tree where: ;; + a string s is an answer ;; + (make-yntree s y n) represents a question s, with yes-decision y ;; and no decision n (define YNT0 "Raven") (define YNT1 (make-yntree "Does it gobble?" "Turkey" (make-yntree "Does it say 'Nevermore'?" "Raven" "Eagle"))) #; (define (fn-for-ynt ynt) (cond [(string? ynt) (... ynt)] [else (... (yntree-s ynt) (fn-for-ynt (yntree-y ynt)) (fn-for-ynt (yntree-n ynt)))])) Solution: ;; YesNoTree Natural -> Boolean ;; produce true if n questions can reach an answer in the tree otherwise false


Turn static files into dynamic content formats.

Create a flipbook