Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

Similar documents
Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

Homework 12: Tail Recursion and Analysis Due: 11:59 PM, Dec 4, 2018

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

The Design Recipe Fall 2018

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

Homework 4: Hash Tables Due: 5:00 PM, Mar 9, 2018

Lecture 27: (PROVISIONAL) Insertion Sort, Selection Sort, and Merge Sort 10:00 AM, Nov 8, 2017

Homework 5: Fun with Scala Due: 5:00 PM, Mar 16, 2018

The Design Recipe Fall 2017

Repetition Through Recursion

Intro. Scheme Basics. scm> 5 5. scm>

CMSC 201 Fall 2016 Homework 6 Functions

Racket Style Guide Fall 2017

Homework 5. Due Friday, March 1 at 5:00 PM

Lecture 8: Iterators and More Mutation

Lab 1: Setup 12:00 PM, Sep 10, 2017

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

MORE RECURSIVE FUNCTIONS

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

Module 10: Imperative Programming, Modularization, and The Future

How To Clone, Backup & Move Your WordPress Blog! Step By Step Guide by Marian Krajcovic

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

CS115 - Module 9 - filter, map, and friends

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

15-122: Principles of Imperative Computation, Fall 2015

Programming Languages ML Programming Project Due 9/28/2001, 5:00 p.m.

Shorthand for values: variables

Homework 2. Due 4:00pm, Wednesday, February 13, Installation and Handin 2

COMP 161 Lecture Notes 16 Analyzing Search and Sort

(Provisional) Lecture 20: OCaml Fun!

CSCI0170. Today s topics. Predicates Natural Number recursion Recursion Diagrams List recursion A first glance at the design recipe

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Lab 10: Sockets 12:00 PM, Apr 4, 2018

Homework 2: Imperative Due: 5:00 PM, Feb 16, 2018

1 ICS 161: Design and Analysis of Algorithms Lecture notes for January 23, Bucket Sorting

Project 2: Eliza Due: 7:00 PM, Nov 3, 2017

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Homework: More Abstraction, Trees, and Lists

CMSC 201 Spring 2018 Project 2 Battleship

COUNTING AND PROBABILITY

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture 5: Implementing Lists, Version 1

Project #1 Seamcarve

CMSC 201 Spring 2019 Lab 06 Lists

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Homework #5 Algorithms I Spring 2017

COMP 105 Homework: Type Systems

18 Final Submission and Essay

(Refer Slide Time: 01.26)

Black Problem 2: Huffman Compression [75 points] Next, the Millisoft back story! Starter files

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

ALGEBRA Sec. 5 IDENTITY AXIOMS. MathHands.com. IDENTITY AXIOMS: Identities

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Divisibility Rules and Their Explanations

Department of Electrical Engineering and Computer Sciences Spring 2001 Instructor: Dan Garcia CS 3 Midterm #2. Personal Information

CS1 Lecture 3 Jan. 22, 2018

CIS 121 Data Structures and Algorithms with Java Spring 2018

Problem One: A Quick Algebra Review

Lecture 7: Lists, Version 2 (with Mutation)

Assignment: 7. Due: Language level: Allowed recursion:

Module 8: Local and functional abstraction

Welcome to... CS113: Introduction to C

Spring CS Homework 3 p. 1. CS Homework 3

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Fall 2017 December 4, 2017

Recursively Enumerable Languages, Turing Machines, and Decidability

Warm-up and Memoization

CS 275 Name Final Exam Solutions December 16, 2016

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Text Input and Conditionals

YOUR NAME PLEASE: *** SOLUTIONS ***

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

Part I: Written Problems

CS Homework 11 p. 1. CS Homework 11

Laboratory 5: Implementing Loops and Loop Control Strategies

If Statements, For Loops, Functions

CMSC 201 Spring 2018 Project 3 Minesweeper

Transcription:

CS17 Integrated Introduction to Computer Science Klein Contents Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018 1 Bookends (Practice) 2 2 Subsets 3 3 Subset Sum 4 4 k-subsets 5 5 k-subset Sum 5 Objectives By the end of this homework you will be able to: 1. write recursive combinatorial procedures 2. write recursive search procedures How to Hand In For this (and all) homework assignments, you should hand in answers for all the non-practice questions. For this homework specifically, this entails answering the Subsets, Subset Sum, k-subsets, and k-subset Sum questions In order to hand in your solutions to these problems, they must be stored in appropriately-named files. In particular, each should be named for the corresponding problem, as follows (e.g., subsets.rkt corresponds to Subsets): ˆ README.txt ˆ subsets.rkt ˆ subset-sum.rkt ˆ ksubsets.rkt ˆ ksubset-sum.rkt For this assignment, all files you turn in that contain code must be Racket files, so they must end with extension.rkt

For this and every future assignment, you should also have a README.txt file whose first line contains only your Banner ID, and optionally with a message to the person grading explaining anything peculiar about the handin. For example: README.txt: B01234567 There s nothing to say except that I m turning in some files plus this README the way the instructions say that I should. To hand in your solutions to these problems, you must upload them to Gradescope. Do not zip or compress them. If you re-submit your homework, you must re-submit all files. If you choose to also store these files on department machines, all your solution files should reside in your /course/cs0170/homeworks/hw07 directory. Practice 1 Bookends (Practice) Task: Write the procedure bookends, which consumes a list of data, alod, and produces a sublist of alod of length at least 2 whose first and last elements are equal to each other. If no such sublist exists, bookends produces the empty list. (bookends (quote (1 2-3 4 2 0))) => (2-3 4 2) (bookends (quote (a b b c))) => (b b) (bookends (quote (a))) => empty Problems Introduction A set X is a collection of objects in no particular order, and with no repetition. A subset of the set X is a set containing only elements of X, but not necessarily all of its elements. Note that a subset might contain nothing at all. For example, {1, 2, 3} is a set, and its subsets are: {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} In this homework, you will use lists to represent sets. The set {1, 2, 3} can be represented by the list (quote (1 2 3)) or the list (quote (3 1 2)), or any of the other 4 combinations. The set of all subsets of {1, 2, 3} can be represented by a list of lists as follows: 2

(define subsets - 123 (quote (() (2) (3) (1 2) (2 3) (1 3) (1 2 3)))) Remember, sets don t necessarily have to be of integers! For instance, the set of all subsets of (quote (a b c)) can be represented in a similar manner as above. Note: Remember that the empty list is a proper subset of every set and should be included in the list of subsets that you generate. Note: As with all homeworks, you should follow the design recipe for each problem. Testing (important!) We ve substancially emphasized testing in this course specifically, you ve been writing unit tests. That is, testing that individual procedure work as you d expect. We ve also recommended you write tests before starting the coding portion. Imagine a program that for a given input should return 1.235325. Say you write the program, test it after writing it, and it returns 1.235326. It s very easy to say - that looks right to me. It s important to write test cases before coding, so that you can think about exactly what your procedure should output. This strategy has a flaw, and that s the fact that somtimes the exact output of our program may depend on implementation details. Consider the set of subsets of {1, 2}: {}, {1}, {2}, {1, 2} In Racket, we could represent this as (quote (() (1) (2) (1 2))). However, with sets, order doesn t matter. That representation is no more correct than (quote ((2 1) (2) (1) ())). Before writing your procedure, it s hard to know which ordering will be produced. What we could instead do, is write a procedure that checks that two sets of subsets are equivalent so that regardless of your percise implementation, your test cases will prove correctness (or incorrectness). When writing a test case for this homework, you can write something like this, where (subsets...) is a call to the procedure you are writing, and expected-answer is the set of sets you re expecting as a result. (check - expect (set - of - sets - equal? (subsets...) expected - answer) true) This requires a procedure (set-of-sets-equal? sets1 sets2), which we will provide a stencil for. You can download the stencil by clicking here, and fill in the areas where there is an ellipsis. You should include this code in both subsets.rkt and ksubsets.rkt - and use it to test both of those problems as both procedures produce a set of sets as solutions. 3

