Comment detail

トランプの和と積のパズル (Nested Flatten)
関係ないですが、Scala 2.6.0-RC1でました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import scala.collection.mutable.HashMap
import scala.collection.mutable.ListBuffer
def solve(_from:int, _to:int) = {
  val lst = for(i <-(_from to _to).toList; j <- (_from to _to) if i <= j) yield (i, j)
  def countBy(lst:List[(int,int)], f:(int,int) => int) = new AnyRef {
    val m =lst.foldLeft(new HashMap[int, ListBuffer[(int,int)]]){(h,x) => 
      h.getOrElseUpdate(f(x._1, x._2), new ListBuffer[(int,int)]) += x
      h
    }
    def apply(x:(int,int)) = m(f(x._1, x._2))
  }
  val am = countBy(lst, _*_)
  val bm = countBy(lst, _+_)
  var list = lst.filter(am(_).size != 1)
  list = list.filter(x => bm(x).size != 1 && 
                          bm(x).forall(x => am(x).size != 1))
  val cm = countBy(list, _*_)
  list = list.filter(cm(_).size == 1)
  val dm = countBy(list, _+_)
  list.filter(dm(_).size == 1)
}

println(solve(1,13).mkString(","))

Index

Feed

Other

Link

Pathtraq

loading...