We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
//Enter your code here. Read input from STDIN. Print output to STDOUTopenSystemopenSystem.Collections.Generic//reduce number of rotation hereletrotationReducermsnsmnr=ifr>(((m-ms)+(n-ns))*2)thenlett=r%(((m-ms)+(n-ns))*2)telser//Actual rotate function letdoRotatematrixmsnsmnt=letrecrotator(matrix:List<List<string>>)msnsmnt=matchtwith|0->matrix|_whent>0->letprev=matrix.[ms].[ns]//shift top rowforiinns..n-1domatrix.[ms].[i]<-matrix.[ms].[i+1]//shift right most columnforiinms..m-1domatrix.[i].[n]<-matrix.[i+1].[n]//shift bottom rowforiinn..-1..ns+1domatrix.[m].[i]<-matrix.[m].[i-1]//shift left most columnforiinm..-1..ms+1domatrix.[i].[ns]<-matrix.[i-1].[ns]matrix.[ms+1].[ns]<-prevrotatormatrixmsnsmn(t-1)|_->failwith"Number of rotation can not be negative"rotatormatrixmsnsmnt//Main matrix rotation functionletmatrixRotate(matrix:List<List<string>>)r=letrecrotate(matrix:List<List<string>>)msnsmnr=ifms<m&&ns<nthen//reduce number of rotation neededlett=rotationReducermsnsmnr//perform actual rotationletinterm=doRotatematrixmsnsmnt//go to inner layer and rotate rotateinterm(ms+1)(ns+1)(m-1)(n-1)relsematrixrotatematrix00(matrix.Count-1)(matrix.[0].Count-1)r//build up a row from inputletbuildList(x:string)(m:List<List<string>>)=lettemp=List<string>()x.Split(" ")|>List.ofArray|>List.iter(funx->temp.Add(x))m.Add(temp)//take input and build up the matrix//to enable in place modification use .Net BCL ListlettakeInput()=letmatrix=newList<List<string>>()letinput=seq{whiletruedoyieldConsole.ReadLine()}input|>Seq.takeWhile(funx->x<>null)|>Seq.iter(funx->buildListxmatrix)matrix[<EntryPoint>]letmainargv=letrotate=Console.ReadLine().Split(" ").[2]|>intletres=(takeInput(),rotate)||>matrixRotateforiinresdoforjinidoConsole.Write(j+" ")Console.Write("\n")0// return an integer exit code
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Matrix Rotation
You are viewing a single comment's thread. Return to all comments →
My F# solution: