如何用Scala语言对多个Colmun(超过十列)进行排序



如何用Scala语言对多个Colmun(超过十列)进行排序。例如:

1  2  3 4
4  5  6 3
1  2  1 1
‌2  3  5 10

所需的输出

1 2 1 1
1 2 3 3
2 3 5 4
4 5 6 10

不多。

val input = io.Source.fromFile("junk.txt")   // open file
                     .getLines               // load all contents
                     .map(_.split("\W+"))   // turn rows into Arrays
                     .map(_.map(_.toInt))    // Arrays of Ints
val output = input.toList          // from Iterator to List
                  .transpose       // swap rows/columns
                  .map(_.sorted)   // sort rows
                  .transpose       // swap back
output.foreach(x => println(x.mkString(" ")))  // report results

注意:这允许数字之间的任何空格,但是如果它遇到其他分离器(逗号等),或者行以空格开头,它将无法创建预期的Array[Int]

另外,如果行的大小不一样,则transpose将投掷。

我遵循以下算法。首先更改行和列的尺寸。然后对行进行排序,然后再次更改尺寸以带回原始的行柱配置。这是概念的示例证明。

object SO_42720909 extends App {
  // generating dummy data
  val unsortedData = getDummyData(2, 3)
  prettyPrint(unsortedData)
  println("----------------- ")
  // altering the dimension
  val unsortedAlteredData = alterDimension(unsortedData)
  //  prettyPrint(unsortedAlteredData)
  // sorting the altered data
  val sortedAlteredData = sort(unsortedAlteredData)
  //  prettyPrint(sortedAlteredData)
  // bringing back original dimension
  val sortedData = alterDimension(sortedAlteredData)
  prettyPrint(sortedData)
  def alterDimension(data: Seq[Seq[Int]]): Seq[Seq[Int]] = {
    val col = data.size
    val row = data.head.size // make it safe
    for (i <- (0 until row))
      yield for (j <- (0 until col)) yield data(j)(i)
  }
  def sort(data: Seq[Seq[Int]]): Seq[Seq[Int]] = {
    for (row <- data) yield row.sorted
  }
  def getDummyData(row: Int, col: Int): Seq[Seq[Int]] = {
    val r = scala.util.Random
    for (i <- (1 to row))
      yield for (j <- (1 to col)) yield r.nextInt(100)
  }
  def prettyPrint(data: Seq[Seq[Int]]): Unit = {
    data.foreach(row => {
      println(row.mkString(", "))
    })
  }
}

最新更新