【问题标题】:Output Elements in List That Are Not Incommon列表中不常见的输出元素
【发布时间】:2015-04-09 03:53:29
【问题描述】:

我创建了一个函数,它应该返回两个列表没有共同点的元素。目前,他们正在准确地输出传递给它的内容。有关如何解决此问题的任何建议?

(define (findDifference lst1 lst2)
    (if (null? lst1) lst2
    (cons (car lst1) (findDifference (cdr lst1) lst2))))

(findDifference '(2 3 4 (2 3) 2 (4 5)) '(2 4 (4 5))

电流输出:(2 3 4 (2 3) 2 (4 5) 2 4 (4 5)) 所需输出:(3 (2 3))

【问题讨论】:

  • 你可以通过暴力破解:遍历第一个列表,检查第二个列表是否包含该元素,如果没有,将其添加到结果中。然后对第二个列表进行相同的处理。请注意,此算法在指数时间内运行。优化可能是为每个元素创建两个哈希集,这将导致分摊的顺序时间。
  • PS:您正在寻找的术语是“对称差异”。
  • @ChristopheDeTroyer 我是新来的计划,所以不知道如何处理这个问题。你能扩展吗?谢谢!

标签: scheme


【解决方案1】:

您要的是两个列表中的symmetric difference。试试这个:

(define (diff list1 list2)
  (union (complement list1 list2)
         (complement list2 list1)))

使用以下帮助程序:

(define (union list1 list2)
  (cond ((null? list1) list2)
        ((member (car list1) list2) (union (cdr list1) list2))
        (else (cons (car list1) (union (cdr list1) list2)))))

(define (complement list1 list2)
  (cond ((null? list1) '())
        ((member (car list1) list2) (complement (cdr list1) list2))
        (else (cons (car list1) (complement (cdr list1) list2)))))

另外请注意,如果您使用的是 Racket,您可以简单地使用内置的set-symmetric-difference 过程来获得相同的效果。例如:

(diff '(2 3 4 (2 3) 2 (4 5)) '(2 4 (4 5)))
=> '(3 (2 3))

【讨论】:

  • 我对如何调用它有点困惑。我尝试通过使用列表的差异、联合和补充来调用它。
  • 是的,我试过了。我收到以下错误:“错误:执行:未绑定符号:“空?”[补码,补码,差异,补码,并集,差异,补码,补码,差异,补码]”
  • 太棒了,成功了。谢谢,真的很感激!
  • 为了完整起见:在这个post 中,只要可以比较元素(不是你的情况,因为元素是列表),有一个快速实现可以很好地用于排序列表。跨度>
【解决方案2】:

因为它看起来像家庭作业,我不想破坏乐趣,这里是蛮力算法,省略了一些。如果你真的被卡住了,我会给你完整的源代码。

(define (sym-diff xs ys)
  ;; Since we have the helper function we can determine all the elements that are in the first list, 
  ;; but not in the second list.
  ;; Then we can pass this intermediate result to the second call to sym-diff-helper. 
  ;;This will return us all the elements that are in the second list but not the first.
  (let ((in-first-not-second ...))
    (sym-diff-helper ys xs in-first-not-second)))

;; This function will return all the elements from the first list that are not in the second list!
(define (sym-diff-helper xs ys acc)
  (cond
    ;; If the first list is empty we have checked it.
    (...
     acc)
    ;; If the first list is not empty yet, check if the first element 
    ;; is in the second list.
    ;; If so, discard it and continue with the rest of the list.
    ((member ... ...)
     (sym-diff-helper ... ... ...)
    ;; If the first element of the first list is not in the second list, 
    ;; add it to the accumulator and continue with the rest of the list.
    (else
     (sym-diff-helper ... ... ...)))

(sym-diff-helper '(1 2 3) '(2 3 4) '())
;; == (1)
(sym-diff-helper '(1 2 (3 4) 5) '(2 3 4) '())  
;; == (5 (3 4) 1)


(sym-diff '(2 3 4 (2 3) 2 (4 5)) '(2 4 (4 5)))
;; == ((2 3) 3)

请注意,我选择使用member。还有一些其他的搜索功能,但它们不太适合这种情况。因此,我把它留在那里。有关搜索功能的更多信息可以在这里找到:http://docs.racket-lang.org/reference/pairs.html#%28part..List.Searching%29

【讨论】:

  • 谢谢,我看看能不能从这里弄到!
  • 我相信你会找到的!祝你好运!
猜你喜欢
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 2010-12-03
  • 2018-09-27
相关资源
最近更新 更多