Skip to content

Commit a433f5c

Browse files
committed
Unrelated test
1 parent 5a274b1 commit a433f5c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

tests/run/rainwater.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rainWater captured by List(0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1) = 6

tests/run/rainwater.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
def rainWater(vec: List[Int]): Int =
3+
if vec.isEmpty then 0
4+
else
5+
val mx = vec.max
6+
val split = vec.indexWhere(_ == mx)
7+
rainWater1(0, vec.take(split + 1))
8+
+ rainWater1(0, vec.drop(split).reverse)
9+
10+
def rainWater1(mx: Int, xs: List[Int]): Int = xs match
11+
case x :: rest =>
12+
val newMx = x max mx
13+
newMx - x + rainWater1(newMx, rest)
14+
case _ =>
15+
0
16+
17+
def test(xs: Int*) =
18+
println(s"rainWater captured by ${xs.toList} = ${rainWater(xs.toList)}")
19+
20+
@main def Test =
21+
test(0,1,0,2,1,0,1,3,2,1,2,1)

0 commit comments

Comments
 (0)