Solution 1
아 문제를 아예 잘못읽고 삽질했다.
def solution(my_string: String): Int = {
val str = my_string.sorted
def filter(idx:Int=0, res:String =""):String={
if(idx==my_string.length) res
else{
if(str(idx).toInt>57) str.slice(0,idx)
else filter(idx+1,res)
}
}
val lst:List[Char] = filter().toList
def sum(idx:Int=0, count:Int = 0):Int={
if(idx==lst.length) count
else sum(idx+1, count-48+lst(idx).toInt)
}
sum()
}
다시 풀어본 solution
Solution 2
def solution(my_string: String): Int = {
def filter(idx:Int=0, tem:String ="",res:List[String]=List()):List[String]={
if(idx==my_string.length) res
else{
if(my_string(idx).toInt <60) filter(idx+1, tem+my_string(idx),res)
else filter(idx+1, "",res ::: List(tem))
}}
def counter(idx:Int=0, sum:Int=0):Int={
if(idx==filter().length) sum
else{
if(filter()(idx)=="") counter(idx+1,sum)
else counter(idx+1, sum+(filter()(idx).toString).toInt)
}}
counter()
}
테케는 통과했는데 테스트에서 두개 정도 틀렸다 .
--> 문자열의 제일 끝에 숫자가 올 때 인식을 못한다.
Solution 3
def solution(my_string: String): Int = {
val str = my_string+"A"
def filter(idx:Int=0, tem:String ="",res:List[String]=List()):List[String]={
if(idx==str.length) res
else{
if(str(idx).toInt <60) filter(idx+1, tem+str(idx),res)
else filter(idx+1, "",res ::: List(tem))
}}
def counter(idx:Int=0, sum:Int=0):Int={
if(idx==filter().length) sum
else{
if(filter()(idx)=="") counter(idx+1,sum)
else counter(idx+1, sum+(filter()(idx).toString).toInt)
}}
counter()
}
이건 정답이다
마지막까지 숫자가 오는 경우를 대비해 제일 뒤에 알파벳 아무거나 붙여주면 된당
마지막에 숫자가 오면 인식을 못하기 때문!
'프로그래밍 > 프로그래머스' 카테고리의 다른 글
프로그래머스 등수 매기기 (1) | 2022.12.08 |
---|---|
다항식 더하기 solution code (0) | 2022.12.01 |
프로그래머스 한 번만 등장한 문자 solution code (0) | 2022.11.30 |
최빈값 return (0) | 2022.11.21 |
11/18 FRI 프로그래머스 분수의 덧셈 (0) | 2022.11.18 |