object Solution {
def solution(score: Vector[Vector[Int]]): Vector[Int] = {
def avg(x:Vector[Int]):Int= (x(0)+x(1))/2
def filList(idx:Int=0,lst:List[Int]=List()):List[Int]={
if(idx==score.length) lst
else filList(idx+1,lst ::: List(avg(score(idx))))
}
val originList = filList() //[70,80,75,90] 평균이 적혀있는 리스트
val sortedList = (filList().sorted).reverse //[90,80,75,70]으로 정렬
def ranker(idx:Int=0,arr:Array[Int]):Array[Int]={
val n:Int = originList.indexOf(sortedList(idx)) //idx 원소가 원래 몇번쨰에 있는지
if(idx==sortedList.length) arr
else{
if(idx==0){
arr(n) = 1
ranker(1, arr)
}
else if (sortedList(idx)!=sortedList(idx-1)) {
arr(n) = idx + 1
ranker(idx + 1, arr)
}
else {
arr(n-1) = idx+1
arr(n) = idx+1
ranker(idx+1, arr)
}
}
}
val arr = new Array[Int](score.length)
ranker(0,arr).toVector
}
}
첫번째 나의 풀이
Exception in thread "main" java.lang.IndexOutOfBoundsException: 4
at scala.collection.LinearSeqOps.apply(LinearSeq.scala:116)
at scala.collection.LinearSeqOps.apply$(LinearSeq.scala:113)
at scala.collection.immutable.List.apply(List.scala:79)
at Solution$.ranker$1(Solution.scala:11)
at Solution$.solution(Solution.scala:30)
at SolutionTest$.$anonfun$main$1(SolutionTest.scala:47)
at SolutionTest$SolutionRunner.run(SolutionTest.scala:28)
at SolutionTest$.main(SolutionTest.scala:47)
at SolutionTest.main(SolutionTest.scala)
at scala.collection.LinearSeqOps.apply(LinearSeq.scala:116)
at scala.collection.LinearSeqOps.apply$(LinearSeq.scala:113)
at scala.collection.immutable.List.apply(List.scala:79)
at Solution$.ranker$1(Solution.scala:11)
at Solution$.solution(Solution.scala:30)
at SolutionTest$.$anonfun$main$1(SolutionTest.scala:47)
at SolutionTest$SolutionRunner.run(SolutionTest.scala:28)
at SolutionTest$.main(SolutionTest.scala:47)
at SolutionTest.main(SolutionTest.scala)
테스트 결과 (~˘▾˘)~
2개 중 0개 성공
solution 2)
object Solution {
def solution(score: Vector[Vector[Int]]): Vector[Int] = {
def func(idx:Int=0, lst:List[Int]=List()):List[Int]={
if(idx==score.length) lst
else {
val value = score(idx)(0)+score(idx)(1)
func(idx+1, lst ::: List(value/2))}
}
val avg_list:List[Int] = func()
val ord_list = (avg_list.sorted).reverse
def ranker(idx:Int=0,res:List[Int]=List()):List[Int]={
if(idx==avg_list.length) res
else{
val index = ord_list.indexOf(avg_list(idx))
ranker(idx+1, res:::List(index+1))
}
}
ranker().toVector
}
}
test 3, 6 틀림
List[Int] 때문에 생기는 소숫점 버림 현상을 고쳐주니 됐다.
def solution(score: Vector[Vector[Int]]): Vector[Int] = {
def func(idx:Int=0, lst:List[Double]=List()):List[Double]={
if(idx==score.length) lst
else {
val value = score(idx)(0)+score(idx)(1)
func(idx+1, lst ::: List(value/2.0))}
}
val avg_list:List[Double] = func()
val ord_list = (avg_list.sorted).reverse
def ranker(idx:Int=0,res:List[Int]=List()):List[Int]={
if(idx==avg_list.length) res
else{
val index = ord_list.indexOf(avg_list(idx))
ranker(idx+1, res:::List(index+1))
}
}
ranker().toVector
}
'프로그래밍 > 프로그래머스' 카테고리의 다른 글
숨어있는 숫자의 덧셈 solution code (0) | 2022.12.01 |
---|---|
다항식 더하기 solution code (0) | 2022.12.01 |
프로그래머스 한 번만 등장한 문자 solution code (0) | 2022.11.30 |
최빈값 return (0) | 2022.11.21 |
11/18 FRI 프로그래머스 분수의 덧셈 (0) | 2022.11.18 |