Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Problem1 풀이 #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/PROBLEM6.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@
| forms | result |
| --- | --- |
| [ ["[email protected]", "제이엠"], ["[email protected]", "제이슨"], ["[email protected]", "워니"], ["[email protected]", "엠제이"], ["[email protected]", "이제엠"] ] | ["[email protected]", "[email protected]", "[email protected]"] |

---
### 기능 구현 목록

1. 닉네임이 같은지 확인하는 기능
- 닉네임이 2글자 연속으로 같은 크루원이 있는지 확인
- 있을 경우 이메일 리스트에 추가
2. 형식 체크 기능
- 닉네임 형식 체크
- 한글로 되어있는가
- 길이가 1자 이상 20자 미만인가
- 이메일 형식 체크
- `@`와 `.`이 들어가 있는가
- 도메인이 `email.com` 인가
- 크루원이 1명 이상 10,000명 이하인가
65 changes: 64 additions & 1 deletion src/main/java/onboarding/Problem1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,71 @@
import java.util.List;

class Problem1 {

public static int solution(List<Integer> pobi, List<Integer> crong) {
int answer = Integer.MAX_VALUE;
if (!(validatePage(pobi) & validatePage(crong))) {
return -1;
}
int pobiScore = comparePageScore(pobi);
int crongScore = comparePageScore(crong);
return comparePobiAndCrong(pobiScore, crongScore);
}

public static int sum(int page) {
int answer = 0;
String pageStr = Integer.toString(page);
for(int i = 0; i < pageStr.length(); i++) {
answer += Integer.parseInt(pageStr.substring(i, i+1));
}
return answer;
}

public static int multi(int page) {
int answer = 1;
String pageStr = Integer.toString(page);
for(int i = 0; i < pageStr.length(); i++) {
answer *= Integer.parseInt(pageStr.substring(i, i+1));
}
return answer;
}

public static int compareSumAndMulti(int page) {
return Math.max(sum(page), multi(page));
}

public static int comparePageScore(List<Integer> scores) {
int left = compareSumAndMulti(scores.get(0));
int right = compareSumAndMulti(scores.get(1));
return Math.max(left, right);
}

public static int comparePobiAndCrong(int pobi_score, int crong_score) {
if (pobi_score == crong_score) {
return 0;
}
return calculateWinner(pobi_score, crong_score);
}

public static int calculateWinner(int pobi, int crong) {
if (pobi > crong) {
return 1;
}
return 2;
}

public static boolean validatePage(List<Integer> pages) {
// 페이지 숫자가 연속되지 않는 경우
if (pages.get(1) != (pages.get(0) + 1)) {
return false;
}
// 페이지의 시작점이 1 미만이거나 끝페이지가 400 초과일 경우
if (pages.get(0) < 1 || pages.get(1) > 400) {
return false;
}
// 오른쪽 페이지가 짝수가 아닐 경우
if (pages.get(1) % 2 !=0) {
return false;
}
return true;
}
}
58 changes: 58 additions & 0 deletions src/test/java/onboarding/Problem1Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package onboarding;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class Problem1Test {

@Test
void case1() {
List<Integer> pobi = List.of(97, 98);
List<Integer> crong = List.of(197, 198);
int result = 0;
assertThat(Problem1.solution(pobi, crong)).isEqualTo(result);
}

@Test
void case2() {
List<Integer> pobi = List.of(131, 132);
List<Integer> crong = List.of(211, 212);
int result = 1;
assertThat(Problem1.solution(pobi, crong)).isEqualTo(result);
}

@Test
void case3() {
List<Integer> pobi = List.of(99, 102);
List<Integer> crong = List.of(211, 212);
int result = -1;
assertThat(Problem1.solution(pobi, crong)).isEqualTo(result);
}

@Test
void case4() {
List<Integer> pobi = List.of(1, 2);
List<Integer> crong = List.of(219, 220);
int result = 2;
assertThat(Problem1.solution(pobi, crong)).isEqualTo(result);
}

@Test
void case5() {
List<Integer> pobi = List.of(-1, 0);
List<Integer> crong = List.of(401, 402);
int result = -1;
assertThat(Problem1.solution(pobi, crong)).isEqualTo(result);
}

@Test
void case6() {
List<Integer> pobi = List.of(2, 3);
List<Integer> crong = List.of(3, 4);
int result = -1;
assertThat(Problem1.solution(pobi, crong)).isEqualTo(result);
}
}