LoginSignup
1
1

More than 5 years have passed since last update.

不等長リストの入れ替え

Last updated at Posted at 2015-12-22
/*
List(
    List(1,2,3,4),
    List(5,6,7,8),
    List(9,0)
)
↓
List(
    List(1,5,9),
    List(2,6,0),
    List(3,7),
    List(4,8)
)*/

case class Player(id: Int, name: String, score: Int)

val t: List[(Int, String, Int)] = 
    (1 to 17) zip ('a' to 'z') map (x => (x._1, x._2.toString, scala.util.Random.nextInt(100))) toList

val players = t.map(x => Player(x._1, x._2, x._3))

val sortedPlayers = players.sortBy(x => (- x.score, x.id))

def transpose[A](xs: List[List[A]]): List[List[A]] = xs.filter(_.nonEmpty) match {
    case Nil => Nil
    case ys: List[List[A]] => ys.map{ _.head }::transpose(ys.map{ _.tail })
}

transpose(sortedPlayers.grouped(4).toList)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1