有序集符号

时间:2012-03-10 15:54:20

标签: printing scheme

我可以使用以下有序结构实现:

;; os-empty         an empty set
;; (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
;;                        running time:  O(log |s|)
;; (os-max s)       produces the to-max element of s, or to-max-ident
;;                        running time:  O(log |s|)
;; (os-after s e)   produces the to-min element of s which is to> than e
;;                        running time:  O(log |s|)
;; (os-before s e)  produces the to-max element of s which is to< than e
;;                        running time:  O(log |s|)
;; (os-op)          produces the result of applying to-op over all e in s
;;                        running time:  O(1)

可以在以下文件中找到代码:https://www.student.cs.uwaterloo.ca/~cs136/assignments/a8/ordered-set.rkthttps://www.student.cs.uwaterloo.ca/~cs136/assignments/a8/total-order.rkt

该结构不允许重复。这是我的问题:我需要编写一个程序,按升序编写每个不同的整数,然后是一个空格,后跟输入中出现的次数,到另一行输出。

Sample Input

1
3
2
1
Output for Sample Input

1 2
2 1
3 1

这就是我所拥有的:

 ;;Prints in ascending order
    (define (printer e s)  
      (cond
        [(equal? (to-unhide e) +inf.0) (printf "")]
        [else (printf "~a\n" (to-unhide e)) (printer (os-after s e) s)]))


    (define (main) 
     (define x (read))
     (cond
       [(eof-object? x) (printer (os-min o) o)]
       [(os-member o (os-singlton (to-hide x))) ....... <--- What to do?
       [else (set! o (os-union (os-singleton (to-hide x)) o)) (main)]))   


    (main)

我的问题是如何基于x创建一个计数器,以及如何使该计数器对x特别...我正在考虑创建一个产生变量的函数,但我认为这不可能是Scheme。有关如何使用此结构记住输入使用次数的任何建议吗?

1 个答案:

答案 0 :(得分:1)

将您的任务分成更小的块。 这是将任务划分为较小功能的一种方法。

  1. 写入列表 - &gt;有序集:list-of-numbers - &gt;有序集 这会将数字列表转换为有序集。

  2. 写入读数:list-of-strings - &gt;列表的号码 这会将字符串列表转换为数字列表

  3. 写下所有行: - &gt;列表的串 重复使用读取线直到看到eof, 并返回读取的所有行的列表。

  4. 使用打印机(list-&gt; ordered-set(读取数字(read-all-lines)))。