https://programmers.co.kr/learn/courses/30/lessons/12910
//테스트코드
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class Quiz1_ArrayOfNumberCanBeDividedTest {
Quiz1_ArrayOfNumberCanBeDivided q;
@BeforeEach
void setUp() {
q = new Quiz1_ArrayOfNumberCanBeDivided();
}
@Test
void test1() {
assertArrayEquals(new int[]{5, 10}, q.solution(new int[]{5, 9, 7, 10}, 5));
}
@Test
void test2() {
assertArrayEquals(new int[]{1, 2, 3, 36}, q.solution(new int[]{2, 36, 1, 3}, 1));
}
@Test
void test3() {
assertArrayEquals(new int[]{-1}, q.solution(new int[]{3, 2, 6}, 10));
}
@Test
void 길이가1인배열() {
assertArrayEquals(new int[]{5}, q.solution(new int[]{5}, 5));
}
}
//프로덕션코드
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Quiz1_ArrayOfNumberCanBeDivided {
public int[] solution(int[] arr, int divisor) {
List<Integer> listOfNumberCanBeDivided = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if (checkNumber(arr[i], divisor)) {
listOfNumberCanBeDivided.add(arr[i]);
}
}
if(listOfNumberCanBeDivided.size() == 0){
return new int[]{-1};
}
Integer[] ArrayOfNumberCanBeDivided = listOfNumberCanBeDivided.toArray(new Integer[0]);
int[] answer = new int[ArrayOfNumberCanBeDivided.length];
for(int i=0; i<ArrayOfNumberCanBeDivided.length; i++){
answer[i] = ArrayOfNumberCanBeDivided[i].intValue();
}
Arrays.sort(answer);
return answer;
}
boolean checkNumber(int number, int divisor) {
if (number % divisor == 0) {
return true;
}
return false;
}
}
//성공! 리팩토링!
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Quiz1_ArrayOfNumberCanBeDivided {
public int[] solution(int[] arr, int divisor) {
List<Integer> listOfNumberCanBeDivided = new ArrayList<>();
setListOfNumberCanBeDivided(arr, divisor, listOfNumberCanBeDivided);
if (listOfNumberCanBeDivided.size() == 0) {
return new int[]{-1};
}
Integer[] ArrayOfNumberCanBeDivided = listOfNumberCanBeDivided.toArray(new Integer[0]);
int[] answer = new int[ArrayOfNumberCanBeDivided.length];
setAnswerFromArrayOfNumberCanBeDivided(ArrayOfNumberCanBeDivided, answer);
Arrays.sort(answer);
return answer;
}
private void setAnswerFromArrayOfNumberCanBeDivided(Integer[] ArrayOfNumberCanBeDivided, int[] answer) {
for (int i = 0; i < ArrayOfNumberCanBeDivided.length; i++) {
answer[i] = ArrayOfNumberCanBeDivided[i].intValue();
}
}
private void setListOfNumberCanBeDivided(int[] arr, int divisor, List<Integer> listOfNumberCanBeDivided) {
for (int i = 0; i < arr.length; i++) {
if (checkNumber(arr[i], divisor)) {
listOfNumberCanBeDivided.add(arr[i]);
}
}
}
boolean checkNumber(int number, int divisor) {
if (number % divisor == 0) {
return true;
}
return false;
}
}
// 성공! 이긴한데 아무리 봐도 이름짓는 센스가 없다... 더 예쁜 이름을 찾으려고 항상 노력하자!
'코딩테스트연습 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 - JAVA] 이상한 문자 만들기 (0) | 2021.12.12 |
---|---|
[프로그래머스 - JAVA] 시저 암호 (0) | 2021.12.08 |
[프로그래머스 - JAVA] 실패율 (0) | 2021.12.03 |
[프로그래머스 - JAVA] 두 개 뽑아서 더하기 (0) | 2021.12.02 |
[프로그래머스 - JAVA] 3진법 뒤집기 (0) | 2021.12.01 |