2 Subsets Task: Write a procedure, subsets, which consumes a set, set, represented as a list, and produces the set of all subsets of set. Note: The order of the subsets produced need not match the order in our examples. (subsets (quote ( - 3 4-5)) => (() ( - 3) (4) ( - 5) ( - 3 4) (4-5) ( - 3-5) ( - 3 4-5)) (subsets (quote (a b)) => (() (a) (b) (a b)) (subsets (quote (1)) => (() (1)) (subsets empty) => (()) Just for Fun: Try out your procedure on sets of length 0 through 10. 3 Subset Sum Task: Write a procedure, subset-sum, which consumes a set of integers, weights, and a target weight, target, and produces true if there exists a subset of weights whose elements sum to target, and false otherwise. Note: Each weight and the target can be positive, negative, or zero. Furthermore, the empty set has weight 0. Hint: What should (subset-sum (quote (1 2 3 4))0) return? (subset - sum (quote (0 2-3 4)) 1) (subset - sum (quote (1 2 3 4)) - 17) => #false (subset - sum (quote (1 17 3 4)) 21) Hint: One solution to this problem is the following: (define (slow - subset - sum weights target) (member? target (map (lambda (alon) (foldr + 0 alon)) (subsets weights)))) This solution first computes all the subsets of weights, then sums all these subsets, and finally checks to see whether target is equal to any of the resulting sums. The disadvantage of this solution 4

is that it wastes a whole lot of space computing all subsets, before checking to see whether any of them sum to target. There is a better solution, one that does not use subsets as a helper, and does not require as much space 1 as slow-subset-sum. The idea underlying the better algorithm is similar to that used to compute subsets: consider whether each weight in weights is part of a subset that sums to target, or not. Just for Fun: Try out your procedure on sets of size 10, 11, 12, and 20. Estimate how long it would take on a list of size 40. 4 k-subsets Task: Write a procedure, k-subsets, which consumes a set, set, and a natural number, k, and produces a list of the subsets of set that are of size k. Note: As in subsets, the order of the subsets produced need not match the order in our examples. Note also that k is a natural number; as such, it cannot be negative, but it can be very large. (k - subsets (quote (1 2)) 0) => (()) (k - subsets (quote (1 2)) 1) => ((1) (2)) (k - subsets (quote ( - 1-2 3)) 3) => (( - 1-2 3)) (k - subsets (quote (2 3 4)) 9) => () (k - subsets (quote (1 2 3 4 5)) 3) => ((3 4 5) (2 4 5) (2 3 5) (2 3 4) (1 4 5) (1 3 5) (1 3 4) (1 2 5) (1 2 4) (1 2 3)) In the next problem, we refer to these subsets as k-element subsets. Hint: One way to solve this problem is to generate all the subsets of set, and then to filter out those of size k. This is not acceptable; for small k, it wastes a large amount of time and space, as the majority of the subsets of set do not have k elements. More Hints: ˆ If k is zero, then set has only one subset of size k the empty set. 1 We ve only used operation-counting as a way to measure performance so far, but the amount of memory a procedure uses also matters in some cases. For our purposes, we can measure this as a combination of two things: (i) how deep our nested function-calling goes (do you have to recur 10 times or 100 times?), and (ii) The maximum, over the execution of a program, of sum of the lengths of all lists in the program at any moment. When you compute all subsets of an n-element list, you ve got a total of 2 n subsets whose average length is about n, so your memory use 2 looks like n 2 2n. By contrast, if you compute and evaluate each subset and then move on to the next, you never have a list with more than n elements. Other approaches can have similar impact on space-use. 5

ˆ Otherwise, if set is empty (and k is not equal to 0), then set has no subsets of size k. ˆ Otherwise (i.e., if set is not empty and k is not equal to 0), there are at least two ways to construct subsets of set of size k. We leave it to you to discover exactly how. Once again, your solution should not use subsets as a helper. 5 k-subset Sum Task: Write a procedure, k-subset-sum, which consumes a set of integers, weights, a natural number, k, and a target weight, target, and produces true if there exists a k-element subset of weights whose elements sum to target, and false otherwise. Note: Once again, as in subset-sum, each weight and the target weight can be positive, negative, or zero. Also, as in k-subsets, k cannot be negative. (k - subset - sum (quote (1 2 3 4)) 2 8) => #false (k - subset - sum (quote (1 2 3 4)) 3 8) (k - subset - sum (quote (1 2 3 4)) 4 11) => #false (k - subset - sum (quote ( - 1 2-3 4)) 2 1) (k - subset - sum (quote ( - 1 2-3 4)) 2-4) Hint: As above, your solution should not use subsets, or any of the other procedures you have already written, as a helper. Instead, try combining ideas from your k-subsets and subset-sum procedures. Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS 17 document by filling out the anonymous feedback form: http://cs.brown.edu/ courses/csci0170/feedback. 6