scala day 2
This commit is contained in:
parent
97920a52fe
commit
0a92c9cec9
1
scala/corrections.txt
Normal file
1
scala/corrections.txt
Normal file
@ -0,0 +1 @@
|
||||
blub#gluck
|
44
scala/day2.scala
Normal file
44
scala/day2.scala
Normal file
@ -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")
|
Loading…
Reference in New Issue
Block a user