Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* We have to make a spiral matrix (n x n) defined by walking over a grid of letters as a spiral (right, down,
- left, up, and again - right, down, left, etc.). We start from the upper left corner of the matrix and fill each cell
- with a letter from a given keyword. We fill the cells starting with the first letter of the keyword; when we
- get to the last letter we return to the first letter again. The process is repeated until the matrix is fully filled.
- See the example below to better understand your task.
- The weight of each letter is the product of its position in the English alphabet and the number 10
- (weight 'a' = 1*10 = 10, weight 'b' = 2*10 =20 β¦ weight 'z' = 26*10 = 260). Find the index and the weight of
- the row with the biggest weight. If several rows have an equal weight, print the upper-most row.
- The input consist of two lines. On the first line of input, you will read a number n, representing the size of
- the matrix. On the second line of input, you will read a string β the keyword.
- On the only output line you must print the index and weight of the row with biggest weight in the format:
- "{row} - {weight}". */
- "use strict";
- function solve(args) {
- var n = parseInt(args[0]);
- var keyword = args[1];
- var matrix = new Array(n);
- var row, col;
- for (row = 0; row < matrix.length; row += 1) {
- matrix[row] = new Array(n);
- }
- row = 0;
- col = 0;
- /*var direction = 'right';
- for (var i = 0; i < (n * n); i += 1) {
- if (direction == "right" && (col > (n - 1) || matrix[row][col])) {
- direction = "down";
- row += 1;
- col -= 1;
- }
- if (direction == "down" && (row > (n - 1) || matrix[row][col])) {
- direction = "left";
- row -= 1;
- col -= 1;
- }
- if (direction == "left" && (col < 0 || matrix[row][col])) {
- direction = "up";
- row -= 1;
- col += 1;
- }
- if (direction == "up" && (row < 0 || matrix[row][col])) {
- direction = "right";
- row += 1;
- col += 1;
- }
- matrix[row][col] = keyword[(i % keyword.length)];
- (direction == 'right') ? col += 1 : (direction == 'down') ? row += 1 :
- (direction == 'left') ? col -= 1 : (direction == 'up') ? row -= 1 : 0;
- }*/
- var letterIndex = 0;
- while (letterIndex < (n * n)) {
- getNextLetter(0, 1);
- row += 1;
- col -= 1;
- getNextLetter(1, 0);
- row -= 1;
- col -= 1;
- getNextLetter(0, -1);
- row -= 1;
- col += 1;
- getNextLetter(-1, 0);
- row += 1;
- col += 1;
- }
- function getNextLetter(rowIndex, colIndex) {
- while (row >= 0 && row < n && col >= 0 && col < n && matrix[row][col] == undefined) {
- matrix[row][col] = keyword[letterIndex % keyword.length];
- row += rowIndex;
- col += colIndex;
- letterIndex += 1;
- }
- }
- var maxSum = 0;
- var index = 0;
- for (row = 0; row < matrix.length; row += 1) {
- var rowSum = 0;
- for (col = 0; col < n; col += 1) {
- rowSum += (matrix[row][col].toLowerCase().charCodeAt(0) - 96);
- }
- if (rowSum > maxSum) {
- maxSum = rowSum;
- index = row;
- }
- }
- maxSum *= 10;
- console.log("%d - %d", index, maxSum);
- }
- solve(['4', 'SoftUni']);
- solve(['7', 'Abcd']);
- solve(['6', 'Hello']);
- solve(['12', 'matrix']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement