You are on page 1of 3

5/10/2020 Choosing the Right Decomposition for a Problem | Reading 1: Recursion | 6.005.

2x Courseware | edX

Course  Readings  Readin…  Choosi…

Choosing the Right Decomposition for a Problem


Choosing the Right Decomposition for a Problem
Finding the right way to decompose a problem, such as a method implementation, is
important. Good decompositions are simple, short, easy to understand, safe from
bugs, and ready for change.

Recursion is an elegant and simple decomposition for some problems. Suppose we


want to implement this speci cation:

/**
* @param word consisting only of letters A-Z or a-z
* @return all subsequences of word, separated by commas,
* where a subsequence is a string of letters found in word
* in the same order that they appear in word.
*/
public static String subsequences(String word)

For example, subsequences("abc") might return "abc,ab,bc,ac,a,b,c,". Note the


trailing comma preceding the empty subsequence, which is also a valid subsequence.

This problem lends itself to an elegant recursive decomposition. Take the rst letter of
the word. We can form one set of subsequences that include that letter, and another
set of subsequences that exclude that letter, and those two sets completely cover the
set of possible subsequences.

https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/01-Recursion/?activate_block_id=block-v1%3AMITx%2B6.0… 1/3
5/10/2020 Choosing the Right Decomposition for a Problem | Reading 1: Recursion | 6.005.2x Courseware | edX

1 public static String subsequences(String word) {


2 if (word.isEmpty()) {
3 return ""; // base case
4 } else {
5 char firstLetter = word.charAt(0);
6 String restOfWord = word.substring(1);
7
8 String subsequencesOfRest = subsequences(restOfWord);
9
10 String result = "";
11 for (String subsequence : subsequencesOfRest.split(",", -1)) {
12 result += "," + subsequence;
13 result += "," + firstLetter + subsequence;
14 }
15 result = result.substring(1); // remove extra leading comma
16 return result;
17 }
18 }

Discussion Hide Discussion


Topic: Reading 1 / Choosing the Right Decomposition for a
Problem

Show all posts by recent activity

 For anyone having trouble ...


1
I created [a permalink to a running version of this method][1]. LMK if this helps you out by hitting…

 subsequences "result" string


5
I'm having an embarrassingly hard time following the recursive ow in this example. The 'result' …

 reverse result
7
Why does the example give me the string reverted? subsequences("abc") returns with ",a,b,ab,c,a…

 Solution with no loop(one liner if converted to ternary in helper method)


2
public static String sequence(String s) { return sequence(s, "", 0); } public static String sequence(St…

 How to see the decomposition


5
They give an example of a recursive solution but don't show what are the steps and principles of …

https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/01-Recursion/?activate_block_id=block-v1%3AMITx%2B6.0… 2/3
5/10/2020 Choosing the Right Decomposition for a Problem | Reading 1: Recursion | 6.005.2x Courseware | edX

subsequenceOfRest.split(",", -1)
 4
Why '-1' has been used as a parameter of split() function? I am not clear. Please explain it.

© All Rights Reserved

https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/01-Recursion/?activate_block_id=block-v1%3AMITx%2B6.0… 3/3

You might also like