Text processing/1: Difference between revisions
Content added Content deleted
(use incorrect template parameters) |
(Added Scala) |
||
Line 1,189: | Line 1,189: | ||
puts "" |
puts "" |
||
puts "Maximum run(s) of #{max_invalid_count} consecutive false readings ends at #{invalid_run_end}"</lang> |
puts "Maximum run(s) of #{max_invalid_count} consecutive false readings ends at #{invalid_run_end}"</lang> |
||
=={{header|Scala}}== |
|||
=={{works with|Scala|2.8}}== |
|||
<lang scala>object DataMunging { |
|||
val pattern = """^(\d+-\d+-\d+)""" + """\s+(\d+\.\d+)\s+(-?\d+)""" * 24 + "$" r; |
|||
def spans[A](list: List[A]) = list.tail.foldLeft(List((list.head, 1))) { |
|||
case ((a, n) :: tail, b) if a == b => (a, n + 1) :: tail |
|||
case (l, b) => (b, 1) :: l |
|||
} |
|||
type Flag = ((Boolean, Int), String) |
|||
type Flags = List[Flag] |
|||
type LineIterator = Iterator[Option[(Double, Int, Flags)]] |
|||
def linesIterator(file: java.io.File) = Source.fromFile(file).getLines().map( |
|||
pattern findFirstMatchIn _ map ( |
|||
_.subgroups match { |
|||
case List(date, rawData @ _*) => |
|||
val dataset = (rawData map (_ toDouble) iterator) grouped 2 toList; |
|||
val valid = dataset filter (_.last > 0) map (_.head) |
|||
val validSize = valid length; |
|||
val validSum = valid sum; |
|||
val flags = spans(dataset map (_.last > 0)) map ((_, date)) |
|||
println("Line: %11s Reject: %2d Accept: %2d Line_tot: %10.3f Line_avg: %10.3f" format |
|||
(date, 24 - validSize, validSize, validSum, validSum / validSize)) |
|||
(validSum, validSize, flags) |
|||
} |
|||
) |
|||
) |
|||
def totalizeLines(fileIterator: LineIterator) = |
|||
fileIterator.foldLeft(0.0, 0, List[Flag]()) { |
|||
case ((totalSum, totalSize, ((flag, size), date) :: tail), Some((validSum, validSize, flags))) => |
|||
val ((firstFlag, firstSize), _) = flags.last |
|||
if (firstFlag == flag) { |
|||
(totalSum + validSum, totalSize + validSize, flags.init ::: ((flag, size + firstSize), date) :: tail) |
|||
} else { |
|||
(totalSum + validSum, totalSize + validSize, flags ::: ((flag, size), date) :: tail) |
|||
} |
|||
case ((_, _, Nil), Some(partials)) => partials |
|||
case (totals, None) => totals |
|||
} |
|||
def main(args: Array[String]) { |
|||
val filenames = args map (new java.io.File(_)) filter (file => file.isFile && file.canRead) |
|||
val lines = filenames.iterator flatMap linesIterator |
|||
val (totalSum, totalSize, flags) = totalizeLines(lines) |
|||
val ((_, run), startDate) = flags.filter(!_._1._1).max |
|||
val report = """| |
|||
|File(s) = %s |
|||
|Total = %10.3f |
|||
|Readings = %6d |
|||
|Average = %10.3f |
|||
| |
|||
|Maximum run(s) of %d consecutive false readings ends at line starting %s""".stripMargin |
|||
println(report format (filenames mkString " ", totalSum, totalSize, totalSum / totalSize, run, startDate)) |
|||
} |
|||
}</lang> |
|||
Last few lines of the sample output: |
|||
<pre> |
|||
Line: 2004-12-29 Reject: 1 Accept: 23 Line_tot: 56.300 Line_avg: 2.448 |
|||
Line: 2004-12-30 Reject: 1 Accept: 23 Line_tot: 65.300 Line_avg: 2.839 |
|||
Line: 2004-12-31 Reject: 1 Accept: 23 Line_tot: 47.300 Line_avg: 2.057 |
|||
File(s) = readings.txt |
|||
Total = 1358393.400 |
|||
Readings = 129403 |
|||
Average = 10.497 |
|||
</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |