From 0a92c9cec95beac4c8a76cf0a52a62a1f19d3241 Mon Sep 17 00:00:00 2001 From: Max Hohlfeld Date: Fri, 12 May 2023 12:42:18 +0200 Subject: [PATCH] scala day 2 --- scala/corrections.txt | 1 + scala/day2.scala | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 scala/corrections.txt create mode 100644 scala/day2.scala diff --git a/scala/corrections.txt b/scala/corrections.txt new file mode 100644 index 0000000..d449750 --- /dev/null +++ b/scala/corrections.txt @@ -0,0 +1 @@ +blub#gluck diff --git a/scala/day2.scala b/scala/day2.scala new file mode 100644 index 0000000..98f520a --- /dev/null +++ b/scala/day2.scala @@ -0,0 +1,44 @@ +import scala.io.Source + +def getTotalSize(strings: List[String]): Int = { + (0 /: strings) {(sum, string) => sum + string.size} +} + +val list = List("123", "345", "ABN") + +val bla = getTotalSize(list) +println(bla) + +trait Censor { + var corrections = Map("Shoot" -> "Pucky", "Darn" -> "Beans") + + + def readCorrections() = { + val filename = "corrections.txt" + + for (line <- Source.fromFile(filename).getLines) { + val splitted = line.split("#") + corrections = corrections + (splitted(0) -> splitted(1)) + } + } + + def correct(input: String): String = { + var output = input; + + corrections.foreach((e: (String, String)) => output = e._1.r.replaceAllIn(output, e._2)) + + return output + } +} + +class Corrector extends Censor { + def say(input: String) = { + val output = correct(input) + println(output) + } +} + +val hering = new Corrector() +hering.readCorrections() +hering.say("I'm a fish, blub blub") +hering.say("Shoot Darn")