35-Section-Handout

35-Section-Handout - CS107 Spring 2007 Handout 35 May 23,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 35 Spring 2007 May 23, 2007 Section Handout: All Things Scheme Problem 1: Building Subsets of a Certain Size Implement the k-subsets function, which accepts a set and a non-negative integer k and constructs a list of all those subsets of the incoming set whose size just happens to equal k . Your implementation should run in time that’s proportional to the number of subsets in the final answer. In particular, you should not reuse the power-set implementation from lecture and then filter on length, because that’s entirely too time-consuming when large sets are paired with small values of k . ;; Function: k-subsets ;; ------------------- ;; k-subsets constructs a list of all those subsets ;; of the specified set whose size just happens to equal k. ;; ;; Examples: (k-subsets '(1 2 3 4) 2) -> ((1 2) (1 3) (1 4) (2 3) (2 4) (3 4)) ;; (k-subsets '(1 2 3 4 5 6) 1) -> ((1) (2) (3) (4) (5) (6)) ;; (k-subsets '(a b c d) 0) -> (()) ;; (k-subsets '(a b d d) 5) -> () (define (k-subsets set k)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 2

35-Section-Handout - CS107 Spring 2007 Handout 35 May 23,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online