Skip to content
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
83 changes: 83 additions & 0 deletions src/main/java/onboarding/Problem1.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,89 @@
class Problem1 {
public static int solution(List<Integer> pobi, List<Integer> crong) {
int answer = Integer.MAX_VALUE;

String pleft = String.valueOf(pobi.get(0));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String 으로 하신 이유가 궁금합니다!

String pright = String.valueOf(pobi.get(1));

int pleftNum = Integer.parseInt(pleft); // pleft를 int로 변환
int prightNum = Integer.parseInt(pright); // pright를 int로 변환

if (pleftNum+1 != prightNum){
answer = -1;
return answer;
}

int lsum = 0;
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);
}

Comment on lines +21 to +42
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공통 관심사 및 유사한 코드에 대해 메소드로 분리하면 가독성이 좋아지지 않을까요? 🤔

int pmaxValue = Math.max(Math.max(lsum, lmulti), Math.max(rsum, rmulti));

String cleft = String.valueOf(crong.get(0));
String cright = String.valueOf(crong.get(1));

int cleftNum = Integer.parseInt(cleft); // pleft를 int로 변환
int crightNum = Integer.parseInt(cright); // pright를 int로 변환

if (cleftNum+1 != crightNum){
answer = -1;
return answer;
}

int clsum = 0;
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);
Comment on lines +57 to +70
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전반적으로 모든 문제에서 for 문이 자주 사용되었는데, 가독성 개선을 위해 보통 stream API 기반 코드로 자주 대체하기도 합니다. 이와 관련하여 학습해보셔도 좋을 것 같습니다!

}

int crmulti = 1;
for (int i = 0; i < cright.length(); i++){
char ch = cright.charAt(i);
crmulti *= Character.getNumericValue(ch);
}

int cmaxValue = Math.max(Math.max(clsum, clmulti), Math.max(crsum, crmulti));

System.out.println("pmaxValue"+ pmaxValue+ "cmaxValue"+ cmaxValue);

if (pmaxValue > cmaxValue){
answer = 1;
} else if (pmaxValue < cmaxValue) {
answer = 2;
} else {
answer = 0;
}

Comment on lines +83 to +90
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

객체지향 프로그래밍 관점에서 else 문의 사용을 지양하라 라는 지향 원칙이 있습니다. 이에 관련한 내용 찾아보셔도 좋을 것 같습니다!

return answer;
}
}
40 changes: 40 additions & 0 deletions src/main/java/onboarding/Problem2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,46 @@
public class Problem2 {
public static String solution(String cryptogram) {
String answer = "answer";
String tmp ="";
while (true) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

글자수로 반복문의 제한을 걸지않고 무한루프로 하신 이유가 궁금합니다!

int rememberIndex = 0;
int check = 0;

if (cryptogram.isEmpty()){
break;
}

for (int i = 0; i < cryptogram.length(); i++) {
if (i == cryptogram.length() - 1) {
break;
}
char target = cryptogram.charAt(i);

if (target == cryptogram.charAt(i + 1)) {
check = 1;
rememberIndex = i;
break;
}
}

if (check == 1) {
tmp="";
for (int i = 0, j = 0; i < cryptogram.length(); i++) {
if (i != rememberIndex && i != (rememberIndex+1)) {
tmp = tmp + cryptogram.charAt(i);
}
}

cryptogram=tmp;

}else{
break;
}

check = 0;
}
answer = tmp;

return answer;
}
}
9 changes: 9 additions & 0 deletions src/main/java/onboarding/Problem3.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
public class Problem3 {
public static int solution(int number) {
int answer = 0;

for (int i = 1; i <= number; i++){
String str = String.valueOf(i);
for (int j = 0; j < str.length(); j++){
if (str.charAt(j)=='3' || str.charAt(j)=='6' || str.charAt(j)=='9'){
answer+=1;
}
}
}
return answer;
}
}
16 changes: 16 additions & 0 deletions src/main/java/onboarding/Problem4.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
public class Problem4 {
public static String solution(String word) {
String answer = "";

for (int i = 0; i < word.length(); i++){

int target = (int)word.charAt(i);
if (target>=65 && target<=90){
int subvar = target-65;
int result = 90-subvar;
answer += (char)result;
} else if (target>=97 && target<=122) {
int subvar = target-97;
int result = 122-subvar;
answer += (char)result;
} else{
answer +=(char)target;
}
}
return answer;
}
}
15 changes: 14 additions & 1 deletion src/main/java/onboarding/Problem5.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package onboarding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Problem5 {
public static List<Integer> solution(int money) {
List<Integer> answer = Collections.emptyList();
List<Integer> answer = new ArrayList<>();
List<Integer> list = Arrays.asList(50000, 10000, 5000, 1000, 500, 100, 50, 10, 1);

for (int i = 0; i<list.size(); i++){
if (money>=list.get(i)){
answer.add(money/list.get(i));
money= money%list.get(i);
}else {
answer.add(0);
}
}

return answer;
}
}
52 changes: 50 additions & 2 deletions src/main/java/onboarding/Problem6.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
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");
List<String> answer = new ArrayList<>();

// 일단 forms 순회 - 타겟 선정
for (int i = 0; i<forms.size(); i++){
List<String> target = forms.get(i);
int targetIndex = i;

// 선정한 타겟의 두글자 순회
String targetStr = target.get(1);
for (int t = 0; t<targetStr.length(); t++) {
if (t == targetStr.length()-1){
break;
}
// 두글자 생성
String checkS = "" + targetStr.charAt(t) + targetStr.charAt(t+1);

// 타겟 이외의 것들 순회
for (int j = 0; j < forms.size(); j++) {
if (targetIndex == j) {
continue;
}
List<String> target2 = forms.get(j);
String targetStr2 = target2.get(1);
// 비교할 새로운 두글자 생성
int check = 0;
for (int tt = 0; tt<targetStr2.length(); tt++) {
if (check==1){
answer.add(target2.get(0));
check = 0;
break;
}

if (tt == targetStr2.length() - 1) {
break;
}
// 두글자 생성
String checkSS = "" + targetStr2.charAt(tt) + targetStr2.charAt(tt + 1);

if (checkS.equals(checkSS)){
check = 1;
}
}
}
}
}

Set<String> set = new HashSet<>(answer);
answer = new ArrayList<>(set);

return answer;
}
}
95 changes: 92 additions & 3 deletions src/main/java/onboarding/Problem7.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,100 @@
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();

Map<String, Integer> map = new TreeMap<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TreeMap에 대한 설명혹시 가능할까요??


// user의 친구리스트 생성
List<String> list = new ArrayList<>();

for(int i = 0; i< friends.size(); i++){
List<String> target = friends.get(i);
String target1 = target.get(0);
String target2 = target.get(1);
if (target1.equals(user)){
list.add(target2);
} else if (target2.equals(user)){
list.add(target1);
}
}


// 친구의 친구 조회후 값 추가
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);
}
}
}
Comment on lines +26 to +49
Copy link

@msung99 msung99 Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앞서 말씀 드렸던 내용과 동일한데, else 문을 지양하는게 좋습니다. else 문을 남용하면 자칫 중첩된 여러 조건문이 작성되고, 인덴트(들여쓰기)의 깊이가 늘어날 수 있기 때문입니다! if 절이 중첩되면서 동작흐름 파악이 힘든 Arrow Anti Pattern 이 전형적인 사례입니다.

https://haon.blog/haon/oop/principle-of-daily-gymnastics-front/

참고해보세요 ㅎㅎ

}

for(int v = 0; v<visitors.size(); v++){
String target = visitors.get(v);
if (map.containsKey(target)){
int tmpV = map.get(target);
tmpV+=1;
map.put(target, tmpV);
} else{
map.put(target, 1);
}
}


// user가 속하면 삭제
if(map.containsKey(user)){
map.remove(user);
}

// 친구가 속하면 삭제

for(int i = 0; i<list.size(); i++){
String targetF = list.get(i);
System.out.println("targetF"+ targetF);
if(map.containsKey(targetF)){
map.remove(targetF);
}
}

// map을 value값으로 정렬하기
List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());

entryList.sort((entry1, entry2) -> {
int valueComparison = entry2.getValue().compareTo(entry1.getValue());
if (valueComparison == 0){
return entry1.getKey().compareTo(entry2.getKey());
} else{
return valueComparison;
}
});

Map<String, Integer> sortedMap = new LinkedHashMap<>();
for (Map.Entry<String, Integer> entry : entryList) {
sortedMap.put(entry.getKey(), entry.getValue());
}
Comment on lines +91 to +94
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashMap 대신 LinkedHashMap 을 선택하신 이유가 있나요?? 사용시 어떠한 장단점이 있을까요?


List<String> answer = new ArrayList<>(sortedMap.keySet());

return answer;
}
}