코딩테스트연습/프로그래머스

[프로그래머스 - JAVA] 나누어 떨어지는 숫자 배열

:)jun 2021. 12. 5. 22:37

https://programmers.co.kr/learn/courses/30/lessons/12910

 

코딩테스트 연습 - 나누어 떨어지는 숫자 배열

array의 각 element 중 divisor로 나누어 떨어지는 값을 오름차순으로 정렬한 배열을 반환하는 함수, solution을 작성해주세요. divisor로 나누어 떨어지는 element가 하나도 없다면 배열에 -1을 담아 반환하

programmers.co.kr

//테스트코드
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;
    }
}

// 성공! 이긴한데 아무리 봐도 이름짓는 센스가 없다... 더 예쁜 이름을 찾으려고 항상 노력하자!