1주차 과제는 전체적으로 코딩 테스트 문제 처럼 간단한 문제가 주어지고 알맞은 답을 출력하도록 하는 프로그래밍을 하는 것이었다.
7문제가 나왔고 난이도는 걱정한 것 보다는 아주 높지는 않았다.
복잡한 알고리즘을 미리 알고 있어야 풀 수 있는 문제는 아니었다.
기능
, 프로그래밍
, 과제 진행
요구사항이 있다.포비가 이겼을 때 1
, 크롱이 이겼을 때 2
, 비겼을 때 0
을 리턴하는 문제가 있었다 평소같으면 다음처럼 코딩을 했을 것이다.// 점수 비교
if (pobiScore > crongScore) {
answer = 1;
} else if (pobiScore < crongScore) {
answer = 2;
} else {
answer = 0;
}
else if(조건)
만 보고 의도를 파악하기 힘들 수 있다.// 점수 비교
if (pobiScore > crongScore) {
answer = 1;
}
if (pobiScore < crongScore) {
answer = 2;
}
if (pobiScore == crongScore) {
answer = 0;
}
가장 일반적인 경우
인 비기는 경우 밖으로 빼주고 Early Return
해주는 식으로 구현을 해보았다.// 점수 비교
if (pobiScore > crongScore) {
return 1;
}
if (pobiScore < crongScore) {
return 2;
}
return 0;
public static String solution(String cryptogram) {
//cryptogram을 검증하는 로직
//cryptogram을 이용하여 문제를 해결하는 로직
//...
}
class Cryptogram {
private String cryptogram;
}
private
으로 선언할 것이다.setter
는 선언하지 않을 것이고, getter
는 웬만해선 선언하지 않을 것이다.class Cyptogram{
private String cryptogram;
public Cryptogram(String cryptogram) {
this.cryptogram = cryptogram;
}
}
class Cyptogram{
private String cryptogram;
public Cryptogram(String cryptogram) {
this.cryptogram = cryptogram;
}
//문자열 길이에 제한사항에 대한 검증
private void validateLength(String cryptogram){
int length = cryptogram.length();
if (length == 0) {
throw new IllegalArgumentException("빈 문자열은 입력할 수 없습니다.");
}
if (length > 1000) {
throw new IllegalArgumentException("1000자 이하로 입력해주세요.");
}
}
//소문자 제한사항에 대한 검증
private void validateLowerCase(String cryptogram) {
for (int i = 0; i < cryptogram.length(); i++) {
if (!Character.isLowerCase(cryptogram.charAt(i))) {
throw new IllegalArgumentException("소문자만 입력할 수 있습니다.");
}
}
}
}
public static String solution(String cryptogram) {
Cryptogram input = new Cryptogram(cryptogram);
// 로직...
}
HashMap<String, List<String>> friendMap = new HashMap<>();
class FriendMap {
private final Map<String, List<String>> friendMap;
public FriendMap(List<List<String>> list) {
validationSize(list);
this.friendMap = new HashMap<>();
// 친구를 서로 이어주는 메소드
setFriendMap(list);
}
private void validationSize(List<List<String>> list) {
if(list.isEmpty()) {
throw new IllegalArgumentException("친구 관계는 비어있을 수 없습니다.");
}
if(list.size() > 10000) {
throw new IllegalArgumentException("친구 관계는 10000개 이하로 입력해주세요.");
}
}
}
class FriendMap {
private final Map<User, List<User>> friendMap;
public FriendMap(List<List<String>> list) {
validationSize(list);
this.friendMap = new HashMap<>();
// 친구를 서로 이어주는 메소드
setFriendMap(list);
}
//검증 로직...
}
List<User>
또한 컬렉션이다. 이 또한 일급 컬렉션으로 만든다.class Friends {
private final List<User> friends;
public Friends() {
friends = new ArrayList<>();
}
public void addFriend(User user) {
this.friends
.add(user);
}
public List<User> getFriends(){
return this.friends;
}
}
class FriendMap {
private final Map<User, Friends> friendMap;
public FriendMap(List<List<String>> list) {
validationSize(list);
this.friendMap = new HashMap<>();
// 친구를 서로 이어주는 메소드
setFriendMap(list);
}
//검증 로직...
}
기능 별로 역할을 명확히 나눴다
는 의미 같다.