Skip to content

Commit 6a0c38f

Browse files
committed
Add recursion algorithm code
1 parent ebc23b4 commit 6a0c38f

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package algorithm.recursion;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class BraceCombination {
9+
10+
/*
11+
TASK
12+
N개 괄호로 만들 수 있는 모든 조합 출력하기.
13+
*/
14+
15+
@Test
16+
public void test() {
17+
assertThat("", is(testFunction()));
18+
}
19+
20+
public String testFunction() {
21+
return "";
22+
}
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package algorithm.recursion;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class Dice {
9+
10+
/*
11+
TASK
12+
주사위로 이동 가능한 경우의 수를 모두 구한다.
13+
*/
14+
15+
@Test
16+
public void test() {
17+
assertThat(4, is(calcDiceCase(3)));
18+
assertThat(8, is(calcDiceCase(4)));
19+
assertThat(16, is(calcDiceCase(5)));
20+
}
21+
22+
public int calcDiceCase(int n) {
23+
if (n < 0) return 0;
24+
if (n == 0) return 1;
25+
return calcDiceCase(n - 1)
26+
+ calcDiceCase(n - 2)
27+
+ calcDiceCase(n - 3)
28+
+ calcDiceCase(n - 4)
29+
+ calcDiceCase(n - 5)
30+
+ calcDiceCase(n - 6);
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package algorithm.recursion;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class Fibonacci {
9+
10+
/*
11+
TASK
12+
Fibonacci 를 계산하는 함수를 작성한다.
13+
*/
14+
@Test
15+
public void test() {
16+
assertThat(5, is(calcFibo(5)));
17+
assertThat(8, is(calcFibo(6)));
18+
assertThat(13, is(calcFibo(7)));
19+
}
20+
21+
public int calcFibo(int num) {
22+
if (num < 2) {
23+
return num;
24+
}
25+
return calcFibo(num - 1) + calcFibo(num - 2);
26+
}
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package algorithm.recursion;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.junit.Assert.assertThat;
10+
11+
public class NBitWays {
12+
13+
/*
14+
TASK
15+
n비트의 모든 경우의 수를 출력한다.
16+
*/
17+
18+
@Test
19+
public void test() {
20+
List<String> actual1 = new ArrayList<>();
21+
actual1.add("00");
22+
actual1.add("01");
23+
actual1.add("10");
24+
actual1.add("11");
25+
assertThat(actual1, is(calcBitCombination(2)));
26+
27+
List<String> actual2 = new ArrayList<>();
28+
actual2.add("000");
29+
actual2.add("001");
30+
actual2.add("010");
31+
actual2.add("011");
32+
actual2.add("100");
33+
actual2.add("101");
34+
actual2.add("110");
35+
actual2.add("111");
36+
assertThat(actual2, is(calcBitCombination(3)));
37+
}
38+
39+
public List<String> calcBitCombination(int n) {
40+
return bitCombination(n, "", new ArrayList<>());
41+
}
42+
43+
private List<String> bitCombination(int n, String str, List<String> list) {
44+
if (n == str.length()) {
45+
list.add(str);
46+
return list;
47+
}
48+
bitCombination(n, str + "0", list);
49+
bitCombination(n, str + "1", list);
50+
return list;
51+
}
52+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package algorithm.recursion;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.junit.Assert.assertThat;
10+
11+
12+
public class Permutation {
13+
14+
/*
15+
TASK
16+
순열을 구한다.
17+
*/
18+
19+
@Test
20+
public void test() {
21+
List<String> actual = new ArrayList<>();
22+
actual.add("123");
23+
actual.add("132");
24+
actual.add("213");
25+
actual.add("231");
26+
actual.add("312");
27+
actual.add("321");
28+
assertThat(actual, is(calcPermutation("123")));
29+
}
30+
31+
public List<String> calcPermutation(String str) {
32+
if (str == null) return null;
33+
return permutation(str, new boolean[str.length()],
34+
"", new ArrayList<>());
35+
}
36+
37+
private List<String> permutation(String str, boolean[] isPick,
38+
String perm, List<String> result) {
39+
if (str.length() == perm.length()) {
40+
result.add(perm);
41+
return result;
42+
}
43+
44+
for (int i = 0; i < str.length(); i++) {
45+
if (isPick[i]) {
46+
continue;
47+
}
48+
isPick[i] = true;
49+
permutation(str, isPick, perm + str.charAt(i), result);
50+
isPick[i] = false;
51+
}
52+
return result;
53+
}
54+
}

0 commit comments

Comments
 (0)