Sort by

recency

|

8 Discussions

|

  • + 0 comments

    scala, building rings, reducing cycles in rings, linear.

    import scala.io.StdIn
    
    object Solution {
      private object Move {
        val down = (1, 0)
        val right = (0, 1)
        val up = (-1, 0)
        val left = (0, -1)
      }
    
      private def rotate(r: Int, matrix: Seq[Seq[Int]]): Seq[Seq[Int]] = {
        if (r < 1)
          return matrix
        val m = matrix.length
        val n = matrix.head.length
        val c = math.min(m, n) / 2
        val rotated = Array.ofDim[Int](m, n)
        (0 until c)
          .map { i =>
            val rows = m - 2 * i
            val cols = n - 2 * i
            val cells = 2 * rows + 2 * cols - 4
            val leftTop = (i, i)
            val leftBottom = (i + rows - 1, i)
            val rightBottom = (i + rows - 1, i + cols - 1)
            val rightTop = (i, i + cols - 1)
            (1 until cells)
              .scanLeft(leftTop -> Move.down) { case ((cell, move), _) =>
                val nextCell = (cell._1 + move._1) -> (cell._2 + move._2)
                val nextMove = nextCell match {
                  case `leftBottom`  => Move.right
                  case `rightBottom` => Move.up
                  case `rightTop`    => Move.left
                  case `leftTop`     => Move.down
                  case _             => move
                }
                nextCell -> nextMove
              }
              .map(_._1)
          }
          .foreach { ring =>
            val split = r % ring.length
            (ring zip (ring.drop(split) ++ ring.take(split)))
              .foreach { case (sourceCell, targetCell) =>
                rotated(targetCell._1)(targetCell._2) = matrix(sourceCell._1)(sourceCell._2)
              }
          }
        return rotated.map(_.toSeq).toSeq
      }
    
      def main(args: Array[String]): Unit = {
        val in = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        val mnr = in.next().split(' ').map(_.toInt)
        val m = mnr.head
        val r = mnr.last
        val arr = (1 to m)
          .map(_ => in.next().split(' ').map(_.toInt).toSeq)
          .toSeq
        println(
          rotate(r, arr)
            .map(_.mkString(" "))
            .mkString("\n")
        )
      }
    }
    
  • + 0 comments

    My F# solution:

    //Enter your code here. Read input from STDIN. Print output to STDOUT
    open System
    open System.Collections.Generic
    
    //reduce number of rotation here
    let rotationReducer ms ns m n r =
        if r > (((m-ms)+(n-ns))*2) then
            let t = r % (((m-ms)+(n-ns))*2)
            t
        else 
            r
    
    //Actual rotate function  
    let doRotate matrix ms ns m n t =
        let rec rotator (matrix: List<List<string>>) ms ns m n t =
            match t with
            |0 -> matrix
            |_ when t>0 ->  let prev =  matrix.[ms].[ns]
                            //shift top row
                            for i in ns..n-1 do
                                matrix.[ms].[i] <- matrix.[ms].[i+1]
                            //shift right most column
                            for i in ms..m-1 do
                                matrix.[i].[n] <- matrix.[i+1].[n]
                            //shift bottom row
                            for i in n .. -1 ..ns+1 do
                                matrix.[m].[i] <- matrix.[m].[i-1]
                            //shift left most column
                            for i in m .. -1 ..ms+1 do
                                matrix.[i].[ns] <- matrix.[i-1].[ns]
                            matrix.[ms+1].[ns] <- prev
                            rotator matrix ms ns m n (t-1)
            |_ -> failwith "Number of rotation can not be negative"
        rotator matrix ms ns m n t
    
    //Main matrix rotation function
    let matrixRotate (matrix: List<List<string>>) r =
        let rec rotate (matrix: List<List<string>>) ms ns m n r =
            if ms < m && ns < n then
                //reduce number of rotation needed
                let t = rotationReducer ms ns m n r
                //perform actual rotation
                let interm = doRotate matrix ms ns m n t
                //go to inner layer and rotate 
                rotate interm (ms+1) (ns+1) (m-1) (n-1) r
            else
                matrix
        rotate matrix 0 0 (matrix.Count-1) (matrix.[0].Count-1) r
    
    //build up a row from input
    let buildList (x:string) (m: List<List<string>>) =
        let temp = List<string>()
        x.Split(" ")|>List.ofArray|>List.iter(fun x -> temp.Add(x))
        m.Add(temp)
    
    //take input and build up the matrix
    //to enable in place modification use .Net BCL List
    let takeInput () =
        let matrix = new List<List<string>>()
        let input =seq{
            while true do
                yield Console.ReadLine()
        }
        input|>Seq.takeWhile(fun x -> x <> null)|>Seq.iter(fun x -> buildList x matrix)
        matrix
    
    [<EntryPoint>]
    let main argv =
        let rotate = Console.ReadLine().Split(" ").[2]|>int 
        let res = (takeInput(),rotate) ||>matrixRotate
        for i in res do
            for j in i do
                Console.Write(j + " ")
            Console.Write("\n")
        0 // return an integer exit code
    
  • + 0 comments

    It's easier to shift around the indices rather than the actual elements. My approach was to not alter the original matrix, but for each index figure out where it would end up, and print appropriately.

  • + 0 comments

    Thank you so much for the making this site because here we can get more things for the work https://rememberpasswordsinmicrosoftedge.net and now we can get more support by this site.

  • + 0 comments

    I have solved this in Java, however it seems I cannot give the solution in Java ?