[java-onboarding-week-1] ivy.lee(이다예) 과제 제출합니다.#5
[java-onboarding-week-1] ivy.lee(이다예) 과제 제출합니다.#5dayaelee wants to merge 7 commits intoJapring-Study:ivy/java-onboarding-week-1from
Conversation
| for (int i = 0; i < pleft.length(); i++){ | ||
| char ch = pleft.charAt(i); | ||
| lsum += Character.getNumericValue(ch); | ||
| } | ||
| int lmulti = 1; | ||
| for (int i = 0; i < pleft.length(); i++){ | ||
| char ch = pleft.charAt(i); | ||
| lmulti *= Character.getNumericValue(ch); | ||
| } | ||
|
|
||
| int rsum = 0; | ||
| for (int i = 0; i < pright.length(); i++){ | ||
| char ch = pright.charAt(i); | ||
| rsum += Character.getNumericValue(ch); | ||
| } | ||
|
|
||
| int rmulti = 1; | ||
| for (int i = 0; i < pright.length(); i++){ | ||
| char ch = pright.charAt(i); | ||
| rmulti *= Character.getNumericValue(ch); | ||
| } | ||
|
|
There was a problem hiding this comment.
공통 관심사 및 유사한 코드에 대해 메소드로 분리하면 가독성이 좋아지지 않을까요? 🤔
| if (pmaxValue > cmaxValue){ | ||
| answer = 1; | ||
| } else if (pmaxValue < cmaxValue) { | ||
| answer = 2; | ||
| } else { | ||
| answer = 0; | ||
| } | ||
|
|
There was a problem hiding this comment.
객체지향 프로그래밍 관점에서 else 문의 사용을 지양하라 라는 지향 원칙이 있습니다. 이에 관련한 내용 찾아보셔도 좋을 것 같습니다!
| for(int j = 0; j<list.size(); j++){ | ||
| for (int jj = 0; jj<friends.size(); jj++) { | ||
| List<String> target = friends.get(jj); | ||
| String target1 = target.get(0); | ||
| String target2 = target.get(1); | ||
|
|
||
| if (list.get(j).equals(target1)) { | ||
| if (map.containsKey(target2)) { | ||
| int tmpV = map.get(target2); | ||
| tmpV += 10; | ||
| map.put(target2, tmpV); | ||
| } else { | ||
| map.put(target2, 10); | ||
| } | ||
| } else if (list.get(j).equals(target2)) { | ||
| if (map.containsKey(target1)) { | ||
| int tmpV = map.get(target1); | ||
| tmpV += 10; | ||
| map.put(target1, tmpV); | ||
| } else { | ||
| map.put(target1, 10); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
앞서 말씀 드렸던 내용과 동일한데, else 문을 지양하는게 좋습니다. else 문을 남용하면 자칫 중첩된 여러 조건문이 작성되고, 인덴트(들여쓰기)의 깊이가 늘어날 수 있기 때문입니다! if 절이 중첩되면서 동작흐름 파악이 힘든 Arrow Anti Pattern 이 전형적인 사례입니다.
https://haon.blog/haon/oop/principle-of-daily-gymnastics-front/
참고해보세요 ㅎㅎ
| for (int i = 0; i < cleft.length(); i++){ | ||
| char ch = cleft.charAt(i); | ||
| clsum += Character.getNumericValue(ch); | ||
| } | ||
| int clmulti = 1; | ||
| for (int i = 0; i < cleft.length(); i++){ | ||
| char ch = cleft.charAt(i); | ||
| clmulti *= Character.getNumericValue(ch); | ||
| } | ||
|
|
||
| int crsum = 0; | ||
| for (int i = 0; i < cright.length(); i++){ | ||
| char ch = cright.charAt(i); | ||
| crsum += Character.getNumericValue(ch); |
There was a problem hiding this comment.
전반적으로 모든 문제에서 for 문이 자주 사용되었는데, 가독성 개선을 위해 보통 stream API 기반 코드로 자주 대체하기도 합니다. 이와 관련하여 학습해보셔도 좋을 것 같습니다!
| Map<String, Integer> sortedMap = new LinkedHashMap<>(); | ||
| for (Map.Entry<String, Integer> entry : entryList) { | ||
| sortedMap.put(entry.getKey(), entry.getValue()); | ||
| } |
There was a problem hiding this comment.
HashMap 대신 LinkedHashMap 을 선택하신 이유가 있나요?? 사용시 어떠한 장단점이 있을까요?
| public static int solution(List<Integer> pobi, List<Integer> crong) { | ||
| int answer = Integer.MAX_VALUE; | ||
|
|
||
| String pleft = String.valueOf(pobi.get(0)); |
| public static String solution(String cryptogram) { | ||
| String answer = "answer"; | ||
| String tmp =""; | ||
| while (true) { |
There was a problem hiding this comment.
글자수로 반복문의 제한을 걸지않고 무한루프로 하신 이유가 궁금합니다!
| public static List<String> solution(String user, List<List<String>> friends, List<String> visitors) { | ||
| List<String> answer = Collections.emptyList(); | ||
|
|
||
| Map<String, Integer> map = new TreeMap<>(); |
No description provided.