Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 01f38ce

Browse filesBrowse files
committed
Add coding test exercise
1 parent 4c9fc91 commit 01f38ce
Copy full SHA for 01f38ce

3 files changed

+179Lines changed: 179 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+97Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package exercise;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.junit.Assert.assertThat;
10+
11+
public class CalenderExample {
12+
13+
/*
14+
TASK
15+
2017년 1월 1일은 일요일이다.
16+
2017년 어느날의 월, 일을 입력받아 요일을 반환하는 함수를 구현한다.
17+
18+
2017년 2월은 28일까지 있다.
19+
요일은 월~일을 0~6으로 표현한다.
20+
예를 들어 월요일이면 0을 반환하고, 토요일은 5, 일요일은 6을 반환한다.
21+
*/
22+
23+
@Test
24+
public void test() {
25+
Calendar c = new Calendar();
26+
assertThat(c.getDay(3, 20), is("금요일"));
27+
}
28+
29+
public int getDayOfWeek(int month, int day) {
30+
int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
31+
int count = 0;
32+
33+
for (int i = 1; i < month; i++) {
34+
count += daysInMonth[i];
35+
}
36+
count += day;
37+
count += 5;
38+
return count % 7;
39+
}
40+
41+
public class Calendar {
42+
private Map<Integer, Integer> calender;
43+
44+
public Calendar() {
45+
this.calender = buildCalender();
46+
}
47+
48+
public String getDay(int month, int day) {
49+
switch (getDayIntFormat(month, day)) {
50+
case 0:
51+
return "월요일";
52+
case 1:
53+
return "화요일";
54+
case 2:
55+
return "수요일";
56+
case 3:
57+
return "목요일";
58+
case 4:
59+
return "금요일";
60+
case 5:
61+
return "토요일";
62+
case 6:
63+
return "일요일";
64+
default:
65+
return null;
66+
}
67+
}
68+
69+
private int getDayIntFormat(int month, int day) {
70+
for (Map.Entry<Integer, Integer> entry : calender.entrySet()) {
71+
int sum = 0;
72+
if (entry.getKey() == month) {
73+
return (sum + day + 5) % 7;
74+
}
75+
sum += entry.getValue();
76+
}
77+
return -1;
78+
}
79+
80+
private Map<Integer, Integer> buildCalender() {
81+
Map<Integer, Integer> calender = new HashMap<>();
82+
calender.put(1, 31);
83+
calender.put(2, 28);
84+
calender.put(3, 31);
85+
calender.put(4, 30);
86+
calender.put(5, 31);
87+
calender.put(6, 30);
88+
calender.put(7, 31);
89+
calender.put(8, 31);
90+
calender.put(9, 30);
91+
calender.put(10, 31);
92+
calender.put(11, 30);
93+
calender.put(12, 31);
94+
return calender;
95+
}
96+
}
97+
}
Collapse file
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package exercise;
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 FactorialZeroCount {
9+
10+
/*
11+
TASK
12+
n!의 결과값에서 0의 개수를 구한다.
13+
*/
14+
15+
@Test
16+
public void test() {
17+
18+
assertThat(countZero1(getFactorial(5)), is(1));
19+
assertThat(countZero1(getFactorial(12)), is(2));
20+
assertThat(countZero2(5), is(1));
21+
assertThat(countZero2(12), is(2));
22+
}
23+
24+
public int getFactorial(int num) {
25+
int result = 1;
26+
for (int i = 1; i <= num; i++) {
27+
result *= i;
28+
}
29+
return result;
30+
}
31+
32+
public int countZero1(int num) {
33+
int count = 0;
34+
while (num % 10 == 0) {
35+
num /= 10;
36+
count++;
37+
}
38+
return count;
39+
}
40+
41+
42+
// 5가 얼마나 곱해졌는지가 중요하다.
43+
public int countZero2(int num) {
44+
int count = 0;
45+
for (int i = 5; i <= num; i += 5) {
46+
int base = i;
47+
while (base % 5 == 0) {
48+
base /= 5;
49+
count++;
50+
}
51+
}
52+
return count;
53+
}
54+
}
Collapse file
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package exercise;
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 NoTempSwapTest {
9+
@Test
10+
public void test() {
11+
int a = 4;
12+
int b = 3;
13+
14+
int[] result = swap(a, b);
15+
assertThat(result[0] == 3, is(true));
16+
assertThat(result[1] == 4, is(true));
17+
}
18+
19+
public int[] swap(int a, int b) {
20+
int[] result = new int[2];
21+
a = a + b;
22+
b = a - b;
23+
a = a - b;
24+
result[0] = a;
25+
result[1] = b;
26+
return result;
27+
}
28+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.