Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
chapter 16 done
  • Loading branch information
gardncl committed May 3, 2017
2 parents c55400f + 3aa2ffb commit bcb8da8
Show file tree
Hide file tree
Showing 28 changed files with 903 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Suggested usage:
Having trouble solving a problem?
* The book contains hints that I intentionally omitted from this repository
* The book contains thorough solutions
* My (sometimes commented) solutions are available in the [solutions] branch
* My (sometimes commented) [solutions] are available

This project is open source so please fork it and help me create a supplement to this phenomenal book. Always looking for edge cases.

Expand Down
9 changes: 6 additions & 3 deletions arrays/src/test/java/SudokuCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public void isValidSudoku2() {
Arrays.asList(7,0,0,0,2,0,0,0,6),
Arrays.asList(0,6,0,0,0,0,2,8,0),
Arrays.asList(0,0,0,4,1,9,0,0,5),
Arrays.asList(0,0,0,0,8,0,0,7,9));
Arrays.asList(0,0,0,0,8,0,0,7,9)
);

test(expected, board);
}
Expand All @@ -57,7 +58,8 @@ public void isValidSudoku3() {
Arrays.asList(7,0,0,0,2,0,0,0,6),
Arrays.asList(0,6,0,0,0,0,2,8,0),
Arrays.asList(6,0,0,4,1,9,0,0,5),
Arrays.asList(0,0,0,0,8,0,0,7,9));
Arrays.asList(0,0,0,0,8,0,0,7,9)
);

test(expected, board);
}
Expand All @@ -74,7 +76,8 @@ public void isValidSudoku4() {
Arrays.asList(7,0,0,0,2,0,0,0,6),
Arrays.asList(0,6,0,0,0,0,2,8,0),
Arrays.asList(0,0,0,4,1,9,0,0,5),
Arrays.asList(0,0,0,0,8,0,0,7,9));
Arrays.asList(0,0,0,0,8,0,0,7,9)
);

test(expected, board);
}
Expand Down
21 changes: 21 additions & 0 deletions datastructures/src/main/java/TreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.ArrayList;
import java.util.List;

public class TreeNode {
public List<Edge> edges = new ArrayList<>();

public static class Edge {
public TreeNode root;
public double length;

public Edge(double length) {
this.length = length;
root = new TreeNode();
}

public Edge(TreeNode root, double length) {
this.root = root;
this.length = length;
}
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>hashtables</module>
<module>sorting</module>
<module>binarysearchtrees</module>
<module>recursion</module>
</modules>
<packaging>pom</packaging>

Expand Down
13 changes: 13 additions & 0 deletions recursion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Chapter 16: Recursion

* 16.1 TowersOfHanoi
* 16.2 NQueens
* 16.3 GeneratePermutations
* 16.4 GeneratePowerSet
* 16.5 GenerateSubsetsK
* 16.6 GenerateStrings
* 16.7 GeneratePalindromicDecompositions
* 16.8 GenerateBinaryTrees
* 16.9 SudokuSolver
* 16.10 ComputeGrayCode
* 16.11 ComputeDiameter
29 changes: 29 additions & 0 deletions recursion/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>elements-of-programming-interviews</artifactId>
<groupId>gardncl</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>recursion</artifactId>
<name>Chapter 16: Recursion</name>
<dependencies>
<dependency>
<groupId>gardncl</groupId>
<artifactId>datastructures</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>gardncl</groupId>
<artifactId>utils</artifactId>
<version>1.0</version>
</dependency>
</dependencies>



</project>
15 changes: 15 additions & 0 deletions recursion/src/main/java/ComputeDiameter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class ComputeDiameter {

/*
16.11
The diameter of a tree is defined to be the length
of a longest path in the tree. Design an efficient
algorithm to compute the diameter of a tree.
*/

public static int computeDiameter(TreeNode T) {

return 0;
}
}
16 changes: 16 additions & 0 deletions recursion/src/main/java/ComputeGrayCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.Collections;
import java.util.List;

public class ComputeGrayCode {

/*
16.10
Write a program which takes n as input and returns an n-bit Gray code.
*/

public static List<Integer> grayCode(int numBits) {

return Collections.emptyList();
}
}
18 changes: 18 additions & 0 deletions recursion/src/main/java/GenerateBinaryTrees.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Collections;
import java.util.List;

public class GenerateBinaryTrees {

/*
16.8
Write a program that returns all distinct binary
trees with a specified number of nodes. All node
data must be 0.
*/

public static List<BinaryTree<Integer>> generateAllBinaryTrees(int numNodes) {

return Collections.emptyList();
}
}
16 changes: 16 additions & 0 deletions recursion/src/main/java/GeneratePalindromicDecompositions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.Collections;
import java.util.List;

public class GeneratePalindromicDecompositions {

/*
16.7
Compute all palindromic decompositions of a given string.
*/

public static List<List<String>> palindromicPartitioning(String input) {

return Collections.emptyList();
}
}
18 changes: 18 additions & 0 deletions recursion/src/main/java/GeneratePermutations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Collections;
import java.util.List;

public class GeneratePermutations {

/*
16.3
Write a program which takes as input an array of distinct
integers and generates all permutations of that array. No
permutation of the array may appear more that once.
*/

public static List<List<Integer>> permutations(List<Integer> A) {

return Collections.emptyList();
}
}
17 changes: 17 additions & 0 deletions recursion/src/main/java/GeneratePowerSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.Collections;
import java.util.List;

public class GeneratePowerSet {

/*
16.4
Write a function that takes as input a set and returns its
power set.
*/

public static List<List<Integer>> generatePowerSet(List<Integer> inputSet) {

return Collections.emptyList();
}
}
18 changes: 18 additions & 0 deletions recursion/src/main/java/GenerateStrings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Collections;
import java.util.List;

public class GenerateStrings {

/*
16.6
Write a program that takes as input a number and returns
all the strings with that number of matched pairs of
parens.
*/

public static List<String> generateBalancedParentheses(int numpairs) {

return Collections.emptyList();
}
}
17 changes: 17 additions & 0 deletions recursion/src/main/java/GenerateSubsetsK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.Collections;
import java.util.List;

public class GenerateSubsetsK {

/*
16.5
Write a program which computes all size k subsets of [0,1,...,n],
where k and n are program inputs.
*/

public static List<List<Integer>> combinations(int n, int k) {

return Collections.emptyList();
}
}
18 changes: 18 additions & 0 deletions recursion/src/main/java/NQueens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Collections;
import java.util.List;

public class NQueens {

/*
16.2
Write a program which returns all distinct non-attacking
placements of n queens on an nxn chessboard where n is
the input to the program.
*/

public static List<List<Integer>> nQueens(int n) {

return Collections.emptyList();
}
}
15 changes: 15 additions & 0 deletions recursion/src/main/java/SudokuSolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.util.List;

public class SudokuSolver {

/*
16.9
Implement a Sudoku solver.
*/

public static boolean solveSudoku(List<List<Integer>> partialAssignment) {

return false;
}
}
19 changes: 19 additions & 0 deletions recursion/src/main/java/TowersOfHanoi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class TowersOfHanoi {

/*
16.1
Write a program which prints a sequence of operations that
transfers n rings from one peg to another. You have a third
peg, which is initially empty. The only operation you can
perform is taking a single ring from the top of one peg and
placing it on teh top of another peg. You must never place a
larger ring above a smaller ring.
*/

private static final int NUM_PEGS = 3;

public static void compute(int numRings) {

}
}
22 changes: 22 additions & 0 deletions recursion/src/test/java/ComputeDiameterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.junit.Test;

import static org.junit.Assert.*;

public class ComputeDiameterTest {

private int expected;
private TreeNode T;

@Test
public void computeDiameter1() throws Exception {
expected = 31;
T = TreeNodeUtil.getTreeNode();

test(expected, T);
}

private void test(int expected, TreeNode T) {
assertEquals(expected, ComputeDiameter.computeDiameter(T));
}

}
33 changes: 33 additions & 0 deletions recursion/src/test/java/ComputeGrayCodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

public class ComputeGrayCodeTest {

private List<Integer> expected;
private int numBits;

@Test
public void grayCode1() throws Exception {
expected = Arrays.asList(0,1,2,3);
numBits = 2;

test(expected, numBits);
}

@Test
public void grayCode2() throws Exception {
expected = Arrays.asList(0,1,3,2,6,7,5,4);
numBits = 3;

test(expected, numBits);
}

private void test(List<Integer> expected, int numBits) {
AssertUtils.assertSameContentsInt(expected, ComputeGrayCode.grayCode(numBits));
}

}
Loading

0 comments on commit bcb8da8

Please sign in to comment.