-
Notifications
You must be signed in to change notification settings - Fork 5
[java-onboarding-week-1] haon.lee(이민성) 과제 제출합니다. #2
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
base: haon/java-onboarding-week-1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,176 @@ | ||
| package onboarding; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Stream; | ||
|
|
||
|
|
||
| class Problem1 { | ||
| static final int FIRST_PAGE = 1; | ||
| static final int LAST_PAGE = 400; | ||
|
|
||
| public static int solution(List<Integer> pobi, List<Integer> crong) { | ||
| int answer = Integer.MAX_VALUE; | ||
| return answer; | ||
| // 입력 페이지 검증 후 예외 사항일 경우 -1을 리턴한다. | ||
| if (isNotPageValid(pobi) || isNotPageValid(crong)) { | ||
| return -1; | ||
| } | ||
|
|
||
| // 포비의 왼쪽, 오른쪽 페이지 번호를 구한다. | ||
| int pobiLeftPage = pobi.get(0); | ||
| int pobiRightPage = pobi.get(1); | ||
|
|
||
| // 크롱의 왼쪽, 오른쪽 페이지 번호를 구한다. | ||
| int crongLeftPage = crong.get(0); | ||
| int crongRightPage = crong.get(1); | ||
|
|
||
| // 포비의 결과 리스트를 구한다. | ||
| List<Integer> pobiResultList = List.of(getSumValue(pobiLeftPage), getSumValue(pobiRightPage), | ||
| getMultiplyValue(pobiLeftPage), getMultiplyValue(pobiRightPage)); | ||
|
|
||
| int pobiScore = getMaxValue(pobiResultList); | ||
|
|
||
| // 크롱의 결과 리스트를 구한다. | ||
| List<Integer> crongResultList = List.of(getSumValue(crongLeftPage), getSumValue(crongRightPage), | ||
| getMultiplyValue(crongLeftPage), getMultiplyValue(crongRightPage)); | ||
|
|
||
| int crongScore = getMaxValue(crongResultList); | ||
|
|
||
| return compareResult(pobiScore, crongScore); | ||
| } | ||
|
|
||
| /** | ||
| * 입력 페이지에 대한 검증을 진행한다. | ||
| * | ||
| * @param pageNums 사용자의 페이지 번호 리스트 | ||
| * @return 올바르지 않은 페이지 리스트라면 true, 아니라면 false | ||
| */ | ||
| private static boolean isNotPageValid(List<Integer> pageNums) { | ||
| int leftPage = pageNums.get(0); | ||
| int rightPage = pageNums.get(1); | ||
|
|
||
| /* 입력 페이지에 대해서 검증을 진행한다. 다음은 예외 사항으로 간주한다. | ||
| 1. 페이지의 범위가 1~400 사이가 아닌 경우 | ||
| 2. 페이지가 시작이나 마지막 면인 경우 | ||
| 3. 왼쪽 페이지가 홀수가 아니고, 오른쪽 페이지가 짝수가 아닌 경우 | ||
| 4. 왼쪽, 오른쪽 페이지의 차가 1이 아닌 경우 (연속된 페이지가 아님) | ||
| */ | ||
| return isNotPageRange(leftPage, rightPage) || isPageFirstOrLast(leftPage) || | ||
| isNotPageOddAndEven(leftPage, rightPage) || isNotPageContinuous(leftPage, rightPage); | ||
| } | ||
|
|
||
| /** | ||
| * 입력 페이지의 범위가 1~400 사이가 아닌지 판단한다. | ||
| * | ||
| * @param leftPage 왼쪽 페이지 | ||
| * @param rightPage 오른쪽 페이지 | ||
| * @return 1~400 사이가 아니라면 true, 맞다면 false | ||
| */ | ||
| private static boolean isNotPageRange(int leftPage, int rightPage) { | ||
| return leftPage < FIRST_PAGE || leftPage >= LAST_PAGE || rightPage <= FIRST_PAGE || rightPage > LAST_PAGE; | ||
| } | ||
|
|
||
| /** | ||
| * 왼쪽 페이지 번호가 시작 면이거나 마지막 면인지 판단한다. | ||
| * | ||
| * @param leftPage 왼쪽 페이지 | ||
| * @return 왼쪽 페이지의 값이 1이거나 399라면 true, 아니라면 false | ||
| */ | ||
| private static boolean isPageFirstOrLast(int leftPage) { | ||
| return leftPage == FIRST_PAGE || leftPage == LAST_PAGE-1; | ||
| } | ||
|
|
||
| /** | ||
| * 왼쪽 페이지가 홀수, 오른쪽 페이지가 짝수가 아닌지 판단한다. | ||
| * | ||
| * @param leftPage 왼쪽 페이지 | ||
| * @param rightPage 오른쪽 페이지 | ||
| * @return 왼쪽 페이지가 홀수가 아니고 오른쪽 페이지가 짝수가 아니면 true, 아니라면 false | ||
| */ | ||
| private static boolean isNotPageOddAndEven(int leftPage, int rightPage) { | ||
| return leftPage % 2 != 1 && rightPage % 2 != 0; | ||
| } | ||
|
|
||
| /** | ||
| * 왼쪽, 오른쪽 페이지가 연속적이지 않은지 확인한다. | ||
| * | ||
| * @param leftPage 왼쪽 페이지 | ||
| * @param rightPage 오른쪽 페이지 | ||
| * @return 오른쪽, 왼쪽 페이지의 차가 1이 아니라면 true, 아니라면 false | ||
| */ | ||
| private static boolean isNotPageContinuous(int leftPage, int rightPage) { | ||
| return rightPage - leftPage != 1; | ||
| } | ||
|
|
||
| /** | ||
| * 입력 받은 페이지 번호의 각 자리수의 합을 구한다. | ||
| * | ||
| * @param page 페이지 번호 | ||
| * @return 각 자리수의 합 | ||
| */ | ||
| private static int getSumValue(int page) { | ||
| int[] pageArr = getPageValueArray(page); | ||
|
|
||
| return Arrays.stream(pageArr).sum(); | ||
| } | ||
|
|
||
| /** | ||
| * 입력 받은 페이지 번호의 각 자리수의 곱을 구한다. | ||
| * | ||
| * @param page 페이지 번호 | ||
| * @return 각 자리수의 곱 | ||
| */ | ||
| private static int getMultiplyValue(int page) { | ||
| int[] pageArr = getPageValueArray(page); | ||
|
|
||
| int multiVal = 1; | ||
| for(int pageVal : pageArr) { | ||
| multiVal *= pageVal; | ||
| } | ||
|
|
||
| return multiVal; | ||
| } | ||
|
|
||
| /** | ||
| * 입력 받은 페이지의 각 자릿수를 담은 배열을 구한다. | ||
| * | ||
| * @param page 페이지 번호 | ||
| * @return 각 자릿수가 담긴 배열 | ||
| */ | ||
| private static int[] getPageValueArray(int page) { | ||
| String[] digitStrArr = String.valueOf(page).split(""); | ||
|
|
||
| return Stream.of(digitStrArr).mapToInt(Integer::parseInt).toArray(); | ||
| } | ||
|
|
||
| /** | ||
| * 페이지 번호에 따른 자리수 계산 결과 중 가장 큰 값을 리턴한다. | ||
| * | ||
| * @param resultArr 자리수 계산 결과를 담은 리스트 | ||
| * @return 가장 큰 값 | ||
| */ | ||
| private static int getMaxValue(List<Integer> resultArr) { | ||
| int maxVal = 0; | ||
| for(int resultVal : resultArr) { | ||
| maxVal = Math.max(maxVal, resultVal); | ||
| } | ||
|
|
||
| return maxVal; | ||
| } | ||
|
|
||
| /** | ||
| * 두 사람의 점수를 비교하여 결과를 출력한다. | ||
| * | ||
| * @param pobiScore 포비의 점수 | ||
| * @param crongScore 크롱의 점수 | ||
| * @return 포비가 이기면 1, 크롱이 이기면 2, 무승부는 0을 리턴한다. | ||
| */ | ||
| private static int compareResult(int pobiScore, int crongScore) { | ||
| if (pobiScore > crongScore) { | ||
| return 1; | ||
| } | ||
| if (pobiScore < crongScore) { | ||
| return 2; | ||
| } | ||
| return 0; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,26 @@ | ||
| package onboarding; | ||
|
|
||
| import java.util.Stack; | ||
|
|
||
| public class Problem2 { | ||
| public static String solution(String cryptogram) { | ||
| String answer = "answer"; | ||
| return answer; | ||
| Stack<Character> stack = new Stack<>(); | ||
|
|
||
| for (char c : cryptogram.toCharArray()) { | ||
| if (!stack.isEmpty() && stack.peek() == c) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stack을 이용한 점 인상깊었습니다 |
||
| stack.pop(); // 스택의 최상단 요소 제거 | ||
| } else { | ||
| stack.push(c); | ||
| } | ||
| } | ||
|
|
||
| // 스택에 남은 문자들을 합쳐 최종 문자열 반환 | ||
| StringBuilder result = new StringBuilder(); | ||
| for (char c : stack) { | ||
| result.append(c); | ||
| } | ||
|
|
||
| return result.toString(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,22 @@ | |
|
|
||
| public class Problem4 { | ||
| public static String solution(String word) { | ||
| String answer = ""; | ||
| return answer; | ||
| StringBuilder result = new StringBuilder(); | ||
|
|
||
| for (char c : word.toCharArray()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아주 간결하고 좋습니다 해당 방법 저도 습득해야겠습니다 |
||
| if (c >= 'A' && c <= 'Z') { | ||
| // 대문자 변환: 'A' -> 'Z', 'B' -> 'Y' ... 'Z' -> 'A' | ||
| result.append((char) ('Z' - (c - 'A'))); | ||
| } else if (c >= 'a' && c <= 'z') { | ||
| // 소문자 변환: 'a' -> 'z', 'b' -> 'y' ... 'z' -> 'a' | ||
| result.append((char) ('z' - (c - 'a'))); | ||
| } else { | ||
| // 알파벳이 아닌 문자는 그대로 추가 | ||
| result.append(c); | ||
| } | ||
| } | ||
|
|
||
| return result.toString(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,22 @@ | ||
| package onboarding; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Problem5 { | ||
| public static List<Integer> solution(int money) { | ||
| List<Integer> answer = Collections.emptyList(); | ||
| return answer; | ||
| // 화폐 단위를 큰 순서대로 리스트에 저장 | ||
| int[] units = {50000, 10000, 5000, 1000, 500, 100, 50, 10, 1}; | ||
| List<Integer> result = new ArrayList<>(); | ||
|
|
||
| // 각 화폐 단위에 대해 몫을 구하고 나머지를 계산 | ||
| for (int unit : units) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저랑 비슷하게 문제 해결하신 것 같습니다! |
||
| result.add(money / unit); // 화폐 개수 | ||
| money %= unit; // 남은 금액 | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,42 @@ | ||
| package onboarding; | ||
|
|
||
| import java.util.List; | ||
| import java.util.*; | ||
|
|
||
| public class Problem6 { | ||
| public static List<String> solution(List<List<String>> forms) { | ||
| List<String> answer = List.of("answer"); | ||
| return answer; | ||
| Set<String> duplicateEmails = new HashSet<>(); | ||
|
|
||
| // 각 닉네임에 대해 두 글자씩 슬라이딩하면서 부분 문자열을 추출하여 비교 | ||
| for (int i = 0; i < forms.size(); i++) { | ||
| String email1 = forms.get(i).get(0); | ||
| String nickname1 = forms.get(i).get(1); | ||
|
|
||
| for (int j = i + 1; j < forms.size(); j++) { | ||
| String email2 = forms.get(j).get(0); | ||
| String nickname2 = forms.get(j).get(1); | ||
|
|
||
| if (hasCommonSubstring(nickname1, nickname2)) { | ||
| duplicateEmails.add(email1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 굉장히 간결하네요 get을 이렇게도 사용할 수 있군요 |
||
| duplicateEmails.add(email2); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Set에 있는 이메일을 리스트로 변환하고 오름차순 정렬 | ||
| List<String> result = new ArrayList<>(duplicateEmails); | ||
| Collections.sort(result); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| // 두 닉네임에서 2글자 이상의 연속된 공통 부분 문자열이 있는지 확인 | ||
| private static boolean hasCommonSubstring(String nickname1, String nickname2) { | ||
| for (int i = 0; i < nickname1.length() - 1; i++) { | ||
| String substring = nickname1.substring(i, i + 2); | ||
| if (nickname2.contains(substring)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,64 @@ | ||
| package onboarding; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.*; | ||
|
|
||
| public class Problem7 { | ||
| public static List<String> solution(String user, List<List<String>> friends, List<String> visitors) { | ||
| List<String> answer = Collections.emptyList(); | ||
| return answer; | ||
| Map<String, Integer> scoreMap = new HashMap<>(); | ||
| Set<String> userFriends = new HashSet<>(); | ||
|
|
||
| // 사용자와 친구인 사람들 기록 | ||
| for (List<String> pair : friends) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 함수로 따로 빼서 안하신 이유가 있을까용?? |
||
| String friendA = pair.get(0); | ||
| String friendB = pair.get(1); | ||
|
|
||
| if (friendA.equals(user)) { | ||
| userFriends.add(friendB); | ||
| } else if (friendB.equals(user)) { | ||
| userFriends.add(friendA); | ||
| } | ||
| } | ||
|
|
||
| // 함께 아는 친구 점수 계산 | ||
| for (List<String> pair : friends) { | ||
| String friendA = pair.get(0); | ||
| String friendB = pair.get(1); | ||
|
|
||
| if (userFriends.contains(friendA) && !friendB.equals(user)) { | ||
| scoreMap.put(friendB, scoreMap.getOrDefault(friendB, 0) + 10); | ||
| } | ||
|
|
||
| if (userFriends.contains(friendB) && !friendA.equals(user)) { | ||
| scoreMap.put(friendA, scoreMap.getOrDefault(friendA, 0) + 10); | ||
| } | ||
| } | ||
|
|
||
| // 타임라인 방문 점수 계산 | ||
| for (String visitor : visitors) { | ||
| scoreMap.put(visitor, scoreMap.getOrDefault(visitor, 0) + 1); | ||
| } | ||
|
|
||
| // 사용자와 이미 친구인 사람들은 추천 목록에서 제거 | ||
| for (String friend : userFriends) { | ||
| scoreMap.remove(friend); | ||
| } | ||
|
|
||
| // 점수 기준으로 정렬, 점수가 같다면 이름순으로 정렬 | ||
| List<String> result = new ArrayList<>(scoreMap.keySet()); | ||
| result.sort((a, b) -> { | ||
| int scoreCompare = scoreMap.get(b) - scoreMap.get(a); | ||
| if (scoreCompare != 0) { | ||
| return scoreCompare; | ||
| } | ||
| return a.compareTo(b); | ||
| }); | ||
|
|
||
| // 최대 5명까지만 추천 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 세세하게 주석 작성하여 코드작성 잘 하신것 같습니다 ! |
||
| if (result.size() > 5) { | ||
| result = result.subList(0, 5); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left,right 페이지의 sum,multiply값을 합쳐서 리스트를 통해 MAX값을 구하는 점이 인상깊었습니다!