【发布时间】:2017-08-18 15:06:35
【问题描述】:
我正在关注 cs61a 2015 年春季课程。
方案项目中的一个问题是:
实现 list-partitions 过程,它列出了所有的方法 在不使用连续整数的情况下划分正整数总数。这 每个分区的内容必须按降序排列。 提示:定义一个辅助过程来构造分区。内置追加 过程创建一个包含两个参数列表的所有元素的列表。 questions.scm 中的 cons-all 过程将第一个元素添加到列表列表中的每个列表。
数字 5 有 4 个不包含连续整数的分区:
5
4、1
3、1、1
1、1、1、1、1
以下5个分区不包括在内,因为是连续的 整数:
3、2
2、2、1
2、1、1、1
我找到了一种解决方案,但无法理解
;; List all ways to partition TOTAL without using consecutive numbers.
(define (apply-to-all proc items)
(if (null? items)
'()
(cons (proc (car items))
(apply-to-all proc (cdr items)))))
(define (cons-all first rests)
(apply-to-all (lambda (rest) (cons first rest)) rests))
(define (caar x) (car (car x)))
(define (cadr x) (car (cdr x)))
(define (cddr x) (cdr (cdr x)))
(define (cadar x) (car (cdr (car x))))
(define (cdar x) (cdr (car x)))
(define (partitions-r a b)
(if (= a 0) nil
(append (cons-all a (list-partitions b))
(cons-f (partitions-r (- a 1) (+ b 1))
))
))
(define (cons-f lst)
(cond
((eq? lst nil) nil)
((eq? (cdar lst) nil) lst)
((< (caar lst) (cadar lst)) (cons-f (cdr lst)))
((= (caar lst) (+ 1 (cadar lst))) (cons-f (cdr lst)))
(else (cons (car lst) (cons-f (cdr lst))))
))
(define (list-partitions total)
(cond ((= total 1) '((1)) )
((= total 0) '(()) )
(else (append nil (partitions-r total 0)))
))
; For these two tests, any permutation of the right answer will be accepted.
(list-partitions 5)
; expect ((5) (4 1) (3 1 1) (1 1 1 1 1))
(list-partitions 7)
; expect ((7) (6 1) (5 2) (5 1 1) (4 1 1 1) (3 3 1) (3 1 1 1 1) (1 1 1 1 1 1 1))
功能 partitions-r 和 cons-f 有什么作用?非常感谢!
【问题讨论】:
标签: algorithm numbers scheme decomposition