코딩 테스트/Level 1

2023 KAKAO BLIND RECRUITMENT - 개인정보 수집 유효기간

컴닥 2023. 1. 5. 21:11
반응형

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

파이썬

짧게 줄인다면.. 

def solution(today, terms, privacies):
    def sum_date(year, month, day, t_month):
        q, month = divmod(month + t_month, 12)
        if month <= 0:
            month += 12
            year -= 1
        return year + q, month, day

    today = tuple(int(each) for each in today.split('.'))
    terms = {k: int(v) for k, v in (term.split() for term in terms)}
    return [index + 1
            for index, (p_date, t_num) in enumerate(privacy.split() for privacy in privacies)
            if today >= sum_date(*(int(each) for each in p_date.split('.')), terms[t_num])]

읽기 편하게 코드를 작성한다면... 

def solution(today, terms, privacies):
    def str2date(text):
        return [int(each) for each in text.split('.')]

    def str_split(text):
        # yield from (each.split() for each in text)
        return (each.split() for each in text)

    def sum_date(date, t_month):
        year, month, day = date
        month += t_month
        q, r = divmod(month, 12)
        year += q
        month = r
        if month <= 0:
            month += 12
            year -= 1
        return [year, month, day]

    today = str2date(today)
    terms = {k: int(v) for k, v in str_split(terms)}
    answer = []
    for index, (p_date, t_num) in enumerate(str_split(privacies)):
        if today >= sum_date(str2date(p_date), terms[t_num]):
            answer.append(index + 1)
    return answer

코틀린

class Solution {
    fun solution(today: String, terms: Array<String>, privacies: Array<String>): IntArray {
        val answer = mutableListOf<Int>()
        val sumDays = { it: List<Int> -> it[0] * 12 * 28 + it[1] * 28 + it[2] }
        val intToday = sumDays(today.split('.').map { it.toInt() })
        val mapTerms = terms
            .map { it.split(' ') }
            .associate { it[0] to it[1].toInt() * 28 }
        for ((index, privacy) in privacies.withIndex()) {
            val temp = privacy.split(' ')
            val privacyDate = sumDays(temp[0].split('.').map { it.toInt() })
            if (intToday >= privacyDate + (mapTerms[temp[1]] ?: 0))
                answer.add(index + 1)
        }
        return answer.toIntArray()
    }
}
class Solution {

    fun solution(today: String, terms: Array<String>, privacies: Array<String>): IntArray {
        val sumDays = { it: List<Int> -> it[0] * 12 * 28 + it[1] * 28 + it[2] }
        val intToday = sumDays(today.split('.').map { it.toInt() })
        val mapTerms = terms
            .map { it.split(' ') }
            .associate { it[0] to it[1].toInt() * 28 }
        return privacies
            .map { it.split(' ') }
            .withIndex()
            .filter { (_, temp) ->
                intToday >= sumDays(temp[0].split('.').map { it.toInt() }) + (mapTerms[temp[1]] ?: 0)
            }.map { it.index + 1 }.toIntArray()
    }
}
반응형