Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

public class BFS implements Algorithm {

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

@Override
public String getSolution() {
return "주변부터 둘러보기";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

public class BinarySearch implements Algorithm {

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

@Override
public String getSolution() {
return "반띵 ㄱㄱ";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

public class DFS implements Algorithm {

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

@Override
public String getSolution() {
return "나는 한 놈씩 조져";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

public class DP implements Algorithm {

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

@Override
public String getSolution() {
return "누가 첫날부터 DP문제를 들고옵니까";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package com.example.algorithmbot;

import java.util.List;

public class Main {

public static void main(String[] args) {
Wchae wchae = new Wchae();
List<Algorithm> algorithmList = List.of(new BFS(), new DFS(), new TwoPointer(),
new BinarySearch(), new DP());

for (int i = 0; i < 20; i++) {
Algorithm first = algorithmList.get((int) (Math.random() * 5));
Algorithm second = algorithmList.get((int) (Math.random() * 5));
System.out.println(wchae.solveAlgorithm(first));
Copy link
Contributor

Choose a reason for hiding this comment

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

이 코드만 봤을 땐, 출력 조건인
우주 : 알고리즘 / 알고리즘 이런 식으로 출력이 아니라

알고리즘
알고리즘
요렇게 출력 될 것 같은데 제가 맞게 이해한건가여??

Copy link
Author

Choose a reason for hiding this comment

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

출력 형식도 안맞추고 냈네요.. 반성하겠습니다.. 바로 수정!!

System.out.println(wchae.solveAlgorithm(second));
System.out.println();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

public class TwoPointer implements Algorithm {

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

@Override
public String getSolution() {
return "너에게 닿기를...";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package com.example.algorithmbot;

import java.util.HashMap;
import java.util.Map;

public class Wchae {

private final Map<Algorithm, Integer> algorithmMap;

public Wchae() {
algorithmMap = new HashMap<>();
}

public String solveAlgorithm(Algorithm algorithm) {
Integer solveCount = algorithmMap.getOrDefault(algorithm, 0);
// 풀어보지 못한 알고리즘인 경우
if (algorithmMap.keySet()
.stream()
.filter(x -> x.isSolution(algorithm))
.findAny().isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

캬 람다 마스따
하나의 기능을 하고 있으니, 변수로 빼서 의미를 명확히 하는건 어떨까여

Copy link
Author

Choose a reason for hiding this comment

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

좋습니다! 변수로 빼니 가독성이 좋아졌어요!

algorithmMap.put(algorithm, 1);
return "스트레스 받네...";
Copy link

Choose a reason for hiding this comment

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

상수로 빼주세여~(33줄 포함)

}
// 풀어본 알고리즘인 경우
boolean isSolved = Math.random() * 10 > solveCount;
if (isSolved) {
if (solveCount < 10) {
algorithmMap.put(algorithm, solveCount + 1);
}
return algorithm.getSolution();
}
return "스트레스 받네...";
}
}