```
백준 20291번 파일정리 Kotlin 구현해 보기
```

이번 글을 통해 배워갈 내용
- 백준 20291번 풀이
https://www.acmicpc.net/problem/
백준 20291번 파일정리는
난이도 실버 등급의 문제로서
파일들의 이름들이 주어지고
파일명확장자들을 카운팅 한 다음 순서대로
출력하면 되는 문재입니다
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
조건에 맞게 카운팅한다음
사전순으로 출력했습니다
import java.util.stream.Collectors
fun main() {
val inputDto = getInput()
print(solution(inputDto))
}
fun getInput(): InputDto {
val fileCnt = readln().toLong()
val fileNames = emptyList<String>().toMutableList()
for (i in 1..fileCnt) {
fileNames.add(readln())
}
return InputDto(fileNames)
}
data class InputDto(
val fileNames: List<String>
)
fun findFileExtensionFrequencies(fileNames: List<String>): String {
val map = emptyMap<String, Int>().toMutableMap()
fileNames.forEach { n ->
run {
val extension = n.split(".")[1]
if (map[extension] == null) {
map[extension] = 1
} else {
map[extension] = map[extension]!! + 1
}
}
}
return map.entries.stream()
.sorted(java.util.Map.Entry.comparingByKey())
.map { m -> "${m.key} ${m.value}" }
.collect(Collectors.joining("\n"))
}
fun solution(dto: InputDto): String {
return findFileExtensionFrequencies(dto.fileNames)
}
// https://codemasterkimc.tistory.com/
테스트
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
internal class MainKtTest {
@Test
fun test() {
assertEquals(
"jpg 2\n" +
"png 1",
solution(
InputDto(listOf("hello.png", "hi.jpg", "kimc.jpg"))
)
)
assertEquals(
"abc 2\n" +
"bbc 1\n" +
"ccc 1",
solution(
InputDto(listOf("hi.bbc", "hello.abc", "hello2.abc", "kimc.ccc"))
)
)
assertEquals(
"icpc 2\n" +
"spc 2\n" +
"txt 3\n" +
"world 1",
solution(
InputDto(
listOf(
"sbrus.txt",
"spc.spc",
"acm.icpc",
"korea.icpc",
"sample.txt",
"hello.world",
"sogang.spc",
"example.txt"
)
)
)
)
}
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
728x90
'Kotlin > Kotlin 알고리즘' 카테고리의 다른 글
| 백준 20920번 영단어 암기는 괴로워 Kotlin 구현해보기 (0) | 2023.03.12 |
|---|---|
| 백준 7795번 먹을 것인가 먹힐 것인가 Kotlin 구현해보기 (0) | 2023.03.11 |
| 백준 11652번 카드 Kotlin 구현해보기 (0) | 2023.03.11 |
| 백준 1015번 수열 정렬 Kotlin 구현해보기 (0) | 2023.03.11 |
| 백준 1182번 부분수열의 합 Kotlin 구현해보기 (1) | 2023.03.11 |