[프로그래머스(Programmers)][Java,Python] (Lv1) 데이터 분석 (PCCE 기출문제 10번)

728x90

 

https://school.programmers.co.kr/learn/courses/30/lessons/250121

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

1. Java

import java.util.*;
class Solution {
    public int[][] solution(int[][] data, String ext, int val_ext, String sort_by) {
    
        int[][] answer = {};
        
        Map<String,Integer> col_info = new HashMap<>();
        ext_info.put("code",0);
        ext_info.put("date",1);
        ext_info.put("maximum",2);
        ext_info.put("remain",3);
        
        int ext_idx = col_info.get(ext);
        List<int[]> data_selected = new ArrayList<>();
        for ( int i = 0; i < data.length; ++i ) {
            if ( data[i][ext_idx] < val_ext ) {
                data_selected.add(data[i]);
            }
        }
        
        int sort_idx = col_info.get(sort_by);
        /* Comparator<int[]> comparator = new Comparator<int[]>() {
        	@Override
        	public int compare(int[] a1, int[] a2) {
        		return a1[sort_idx] - a2[sort_idx];
        	}
        };
        Comparator<int[]> comparator = (a1, a2) -> (a1[sort_idx] - a2[sort_idx]); */
        // Collections.sort(data_selected, comparator);
        Collections.sort(data_selected, ((a1, a2) -> (a1[sort_idx] - a2[sort_idx])));
        
        answer = new int[data_selected.size()][ext_info.size()];
        for ( int i = 0; i < data_selected.size(); ++i ) {
            for ( int j = 0; j < ext_info.size(); ++j ) {
                answer[i][j] = data_selected.get(i)[j];
            }
        }
        return answer;
    }
}



2. Python

def solution(data, ext, val_ext, sort_by):
    answer = []
    col_info = {"code":0, "date":1, "maximum":2, "remain":3}
    ext_idx = col_info[ext]
    data_selected = []
    for i in range(len(data)) :
        if data[i][ext_idx] < val_ext :
            data_selected.append(data[i])
    sort_idx = col_info[sort_by]
    data_selected.sort(key=lambda answer: answer[sort_idx])
    answer = data_selected
    return answer

 

반응형