Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given a sequence of text lines. Assume these text lines form a matrix of characters
- (pad the missing positions with spaces to build a rectangular matrix).Write a program to rotate the matrix
- by 90, 180, 270, 360, … degrees. Print the result at the console as sequence of strings. */
- "use strict";
- function rotateTheStringMatrix(args) {
- var rotate = Number(args[0].split(/\D+/).filter(Boolean));
- rotate %= 360;
- var i;
- var matrix = [];
- var maxStringLength = 1;
- for (i = 1; i < args.length; i += 1) {
- if (args[i].length > maxStringLength) {
- maxStringLength = args[i].length;
- }
- matrix.push(args[i].split('').filter(Boolean));
- }
- for (i = 0; i < matrix.length; i += 1) {
- while (matrix[i].length < maxStringLength) {
- matrix[i].push(' ');
- }
- }
- function rotateToNext90Degrees(matrix) {
- var maxLengthRow = matrix.length;
- var maxLengthCol = matrix[0].length;
- var newMatrix = new Array(maxLengthCol);
- for (var col = 0; col < maxLengthCol; col += 1) {
- newMatrix[col] = new Array(maxLengthRow);
- for (var row = 0; row < maxLengthRow; row += 1) {
- newMatrix[col][maxLengthRow - row - 1] = matrix[row][col];
- }
- }
- return newMatrix;
- }
- while (rotate > 0) {
- matrix = rotateToNext90Degrees(matrix);
- rotate -= 90;
- }
- for (var row in matrix) {
- if (matrix.hasOwnProperty(row)) {
- console.log(matrix[row].join(''));
- }
- }
- }
- rotateTheStringMatrix([
- 'Rotate(90)',
- 'hello',
- 'softuni',
- 'exam'
- ]);
- rotateTheStringMatrix([
- 'Rotate(180)',
- 'hello',
- 'softuni',
- 'exam'
- ]);
- rotateTheStringMatrix([
- 'Rotate(270)',
- 'hello',
- 'softuni',
- 'exam'
- ]);
- rotateTheStringMatrix([
- 'Rotate(720)',
- 'js',
- 'exam'
- ]);
- rotateTheStringMatrix([
- 'Rotate(810)',
- 'js',
- 'exam'
- ]);
- rotateTheStringMatrix([
- 'Rotate(0)',
- 'js',
- 'exam'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement