-
-
Save theodoreLee/5e58f4cff0464fee8a85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileOutputStream | |
import scala.io.Source | |
object StandingOvation { | |
val INPUT = "A-large-practice.in" | |
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out" | |
def minFriends(shyness: String): Int = { | |
def loop(currentLevel: Int, total: Int, acc: Int): Int = { | |
if (currentLevel >= shyness.size) acc | |
else { | |
val current = shyness(currentLevel) - '0' | |
val acc0 = if (currentLevel < total) 0 else currentLevel - total | |
loop(currentLevel + 1, total + current + acc0, acc + acc0) | |
} | |
} | |
loop(0, 0, 0) | |
} | |
def main(args: Array[String]): Unit = { | |
val itr = Source.fromFile(INPUT).getLines() | |
val sets = itr.next().toInt | |
val writer = new FileOutputStream(OUTPUT) | |
// val writer = Console.out | |
try { | |
Console.withOut(writer) { | |
for (set <- 1 to sets) { | |
val Array(_, shyness) = itr.next().split(' ') | |
println(f"Case #${set}: ${minFriends(shyness)}") | |
} | |
} | |
} finally { | |
writer.flush() | |
writer.close() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalatest.FunSuite | |
import StandingOvation._ | |
class StandingOvationSpec extends FunSuite { | |
test("sample #1") { | |
assert(minFriends("11111") === 0) | |
} | |
test("sample #2") { | |
assert(minFriends("09") === 1) | |
} | |
test("sample #3") { | |
assert(minFriends("110011") === 2) | |
} | |
test("sample #4") { | |
assert(minFriends("1") === 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment