-
Exercism - Largest Series Product (With.Kotlin)문제풀이/Exercism 2019. 8. 21. 20:51반응형
[Instructions]
Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.
For example, for the input '1027839564', the largest product for a series of 3 digits is 270 (9 * 5 * 6), and the largest product for a series of 5 digits is 7560 (7 * 8 * 3 * 9 * 5).
Note that these series are only required to occupy adjacent positions in the input; the digits need not be numerically consecutive.
For the input '73167176531330624919225119674426574742355349194934', the largest product for a series of 6 digits is 23520.
[Solution1]
class Series(str: String) { private val numbers = str init { str.toCharArray() .map { c -> c.isLetter() } .find { it }?.let { require(!it) { "isLetter" } } } fun getLargestProduct(size: Int): Int { require(numbers.length >= size && size >= 0) val numList = numbers.map { it.toString().toInt() } var bestSum = 0 for (i in 0 .. numList.size - size){ var sum = 1 for (j in i until i+size){ sum *= numList[j] } if(bestSum < sum) { bestSum = sum } } return bestSum } }
이중 for문을 사용하여 처리
[Solution2]
class Series(str: String) { private val numbers = str init { str.toCharArray() .map { c -> c.isLetter() } .find { it }?.let { require(!it) { "isLetter" } } } fun getLargestProduct(size: Int): Int { require(numbers.length >= size && size >= 0) if(size == 0) return 1 return numbers.map { it.toString().toInt() } .windowed(size) .map { it.reduce { acc, i -> acc * i } }.max() ?: 1 } }
windowed , reduce , max 를 사용하여 처리하도록 수정
[Solution3]
class Series(str: String) { private val numbers = str init { str.toCharArray() .filter { it.isLetter() } .let { require(it.isEmpty()) { "isLetter" } } } fun getLargestProduct(size: Int): Int { require(numbers.length >= size && size >= 0) if(size == 0) return 1 return numbers.map { it.toString().toInt() } .windowed(size) .map { it.reduce { acc, i -> acc * i } }.max() ?: 1 } }
map , find 를 합쳐서 filter 로 처리
[Solution4]
class Series(val str: String) { init { str.toCharArray() .filter { it.isLetter() } .let { require(it.isEmpty()) { "isLetter" } } } fun getLargestProduct(size: Int): Int { require(str.length >= size && size >= 0) if(size == 0) return 1 return str.map { it.toString().toInt() } .windowed(size) .map { it.reduce { acc, i -> acc * i } }.max() ?: 1 } }
private val numbers = str 를 줄이도록 수정
[Solution5]
class Series(private val str: String) { init { str.toCharArray() .filter { it.isLetter() } .let { require(it.isEmpty()) { "isLetter" } } } fun getLargestProduct(size: Int): Int { require(str.length >= size && size >= 0) if(size == 0) return 1 return str.map { it.toString().toInt() } .windowed(size) .map { it.reduce { acc, i -> acc * i } }.max() ?: 1 } }
private 추가
반응형'문제풀이 > Exercism' 카테고리의 다른 글
Exercism - Diamond(with.Kotlin) (0) 2019.08.27 Exercism - Luhn(with.Kotlin) (0) 2019.08.19 Exercism - Acronym(with.Kotlin) (0) 2019.08.19 Exercism - Perfect Numbers(with.Kotlin) (0) 2019.08.17 Exercism - Difference Of Squares(with.Kotlin) (0) 2019.08.06 Exercism - Scrabble Score (With.Kotlin) (0) 2019.07.30 Exercism - Space Age (With.Kotlin) (0) 2019.07.26 Exercism - Gigasecond(With.Kotlin) (0) 2019.07.23