Last active
March 14, 2018 01:12
-
-
Save dzt/a79cf5348fea243ba8a31879de277f61 to your computer and use it in GitHub Desktop.
Chapter 10 - Recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataSet { | |
private int[] values; | |
private int first; | |
private int last; | |
public DataSet(int[] values, int first, int last) { | |
this.values = values; | |
this.first = first; | |
this.last = last; | |
} | |
public int getMaximum() { | |
if (first > last) | |
return first; | |
else { | |
getMaximum(); | |
} | |
return -1; | |
} | |
public int getSum() { | |
return getSumExec(values.length, 0); | |
} | |
public int getSumExec(int length, int pos) { | |
if (pos >= length) { | |
return 0; | |
} | |
return getSumExec(length, pos + 1) + values[pos]; | |
} | |
public static void main(String[] args) { | |
int[] array = {1, 2, 3}; | |
DataSet ds = new DataSet(array, 1, 3); | |
System.out.println(ds.getSum()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Sentence { | |
private String s; | |
public Sentence(String s) { | |
this.s = s; | |
} | |
public void reverse() { | |
this.s = reverseExec(this.s); | |
} | |
private String reverseExec(String s) { | |
/* | |
Note to self: Backwards stack, string values get appended to final output. | |
Method nest will return proper values once complete in stack. | |
*/ | |
if (s.length() <= 1) { | |
return s; | |
} | |
return reverseExec(s.substring(1, s.length())) + s.charAt(0); | |
} | |
private String reverseItteration(String s) { | |
String text = ""; | |
for(int i = s.length() - 1; i >= 0; i--) { | |
text += s.charAt(i); | |
} | |
return text; | |
} | |
private boolean find(String p) { | |
return findExec(p, 0); | |
} | |
private boolean findExec(String part, int index) { | |
/* | |
Return false To avoid going out of bounds of the substring in following | |
conditional. | |
*/ | |
/* | |
Note to self: true/false can ONLY be called once in recursion process | |
*/ | |
if (s.length() < index + part.length()) { | |
return false; | |
} | |
if (s.substring(index, index + part.length()).equalsIgnoreCase(part)) { | |
System.out.println(index); | |
return true; | |
} | |
return findExec(part, index + 1); | |
} | |
private int indexOf(String p) { | |
return execIndexOf(p, 0); | |
} | |
private int execIndexOf(String part, int index) { | |
if (s.length() < index + part.length()) { | |
return -1; | |
} | |
if (s.substring(index, index + part.length()).equalsIgnoreCase(part)) { | |
return index; | |
} | |
System.out.println("go to method"); | |
return execIndexOf(part, index + 1); | |
} | |
public String getText() { | |
return s; | |
} | |
public static void main(String[] args) { | |
Sentence lol = new Sentence("hello" ); | |
System.out.println("Does it contain \"ll\"?: " + lol.find("ll") + "\n"); | |
System.out.println("Index of \"ll\"?: " + lol.indexOf("ll") + "\n"); | |
//lol.reverse(); | |
System.out.println("getText method after reverse: " + lol.getText()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class SubsetGenerator { | |
private String word; | |
private boolean genCalled; | |
private ArrayList<String> sp = new ArrayList<>(); | |
public SubsetGenerator(String word) { | |
this.word = word; | |
this.genCalled = false; | |
} | |
public ArrayList<String> generate() { | |
return generateExec(0, 0, word.length() - 1); | |
} | |
public ArrayList<String> generateExec(int mode, int index, int extend) { | |
/* | |
Modes: | |
0 - empty output | |
1 - letter combos | |
2 - extend combos up to charAt | |
*/ | |
int newMode = mode; | |
int newIndex = index; | |
int newExtend = extend; | |
// Avoid duplicates in ArrayList by calling the method more than once | |
if (genCalled) return sp; | |
if (word.equalsIgnoreCase("") || word.length() == 0) { | |
sp.add("\"\""); | |
return sp; | |
} | |
if (mode == 0) { | |
sp.add("\"\""); | |
newMode = 1; | |
} | |
if (index == (word.length()) && mode == 1) { | |
newMode++; | |
newIndex = 0; | |
} | |
if (mode == 1 && index < word.length()) { | |
// To avoid repetition in character output | |
if (!sp.contains(word.charAt(index) + "")) { | |
sp.add(word.charAt(index) + ""); | |
} | |
newIndex++; | |
} | |
if (mode == 2) { | |
if (extend == (word.length() - 1) && extend > index) { | |
String portion = word.substring(index, extend); | |
if (!(portion.length() == 1)) { | |
sp.add(portion); | |
} | |
newExtend--; | |
} else if (extend > index) { | |
String portion = word.substring(index, extend); | |
if (!(portion.length() == 1)) { | |
sp.add(portion); | |
} | |
if (extend == (index + 1)) { | |
newExtend = 0; | |
newIndex++; | |
} else { | |
newExtend--; | |
} | |
} else if (index == (word.length() - 1) && extend == (index)) { | |
if (!(word.substring(index, index + 1).length() == 1)) { | |
sp.add(word.substring(index, index + 1)); | |
} | |
return sp; | |
} else { | |
newIndex++; | |
newExtend = word.length() - 1; | |
} | |
} | |
return generateExec(newMode, newIndex, newExtend); | |
} | |
public static void main(String[] args) { | |
SubsetGenerator g = new SubsetGenerator("skateboarding"); | |
System.out.println(g.generate()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment