Skip to content

Commit 9148476

Browse files
committed
chapter 17: dynamic programming done
1 parent 6beaa97 commit 9148476

12 files changed

+479
-0
lines changed

dynamicprogramming/src/main/java/KnapsackProblem.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class KnapsackProblem {
88
Write a program for the knapsack problem that selects a
99
subset of items that has maximum value and satisfies
1010
the weight constraint.
11+
12+
Tuple -> (cost,weight)
1113
*/
1214

1315
public static int compute(List<Tuple> items, int capacity) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class BedBathBeyondProblemTest {
11+
12+
private List<String> expected;
13+
private String domain;
14+
private Set<String> dictionary;
15+
16+
@Test
17+
public void decompose1() throws Exception {
18+
expected = Arrays.asList(
19+
"a",
20+
"man",
21+
"a",
22+
"plan",
23+
"a",
24+
"canal"
25+
);
26+
domain = "amanaplanacanal";
27+
dictionary = new HashSet<>(Arrays.asList(
28+
"a",
29+
"man",
30+
"plan",
31+
"canal"
32+
));
33+
34+
test(expected, domain, dictionary);
35+
}
36+
37+
private static void test(List<String> expected, String domain, Set<String> dictionary) {
38+
assertEquals(expected, BedBathBeyondProblem.decompose(domain, dictionary));
39+
}
40+
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import org.junit.Test;
2+
3+
import static org.junit.Assert.*;
4+
5+
public class ComputeBinomialCoefficientsTest {
6+
7+
private int expected;
8+
private int n;
9+
private int m;
10+
11+
@Test
12+
public void compute1() throws Exception {
13+
expected = 10;
14+
n = 5;
15+
m = 2;
16+
17+
test(expected, n, m);
18+
}
19+
20+
@Test
21+
public void compute2() throws Exception {
22+
expected = 850668;
23+
n = 42;
24+
m = 37;
25+
26+
test(expected, n, m);
27+
}
28+
29+
@Test
30+
public void compute3() throws Exception {
31+
expected = 245157;
32+
n = 23;
33+
m = 7;
34+
35+
test(expected, n, m);
36+
}
37+
38+
private void test(int expected, int n, int m) {
39+
assertEquals(expected, ComputeBinomialCoefficients.compute(n, m));
40+
}
41+
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import org.junit.Test;
2+
3+
import static org.junit.Assert.*;
4+
5+
public class ComputeLevenshteinTest {
6+
7+
private int expected;
8+
private String A;
9+
private String B;
10+
11+
@Test
12+
public void levenschteinDistance1() throws Exception {
13+
expected = 4;
14+
A = "Saturday";
15+
B = "Sunday";
16+
17+
test(expected, A, B);
18+
}
19+
20+
@Test
21+
public void levenschteinDistance2() throws Exception {
22+
expected = 2;
23+
A = "book";
24+
B = "back";
25+
26+
test(expected, A, B);
27+
}
28+
29+
@Test
30+
public void levenschteinDistance3() throws Exception {
31+
expected = 9;
32+
A = "fantastic";
33+
B = "excellent";
34+
35+
test(expected, A, B);
36+
}
37+
38+
private void test(int expected, String A, String B) {
39+
assertEquals(expected, ComputeLevenshtein.levenschteinDistance(A,B));
40+
}
41+
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import org.junit.Test;
2+
3+
import static org.junit.Assert.*;
4+
5+
public class CountMovesToClimbStairsTest {
6+
7+
private int expected;
8+
private int top;
9+
private int maximumStep;
10+
11+
@Test
12+
public void numberOfWays1() throws Exception {
13+
expected = 5;
14+
top = 4;
15+
maximumStep = 2;
16+
17+
test(expected, top, maximumStep);
18+
}
19+
20+
@Test
21+
public void numberOfWays2() throws Exception {
22+
expected = 12;
23+
top = 5;
24+
maximumStep = 3;
25+
26+
test(expected, top, maximumStep);
27+
}
28+
29+
private void test(int expected, int top, int maximumStep) {
30+
assertEquals(expected, CountMovesToClimbStairs.numberOfWays(top, maximumStep));
31+
}
32+
33+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import org.junit.Test;
2+
3+
import static org.junit.Assert.*;
4+
5+
public class CountPossibleTraversalsTest {
6+
7+
private int expected;
8+
private int n;
9+
private int m;
10+
11+
@Test
12+
public void numberOfWays1() throws Exception {
13+
expected = 2;
14+
n = 2;
15+
m = 2;
16+
17+
test(expected, n, m);
18+
}
19+
20+
@Test
21+
public void numberOfWays2() throws Exception {
22+
expected = 3;
23+
n = 2;
24+
m = 3;
25+
26+
test(expected, n, m);
27+
}
28+
29+
@Test
30+
public void numberOfWays3() throws Exception {
31+
expected = 20;
32+
n = 3;
33+
m = 4;
34+
35+
test(expected, n, m);
36+
}
37+
38+
@Test
39+
public void numberOfWays4() throws Exception {
40+
expected = 35;
41+
n = 4;
42+
m = 5;
43+
44+
test(expected, n, m);
45+
}
46+
47+
@Test
48+
public void numberOfWays5() throws Exception {
49+
expected = 70;
50+
n = 5;
51+
m = 5;
52+
53+
test(expected, n, m);
54+
}
55+
56+
private void test(int expected, int n, int m) {
57+
assertEquals(expected, CountPossibleTraversals.numberOfWays(n, m));
58+
}
59+
60+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class KnapsackProblemTest {
9+
10+
private int expected;
11+
private List<Tuple> items;
12+
private int capacity;
13+
14+
@Test
15+
public void compute1() throws Exception {
16+
expected = 695;
17+
items = Arrays.asList(
18+
new Tuple(65,20),
19+
new Tuple(35,8),
20+
new Tuple(245,60),
21+
new Tuple(195,55),
22+
new Tuple(65,55),
23+
new Tuple(150,70),
24+
new Tuple(275,85),
25+
new Tuple(155,25),
26+
new Tuple(120,30),
27+
new Tuple(320,65),
28+
new Tuple(75,75),
29+
new Tuple(40,10),
30+
new Tuple(200,95),
31+
new Tuple(100,50),
32+
new Tuple(220,40),
33+
new Tuple(99,10)
34+
);
35+
capacity = 130;
36+
37+
test(expected, items, capacity);
38+
}
39+
40+
private void test(int expected, List<Tuple> items, int capacity) {
41+
assertEquals(expected, KnapsackProblem.compute(items, capacity));
42+
}
43+
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class LongestNondecreasingSubsequenceTest {
9+
10+
private int expected;
11+
private List<Integer> A;
12+
13+
@Test
14+
public void compute1() throws Exception {
15+
expected = 4;
16+
A = Arrays.asList(0,8,4,12,2,10,6,14,1,9);
17+
18+
test(expected, A);
19+
}
20+
21+
@Test
22+
public void compute2() throws Exception {
23+
expected = 5;
24+
A = Arrays.asList(0,8,4,12,2,10,6,14,1,14);
25+
26+
test(expected, A);
27+
}
28+
29+
@Test
30+
public void compute3() throws Exception {
31+
expected = 4;
32+
A = Arrays.asList(0,8,4,12,2,15,6,14,1,9);
33+
34+
test(expected, A);
35+
}
36+
37+
private void test(int expected, List<Integer> A) {
38+
assertEquals(expected, LongestNondecreasingSubsequence.compute(A));
39+
}
40+
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class MaximumCoinsGainTest {
9+
10+
private int expected;
11+
private List<Integer> coins;
12+
13+
@Test
14+
public void computeMaximum1() throws Exception {
15+
expected = 5;
16+
coins = Arrays.asList(1,5);
17+
18+
test(expected, coins);
19+
}
20+
21+
@Test
22+
public void computeMaximum2() throws Exception {
23+
expected = 15;
24+
coins = Arrays.asList(5,1,10,5);
25+
26+
test(expected, coins);
27+
}
28+
29+
@Test
30+
public void computeMaximum3() throws Exception {
31+
expected = 31;
32+
coins = Arrays.asList(10,25,5,1,10,5);
33+
34+
test(expected, coins);
35+
}
36+
37+
private static void test(int expected, List<Integer> coins) {
38+
assertEquals(expected, MaximumCoinsGain.computeMaximum(coins));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)