Today's Codekata
// 개인정보 수집 유효기간
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
Map<String, Integer> termMap = new HashMap<>();
for (String term : terms) {
String[] split = term.split(" ");
termMap.put(split[0], Integer.parseInt(split[1]));
}
int todayDays = toDays(today);
List<Integer> result = new ArrayList<>();
for (int i = 0; i < privacies.length; i++) {
String[] split = privacies[i].split(" ");
String date = split[0];
String type = split[1];
int collectedDays = toDays(date);
int expireDays = collectedDays + termMap.get(type) * 28 - 1;
if (expireDays < todayDays) {
result.add(i + 1);
}
}
int[] answer = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
answer[i] = result.get(i);
}
return answer;
}
private int toDays(String date) {
String[] split = date.split("\\.");
int year = Integer.parseInt(split[0]);
int month = Integer.parseInt(split[1]);
int day = Integer.parseInt(split[2]);
return year * 12 * 28 + month * 28 + day;
}
}
Split
`split()`은 문자열을 특정 구분자(delimiter) 기준으로 나누어 배열로 반환하는 메서드이다.
`String[] parts = term.split("구분자")`와 같은 구조로 사용된다.
`split()`은 정규표현식을 받기 때문에 위처럼 점(.)을 기준 삼을 땐 `\\.`처럼 이스케이프 처리해야 한다.
# Managers with at Least 5 Direct Reports
SELECT name
FROM Employee
WHERE id IN (
SELECT managerId
FROM Employee
GROUP BY managerId
HAVING COUNT(*) >= 5
);
# Not Boring Movies
SELECT id, movie, description, rating
FROM Cinema
WHERE id % 2 = 1
AND description != 'boring'
ORDER BY rating desc;
# Confirmation Rate
SELECT s.user_id,
ROUND(COALESCE(SUM(c.action = 'confirmed') * 1.0 / COUNT(c.action), 0), 2) AS confirmation_rate
FROM Signups s
LEFT JOIN Confirmations c on s.user_id = c.user_id
GROUP BY s.user_id;
`COALESCE()`는 `null`값을 대체할 때 유용하다. 값이 없으면 정해준 값을 넣어준다. `IFNULL()`과 달리 괄호 안에 (value1, value2, value3, '정해줄 값') 이런 식으로 여러 값을 넣을 수 있고, 널이 아닌 첫 번째 값을 반환한다.
오늘도 코드카타로 기본기를 다졌다. 다가오는 한주 집중하고 몰입하는 내 모습을 그려본다.