ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 프로그래머스 / 과제 진행하기
    코딩 테스트/Level 2 2023. 4. 1. 13:22
    반응형

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

    파이썬

    def solution(plans):
        timeline = []
        plans2 = map(lambda x: (x[0], int(x[1][0]) * 60 + int(x[1][1]), x[2]),
                     map(lambda x: (x[0], x[1].split(':'), int(x[2])), plans))
        for name, start, playtime in sorted(plans2, key=lambda x: x[1]):
            for index, each in enumerate(timeline):
                if each[1] > start:
                    timeline[index][1] += playtime
            timeline.append([name, start + playtime])
        return list(map(lambda x: x[0], sorted(timeline, key=lambda x: x[1])))

    가독성 좋게(?) 코드를 쓴다면..

    def solution(plans):
        timeline = []
        names = map(lambda x: x[0], plans)
        starts = map(lambda x: int(x[0]) * 60 + int(x[1]), 
                     map(lambda x: x[1].split(':'), plans))
        playtimes = map(lambda x: int(x[2]), plans)
        for name, start, playtime in sorted(
                zip(names, starts, playtimes), key=lambda x: x[1]):
            for index, each in enumerate(timeline):
                if each[1] > start:
                    timeline[index][1] += playtime
            timeline.append([name, start + playtime])
        return list(map(lambda x: x[0], sorted(timeline, key=lambda x: x[1])))

     

    코틀린

    class Solution {
        fun solution(plans: Array<Array<String>>): Array<String> {
            val timeline = mutableListOf<Pair<String, Int>>()
            val plans2 = plans.map {
                Triple(
                    it[0],
                    it[1].split(":").fold(0) { acc, s -> acc * 60 + s.toInt() },
                    it[2].toInt()
                )
            }.sortedBy { it.second }
            for ((name, start, playtime) in plans2) {
                timeline.forEachIndexed { index, each ->
                    if (each.second > start) {
                        timeline[index] = Pair(each.first, each.second + playtime)
                    }
                }
                timeline.add(Pair(name, start + playtime))
            }
            return timeline.sortedBy { it.second }.map { it.first }.toTypedArray()
        }
    }
    class Solution {
        fun solution(plans: Array<Array<String>>): Array<String> {
            val timeline = mutableListOf<Pair<String, Int>>()
            plans.map {
                Triple(it[0], it[1].split(":").let { (h, m) -> h.toInt() * 60 + m.toInt() }, it[2].toInt())
            }.sortedBy { it.second }.forEach { (name, start, playtime) ->
                timeline.forEachIndexed { index, each ->
                    if (each.second > start) timeline[index] = Pair(each.first, each.second + playtime)
                }
                timeline.add(Pair(name, start + playtime))
            }
            return timeline.sortedBy { it.second }.map { it.first }.toTypedArray()
        }
    }
    class Solution {
        fun solution(plans: Array<Array<String>>): Array<String> {
            val timeline = mutableListOf<Pair<String, Int>>()
            plans
                .map {
                    Triple(
                        it[0],
                        it[1].split(":")
                            .let { (h, m) -> h.toInt() * 60 + m.toInt() },
                        it[2].toInt()
                    )
                }
                .sortedBy { it.second }
                .forEach { (name, start, playtime) ->
                    timeline
                        .forEachIndexed { index, each ->
                            if (each.second > start)
                                timeline[index] = Pair(each.first, each.second + playtime)
                        }
                    timeline.add(Pair(name, start + playtime))
                }
            return timeline
                .sortedBy { it.second }
                .map { it.first }
                .toTypedArray()
        }
    }
    반응형
Designed by Tistory.