-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremoveAnswers.scala
More file actions
31 lines (27 loc) · 1017 Bytes
/
removeAnswers.scala
File metadata and controls
31 lines (27 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.{PrintWriter, File}
import scala.io.Source
def filterStart(lines: List[String]): List[String] = lines match {
case Nil =>
println("no answer found!")
Nil
case " //startAnswer" :: rest => " import Exercise._" :: filterMiddle(rest)
case first :: rest => first :: filterStart(rest)
}
def filterMiddle(lines: List[String]): List[String] = lines match {
case Nil => throw new RuntimeException("no end to the answer!")
case " //endAnswer" :: rest => rest
case _ :: rest => filterMiddle(rest)
}
val chapters: List[File] = new File("src/test/scala/practise").listFiles.filter(_.isDirectory).toList
val exercises = chapters.flatMap { _.listFiles }
exercises.foreach { exercise: File =>
val fileName = exercise.getPath
println("converting " + fileName)
val inputLines: List[String] = Source.fromFile(exercise).getLines().toList
val outputLines = filterStart(inputLines)
val output = outputLines.mkString("\n")
new PrintWriter(fileName) {
write(output)
close()
}
}