CS 136 Assignment 8

Due Wednesday November 14, 2012, 11:59am. Half marks for late submissions. No hand-marking.

For this assignment you will use an ordered set ADT ordered-set.rkt which is supplied to you. You will not modify ordered-set.rkt, but you will modify total-order.rkt, which is required by ordered-set.rkt and defines the total order for the ADT. By altering total-order.rkt, you are able to store different types of elements in an ordered set, as well as define how they should be ordered. We make one restriction to your modification: If you choose to modify the to-elem struct inside total-order.rkt, you must make sure it still has a field called data.

Remember that for a total order, we need a set of items and an operation that allows us to order all the items maintaining the properties of totality, antisymmetry, and transitivity. As an example, here is a trivial (i.e. inadequate) test program that requires total-order.rkt and ordered-set.rkt.

The file ordered-set.rkt provides the following functions:

;; In all functions below, e must be a to-elem struct
;; os-empty         the empty set
;; (os-empty? s)    produces true if s is empty, false otherwise
;; (os-singleton e) produces a set with one element e.
;; (os-member? s e)  produces true if e is a member of s; false otherwise
;; (os-union s t)   produces union of s and t in time 
;;                                  O(min(|s|,|t|) log max(|s|,|t|))
;; (os-intersection s t) produces intersection of s and t in time 
;;                                  O(min(|s|,|t|) log max(|s|,|t|))
;; (os-difference s t)   produces difference  s \ t in time 
;;                                  O(min(|s|,|t|) log max(|s|,|t|))
;; (os-min s)       produces the to-min element of s, 
;;                        or to-min-ident if s is empty
;;                        running time:  O(log |s|)
;; (os-max s)       produces the to-max element of s,
;;                        or to-max-ident if s is empty
;;                        running time:  O(log |s|)
;; (os-after s e)   produces the to-min element of s which is to> than e,
;;                        or to-min-ident if there is no such element
;;                        running time:  O(log |s|)
;; (os-before s e)  produces the to-max element of s which is to< than e
;;                        or to-max-ident if there is no such element
;;                        running time:  O(log |s|)
;; (os-op s)          produces the result of applying the binary operator
;;                        to-op to all e in s. If s is empty, returns the to-op-ident
;;                        similar idea to foldl
;;                        running time:  O(1)

You will also need to write a main program that will read from input. Recall that in Racket, we use tail recursion to do repetition (instead of iteration) and will need to use (read). Here is some sample code to get you started. To submit multiple files to Marmoset, you must combine them using zip. For example, to submit total-order.rkt and also main.rkt, execute the following command:

   zip submit.zip main.rkt total-order.rkt
then submit submit.zip to Marmoset.

Assignment 8 Problem 1 [10 marks]. Files: total-order.rkt, main.rkt

Write a program to read a sequence of integers, not necessarily unique, from standard input. Your program should write each distinct integer to standard output, on a separate line, in ascending order. Then it should write each distinct integer to standard output, on a separate line, in descending order. You must use ordered-set.rkt to solve this problem; you may not use lists, vectors, or any other data structure to store the set of integers. The running time of your solution must be O(n log n) where n is the size of the input.

Sample Input

1
3
2
1

Output for Sample Input

1
2
3
3
2
1

Assignment 8 Problem 2 [10 marks]. Files: total-order.rkt, main.rkt

Modify your solution to problem 1 to order the integers by lexicographic order based on the binary list representation as defined by the function in this file. Again, you must use ordered-set.rkt to solve this problem; you may not use lists, vectors, or any other data structure to store the set of integers. You may assume the integers are non-negative.

Sample Input 1

1
3
2
1

Output for Sample Input 1

2
1
3
3
1
2

Sample Input 2

2
4
6

Output for Sample Input 2

4
2
6
6
2
4

Assignment 8 Problem 3 [10 marks]. Files: total-order.rkt, main.rkt

Modify your solution to problem 1 (not problem 2) to write each distinct integer in ascending order, followed by a space, followed by the number of times it occurs in the input, to a separate line of output. You must use ordered-set.rkt to solve this problem; you may not use lists, vectors, or any other data structure to store the set of integers.

Sample Input

1
3
2
1

Output for Sample Input

1 2
2 1
3 1

Assignment 8 Problem 4 [10 marks]. Files: total-order.rkt, scoreboard.rkt

Implement a scoreboard for a Simon game. Your scoreboard should read a sequence of commands from standard input, and produce output as specified by the command:

Sample Input

score Fred 10
score Wilma 20
score Fred 20
highscore
score Betty 30
highscore
best Fred
score Fred 25
best Fred
best Barney

Output for Sample Input

highscore 20
highscore 30
best Fred 20
best Fred 25
best Barney ?
All commands should have running time O(log n) where n is the number of input lines. You must use ordered-set.rkt to solve this problem; you may not use lists, vectors, or any other data structure to store the players' names or scores. Hint: you can solve this problem by defining a suitable total order on a compound value consisting of the player's name and score.

Assignment 8 Problem 5 [10 marks]. Files: total-order.rkt scoreboard.rkt

Augment your solution for problem 4 to implement the additional command

Sample Input

score Fred 10
score Wilma 20
score Fred 20
highscore
score Betty 30
highscore
best Fred
score Fred 25
best Fred
disqualify Betty
highscore
best Betty

Output for Sample Input

highscore 20
highscore 30
best Fred 20
best Fred 25
highscore 25
best Betty ?
All commands should have running time O(log n) where n is the number of input lines. You must use ordered-set.rkt to solve this problem; you may not use lists, vectors, or any other data structure to store the players' names or scores.

Assignment 8 Problem 6 [10 marks]. Files: total-order.rkt, scoreboard.rkt

Augment your solution for problem 5 to implement the additional command

Sample Input

score Fred 10
score Wilma 20
score Fred 20
leaders

Output for Sample Input

leaders Fred Wilma
The running time of leaders must be O(m log n) where m is the number of leaders and n is the number of input lines. You must use ordered-set.rkt to solve this problem; you may not use lists, vectors, or any other data structure to store the players' names or scores.