Created
June 23, 2011 16:17
-
-
Save yogthos/1042889 to your computer and use it in GitHub Desktop.
Scala solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Matmul { | |
def matgen(n: Integer): Array[Array[Double]] = { | |
var a = new Array[Array[Double]](n, n) | |
val tmp = 1. / n / n | |
Range(0, n) map ((i) => | |
Range(0, n) map ((j) => | |
a(i)(j) = tmp * (i - j) * (i + j))) | |
return a | |
} | |
def matmul(a: Array[Array[Double]], b: Array[Array[Double]]): Array[Array[Double]] = { | |
val m = a.length | |
val n = a(0).length | |
val p = b(0).length | |
var x = new Array[Array[Double]](m, p) | |
var c = b.transpose | |
Range(0, m) map ((i) => | |
Range(0, p) map ((j) => | |
x(i)(j) = Range(0, n).foldLeft(0.0)((s, k) => s + a(i)(k) * c(j)(k)))) | |
return x | |
} | |
def main(args: Array[String]) { | |
val n = 100 | |
var x = matmul(matgen(n), matgen(n)); | |
println(x(n/2)(n/2)) | |
} | |
} | |
/// | |
import scala.collection.mutable.HashMap | |
object Dict { | |
def main(args: Array[String]) { | |
val h = new HashMap[String,Integer]() | |
var max = 0 | |
for( ln <- io.Source.stdin.getLines ) { | |
var count = 1 | |
h.get(ln) match { case Some(x) => {count = x + 1; h.put(ln, count)} | |
case None => h.put(ln, count) | |
} | |
if (count > max) max = count | |
} | |
println(h.size +"\t"+ max) | |
} | |
} | |
/// | |
import scala.util.matching.Regex | |
import java.util.regex.Pattern | |
object Patmatch { | |
def main(args: Array[String]) { | |
if (args.length < 1) { | |
println("Usage: scala Patmatch.scala pattern < in.file") | |
return | |
} | |
val regex = Pattern.compile(args(0)) | |
for( ln <- io.Source.stdin.getLines ) { | |
if (regex.matcher(ln).find) println(ln) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment