comparison tools/parse.scala @ 1:cdfc5e2de435

Checking in our problem set and a simple scala parser
author Joshua Garnett <josh.garnett@gmail.com>
date Thu, 08 Aug 2013 21:08:01 -0400
parents
children 5cf8de487ed6
comparison
equal deleted inserted replaced
0:e546c1156de2 1:cdfc5e2de435
1 //
2 // How to run: scala -cp json-smart-1.0.9-1.jar parse.scala
3 //
4 import net.minidev.json._
5 import scala.collection.JavaConversions._
6 import scala.collection.mutable._
7
8 val file = scala.io.Source.fromFile("../data/myproblems.json").mkString
9 val json = JSONValue.parse(file).asInstanceOf[JSONArray]
10
11 var totalProblems = json.size
12 var totalSize = 0
13 val operatorHash = HashMap[String, Int]()
14
15 for(p <- json) {
16 val problem = p.asInstanceOf[JSONObject]
17 totalSize += problem.get("size").asInstanceOf[java.lang.Integer]
18 val operators = problem.get("operators").asInstanceOf[JSONArray]
19 for(o <- operators) {
20 val operator = o.asInstanceOf[String]
21 operatorHash(operator) = operatorHash.get(operator) match {
22 case Some(count) => count + 1
23 case None => 1
24 }
25 }
26 }
27
28 println("Total problems: " + totalProblems)
29 println("Total size: " + totalSize)
30 println("Average size: " + totalSize / totalProblems)
31
32 println("Operator Count: ")
33 for((key, value) <- operatorHash) {
34 println(" " + key + ":" + value)
35 }