Advertisement
dimipan80

Exams - Spiral Matrix (on JavaScript)

Dec 31st, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* We have to make a spiral matrix (n x n) defined by walking over a grid of letters as a spiral (right, down,
  2.  left, up, and again - right, down, left, etc.). We start from the upper left corner of the matrix and fill each cell
  3.  with a letter from a given keyword. We fill the cells starting with the first letter of the keyword; when we
  4.  get to the last letter we return to the first letter again. The process is repeated until the matrix is fully filled.
  5.  See the example below to better understand your task.
  6.  The weight of each letter is the product of its position in the English alphabet and the number 10
  7.  (weight 'a' = 1*10 = 10, weight 'b' = 2*10 =20 … weight 'z' = 26*10 = 260). Find the index and the weight of
  8.  the row with the biggest weight. If several rows have an equal weight, print the upper-most row.
  9.  The input consist of two lines. On the first line of input, you will read a number n, representing the size of
  10.  the matrix. On the second line of input, you will read a string – the keyword.
  11.  On the only output line you must print the index and weight of the row with biggest weight in the format:
  12.  "{row} - {weight}". */
  13.  
  14. "use strict";
  15.  
  16. function solve(args) {
  17.     var n = parseInt(args[0]);
  18.     var keyword = args[1];
  19.  
  20.     var matrix = new Array(n);
  21.     var row, col;
  22.     for (row = 0; row < matrix.length; row += 1) {
  23.         matrix[row] = new Array(n);
  24.     }
  25.    
  26.     row = 0;
  27.     col = 0;
  28.     /*var direction = 'right';
  29.       for (var i = 0; i < (n * n); i += 1) {
  30.         if (direction == "right" && (col > (n - 1) || matrix[row][col])) {
  31.             direction = "down";
  32.             row += 1;
  33.             col -= 1;
  34.         }
  35.         if (direction == "down" && (row > (n - 1) || matrix[row][col])) {
  36.             direction = "left";
  37.             row -= 1;
  38.             col -= 1;
  39.         }
  40.         if (direction == "left" && (col < 0 || matrix[row][col])) {
  41.             direction = "up";
  42.             row -= 1;
  43.             col += 1;
  44.         }
  45.         if (direction == "up" && (row < 0 || matrix[row][col])) {
  46.             direction = "right";
  47.             row += 1;
  48.             col += 1;
  49.         }
  50.         matrix[row][col] = keyword[(i % keyword.length)];
  51.         (direction == 'right') ? col += 1 : (direction == 'down') ? row += 1 :
  52.             (direction == 'left') ? col -= 1 : (direction == 'up') ? row -= 1 : 0;
  53.     }*/
  54.  
  55.     var letterIndex = 0;
  56.     while (letterIndex < (n * n)) {
  57.         getNextLetter(0, 1);
  58.         row += 1;
  59.         col -= 1;
  60.  
  61.         getNextLetter(1, 0);
  62.         row -= 1;
  63.         col -= 1;
  64.  
  65.         getNextLetter(0, -1);
  66.         row -= 1;
  67.         col += 1;
  68.  
  69.         getNextLetter(-1, 0);
  70.         row += 1;
  71.         col += 1;
  72.     }
  73.  
  74.     function getNextLetter(rowIndex, colIndex) {
  75.         while (row >= 0 && row < n && col >= 0 && col < n && matrix[row][col] == undefined) {
  76.             matrix[row][col] = keyword[letterIndex % keyword.length];
  77.             row += rowIndex;
  78.             col += colIndex;
  79.             letterIndex += 1;
  80.         }
  81.     }
  82.  
  83.     var maxSum = 0;
  84.     var index = 0;
  85.     for (row = 0; row < matrix.length; row += 1) {
  86.         var rowSum = 0;
  87.         for (col = 0; col < n; col += 1) {
  88.             rowSum += (matrix[row][col].toLowerCase().charCodeAt(0) - 96);
  89.         }
  90.  
  91.         if (rowSum > maxSum) {
  92.             maxSum = rowSum;
  93.             index = row;
  94.         }
  95.     }
  96.  
  97.     maxSum *= 10;
  98.     console.log("%d - %d", index, maxSum);
  99. }
  100.  
  101. solve(['4', 'SoftUni']);
  102. solve(['7', 'Abcd']);
  103. solve(['6', 'Hello']);
  104. solve(['12', 'matrix']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement