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
51 changes: 51 additions & 0 deletions v1/backend/JAVA_LV_2_알고리즘의_굴레/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## 초기 구상

### 1. 상속관계

#### - Algorithm을 인터페이스로 활용하는 클래스들

1. BFS
2. BinarySearch
3. DFS
4. DP
5. TwoPointer

#### -Algorithm 인터페이스 구조

1. isSolution
- 매개변수로 들어온 알고리즘이 현재 객체와 동일한 유형의 객체인지 확인.

2. getSolution
- 알고리즘의 종류별로 알맞은 답을 반환하는 매서드
-> 따라서 각 클래스들은 반환할 답을 프로퍼티로 가지고 있어야 한다.

### 2. Wchae 구성

#### - 프로퍼티

- algorithmMap : 푼 알고리즘과 푼 횟수를 저장하는 HashMap
- unsolvedMessage : 풀지 못한 경우의 반환문

#### - 메서드

1. solveAlgorithm : Algorithm 인터페이스를 매개변수로 받아 알고리즘을 풀었는지에 따라 다른 결과 반환
-> 알고리즘을 처음 map에 넣는 경우 풀 확률을 10%로 두고 저장

### 3. main 구성

#### enum AlgorithmType

- 각 알고리즘별로 정수값과 알고리즘 이름을 묶어서 명시해둠

#### 프로퍼티

- random : 문제를 풀 확률 계산을 위한 난수 생성 객체
- wchae : 문제를 풀 우주 객체
- list : 각 알고리즘을 종류별로 가지고 있는 ArrayList

#### 메서드

1. printAlgorithm : 오늘 어떤 알고리즘을 풀지 고르고 출력하는 메서드
- 하드코딩된 문자열 대신 enum 객체 사용

2. printSolvedMents : 알고리즘 풀이 후 반환된 응답을 올바르게 출력
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,20 @@

public class BFS implements Algorithm {

private final String solution;

BFS() {
solution = "주변부터 둘러보기";
}

@Override // 기능 : 컴파일러가 오타 등을 확인함(상위 클래스에 메서드 존재 확인), 가독성과 유지보수 향상
public boolean isSolution(Algorithm algorithm) {
return (algorithm instanceof BFS);
}

@Override
public String getSolution() {
return (this.solution);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@

public class BinarySearch implements Algorithm {

private final String solution;

BinarySearch() {
solution = "반띵 ㄱㄱ";
}
Comment on lines +5 to +9
Copy link

Choose a reason for hiding this comment

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

Suggested change
private final String solution;
BinarySearch() {
solution = "반띵 ㄱㄱ";
}
private static final String solution = "반띵 ㄱㄱ";

변수로 둘 거라면 상수화를 추천합니다


@Override
public boolean isSolution(Algorithm algorithm) {
return (algorithm instanceof BinarySearch);
}

@Override
public String getSolution() {
return (this.solution);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@

public class DFS implements Algorithm {

private final String solution;

DFS() {
solution = "나는 한 놈씩 조져";
}

@Override
public boolean isSolution(Algorithm algorithm) {
return (algorithm instanceof DFS);
}

@Override
public String getSolution() {
return (this.solution);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,20 @@

public class DP implements Algorithm {

private final String solution;

DP() {
solution = "누가 첫날부터 DP문제를 들고옵니까";
}

@Override // 기능 : 컴파일러가 오타 등을 확인함(상위 클래스에 메서드 존재 확인), 가독성과 유지보수 향상
public boolean isSolution(Algorithm algorithm) {
return (algorithm instanceof DP);
}

@Override
public String getSolution() {
return (this.solution);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
package com.example.algorithmbot;

import java.util.ArrayList;
import java.util.Random;

enum AlgorithmType {
BFS("BFS"),
DFS("DFS"),
BINARY("BinarySearch"),
DP("DP"),
TWOPOINTER("TwoPointer");

private final String type;

AlgorithmType(String type) {
this.type = type;
}

public String getType() {
return type;
}
}
Comment on lines +6 to +22
Copy link

Choose a reason for hiding this comment

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

enum 내부 필드 활용 굿
벗 파일 분리 해.줘.


public class Main {

public static void main(String[] args) {
Random random = new Random();
Wchae wchae = new Wchae();
ArrayList<Algorithm> list = new ArrayList<>();

list.add(new BFS());
list.add(new DFS());
list.add(new BinarySearch());
list.add(new DP());
list.add(new TwoPointer());

for (int i = 0; i < 20; i++) {
int firstAlgorithm, secondAlgorithm;
String firstMsg, secondMsg;

firstAlgorithm = random.nextInt(list.size());
secondAlgorithm = random.nextInt(list.size());
printAlgorithm(firstAlgorithm, secondAlgorithm);
Comment on lines +41 to +43
Copy link

Choose a reason for hiding this comment

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

int보다 랜덤한 AlgorithmType을 뽑은 후 해당 타입을 넘기는게 좋을 것 같아요


firstMsg = wchae.solveAlgorithm(list.get(firstAlgorithm));
secondMsg = wchae.solveAlgorithm(list.get(secondAlgorithm));
printSolvedMents(firstMsg, secondMsg);
}
}

static void printAlgorithm(int firstAlgorithm, int secondAlgorithm) {
AlgorithmType[] algorithmTypes = {AlgorithmType.BFS, AlgorithmType.DFS, AlgorithmType.BINARY, AlgorithmType.DP, AlgorithmType.TWOPOINTER};
Copy link

Choose a reason for hiding this comment

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

Enum 클래스 내부의 static 상수로 옮겨주세요

String DEFAULT = "오늘의 알고리즘 : ";
String DELIMITER = ", ";

System.out.println(DEFAULT + algorithmTypes[firstAlgorithm].getType() + DELIMITER + algorithmTypes[secondAlgorithm].getType());
Copy link

Choose a reason for hiding this comment

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

Suggested change
System.out.println(DEFAULT + algorithmTypes[firstAlgorithm].getType() + DELIMITER + algorithmTypes[secondAlgorithm].getType());
System.out.println("오늘의 알고리즘 : %s, %s", algorithm1.getType(), algorithm2.getType());

처럼 하나의 문자열로 만들어서 넘겨주세요

}

static void printSolvedMents(String firstMsg, String secondMsg) {
String DEFAULT = "우주 : ";
String DELIMITER = " / ";

System.out.println(DEFAULT + firstMsg + DELIMITER + secondMsg);
System.out.println();
Comment on lines +63 to +64
Copy link

Choose a reason for hiding this comment

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

상동

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,20 @@

public class TwoPointer implements Algorithm {

private final String solution;

TwoPointer() {
solution = "너에게 닿기를…";
}

@Override
public boolean isSolution(Algorithm algorithm) {
return (algorithm instanceof TwoPointer);
}

@Override
public String getSolution() {
return (this.solution);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
package com.example.algorithmbot;

import java.util.HashMap;
import java.util.Optional;
import java.util.Random;

public class Wchae {
private final HashMap<Algorithm, Integer> algorithmMap;
private final String unsolvecMessage;
private final Integer HUNDRED_PERCENT;

public Wchae() {
algorithmMap = new HashMap<>();
unsolvecMessage = "스트레스 받네…";
HUNDRED_PERCENT = 10;
}

public String solveAlgorithm(Algorithm algorithm) {
Random random = new Random();

Optional<HashMap.Entry<Algorithm, Integer>> entryOptional = algorithmMap.entrySet().stream()
.filter(entry -> entry.getKey().isSolution(algorithm))
.findAny();

if (entryOptional.isPresent()) {
if (entryOptional.get().getValue() + random.nextInt(HUNDRED_PERCENT) >= HUNDRED_PERCENT) {
algorithmMap.put(algorithm, entryOptional.get().getValue() + 1);
return (algorithm.getSolution());
} else
return (unsolvecMessage);
} else {
algorithmMap.put(algorithm, 1);
return (unsolvecMessage);
}
}
}