SW Test/Programmers

[Programmers] Lv1. ์ถ”์–ต์ ์ˆ˜(kotlin)

An effort will never betray ๐Ÿ˜Ž 2023. 4. 13. 23:18
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

 

์˜ˆ์‹œ

 

 

ํ’€์ด

  • name์— ์žˆ์œผ๋ฉด ํ•ด๋‹น ์ ์ˆ˜๋ฅผ ๋”ํ•˜๊ณ  ์—†์œผ๋ฉด 0์œผ๋กœ ๋”ํ•˜๋ฉด ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
class Solution {
    fun solution(name: Array<String>, yearning: IntArray, photo: Array<Array<String>>): IntArray =
        photo.map{ names -> 
            names.foldIndexed(0){ index, acc, i -> 
                acc + if(name.indexOf(i) >= 0) yearning[name.indexOf(i)] else 0
            }
        }.toIntArray()
}

 

 

์ฐธ๊ณ 

 

foldIndexed - Kotlin Programming Language

 

kotlinlang.org

 

๋ฐ˜์‘ํ˜•