2024_09_24 Kata

SJ.CHO·2024년 9월 24일

알고리즘 Kata

71.

답안 :

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
	public int[] solution(String today, String[] terms, String[] privacies) {
		List<Integer> list = new ArrayList<>();
		Map<Character, Integer> map = new HashMap<>();
		StringBuffer sb = new StringBuffer();
		String[] todayPrats = today.split("\\.");
		int todayYear = Integer.parseInt(todayPrats[0]);
		int todayMonth = Integer.parseInt(todayPrats[1]);
		int todayDay = Integer.parseInt(todayPrats[2]);
		// map 약관맵핑
		for (String item : terms) {
			char c = item.charAt(0);
			String tempMonth = item.substring(2, item.length());
			int x = Integer.parseInt(tempMonth);
			map.put(c, x);
		}
		for (int i = 0; i < privacies.length; i++) {
			String temp = privacies[i].substring(0, privacies[i].length() - 2);
			String[] privaciesParts = temp.split("\\.");
			int privaciesYear = Integer.parseInt(privaciesParts[0]);
			int privaciesMonth = Integer.parseInt(privaciesParts[1]);
			int privaciesDay = Integer.parseInt(privaciesParts[2]);

			int validityPeriod = map.get(privacies[i].charAt(privacies[i].length() - 1));
			privaciesMonth += validityPeriod;
			while (privaciesMonth >= 13) {
				privaciesMonth -= 12;
				privaciesYear++;
			}

			if (todayYear > privaciesYear) {
				list.add(i + 1);
				continue;
			} else if (todayYear == privaciesYear && todayMonth > privaciesMonth) {
				list.add(i + 1);
				continue;
			} else if (todayYear == privaciesYear && todayMonth == privaciesMonth && todayDay >= privaciesDay) {
				list.add(i + 1);
				continue;
			}

		}

		int[] answer = list.stream().mapToInt(Integer::intValue).toArray();
		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		String today = "2020.10.15";
		String[] terms = { "A 100" };
		String[] privacies = { "2018.06.16 A","2008.02.15 A" };
		s.solution(today, terms, privacies);
	}
}
  • 알고리즘 설명 :
    • 주어진 약관을 맵핑하여 각 약관의 유효기간을 저장
    • 각 개인정보 및 현재날짜를 String.spilt() 메소드를 통해 분리하여 년도,월,일자 기준으로 저장
    • 달의 정수범위는 1~100 까지기에 반복문을 사용하여 처리
    • 이후 현재날짜와 만료날짜를 조건에 맞게 비교하여 List에 저잘후 배열로 변환하여 반환.

틀린답안 :

package answer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
	public int[] solution(String today, String[] terms, String[] privacies) {
		List<Integer> list = new ArrayList<>();
		Map<Character, Integer> map = new HashMap<>();
		StringBuffer sb = new StringBuffer();
		today = today.replace(".", "");
		// map 약관맵핑
		for (String item : terms) {
			char c = item.charAt(0);
			String tempMonth = item.substring(2, item.length());
			int x = Integer.parseInt(tempMonth);
			map.put(c, x);
		}
		for (int i = 0; i < privacies.length; i++) {
			char term = privacies[i].charAt(privacies[i].length() - 1);
			String temp = privacies[i].substring(0, privacies[i].length() - 2);
			String[] words1 = temp.split("\\.");
			int month = map.get(term) + Integer.parseInt(words1[1]);
			if (month >= 13) {
				words1[0] = String.valueOf(Integer.parseInt(words1[0]) + 1);
				month -= 12;
				if (month <= 9) {
					words1[1] = "0" + String.valueOf(month);
				} else {
					words1[1] = String.valueOf(month);
				}
			} else {
				if (month <= 9) {
					words1[1] = "0" + String.valueOf(month);
				} else {
					words1[1] = String.valueOf(month);
				}
			}
			for (String item : words1) {
				sb.append(item);
			}
			int x = Integer.parseInt(sb.toString());
			int y = Integer.parseInt(today);
			if (y >= x) {
				list.add(i + 1);
			}
			sb.delete(0, sb.length());
		}
		int[] answer = list.stream().mapToInt(Integer::intValue).toArray();
		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		String today = "2022.05.19";
		String[] terms = { "A 6", "B 12", "C 3" };
		String[] privacies = { "2021.05.02 A", "2021.07.01 B", "2022.02.19 C", "2022.02.20 C" };
		s.solution(today, terms, privacies);
	}
}

  • 알고리즘에대한 접근방법은 맞았으나 문자열의 처리, 달의 정수 최댓값을 제대로 이해하지않고 작성하여 좀 더 컨벤션을 지키고 가독성좋은 코드를 만들고자 다시 리팩토링.

SQL Kata

76.

답안 :

SELECT Year(o1.SALES_DATE) as 'YEAR',Month(o1.SALES_DATE) as 'MONTH',count(distinct u1.USER_ID) as 'PURCHASED_USERS',
Round(COUNT (DISTINCT o1.USER_ID)/(SELECT COUNT (*) FROM USER_INFO WHERE JOINED LIKE '2021%'),1) AS PUCHASED_RATIO
from USER_INFO u1
join ONLINE_SALE o1
on u1.USER_ID=o1.USER_ID
where Year(u1.JOINED)='2021'
group by 1,2
order by 1,2
  • 알고리즘 설명 :
    • SALES_DATE 컬럼에서 연도와 월을 추출.
    • 고유한 구매자수 count(distinct u1.USER_ID) 를 카운트하여 컬럼생성
    • 2021년에 가입한 사용자 수로 나눈 구매자 비율을 계산

77.

  • 테이블 내에서 low_fats 와 recyvlacle 컬럼이 'Y' 인 행을 찾는 문제

답안 :

select product_id
from Products
where low_fats = 'Y' AND recyclable = 'Y'
profile
70살까지 개발하고싶은 개발자

0개의 댓글