Advertisement
dimipan80

Exams - Pyramid (on JavaScript)

Jan 8th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are a given pyramid of integer numbers. Your task is to print a growing sequence of integers,
  2.  starting from the top of the pyramid. If a row does not contain a number larger than the previous one,
  3.  we go to the next row and search for a number greater than the previous number + 1.
  4.  On the first line, you will get the number of lines N.
  5.  On the next N you will get the rows of the pyramid. The first row will contain only 1 number.
  6.  The numbers in each row are separated by one or more spaces.
  7.  There will be a different number of spaces at the beginning of each line.
  8.  Print on the console all numbers of the sequence separated by a comma and a space. */
  9.  
  10. "use strict";
  11.  
  12. function solve(args) {
  13.     var countRows = parseInt(args[0]);
  14.     var previousNum = parseInt(args[1]);
  15.     var outputArr = [previousNum];
  16.     for (var i = 2; i <= countRows; i += 1) {
  17.         var inputNums = args[i].split(/\s+/).filter(Boolean);
  18.         inputNums.forEach(function (num) {
  19.             parseInt(num);
  20.         });
  21.         inputNums.sort(function (a, b) {
  22.             return (a - b);
  23.         });
  24.         var isFoundBiggerNumber = false;
  25.         for (var j = 0; j < inputNums.length; j += 1) {
  26.             if (parseInt(inputNums[j]) > previousNum) {
  27.                 previousNum = parseInt(inputNums[j]);
  28.                 outputArr.push(previousNum);
  29.                 isFoundBiggerNumber = true;
  30.                 break;
  31.             }
  32.         }
  33.         if (!isFoundBiggerNumber) {
  34.             previousNum += 1;
  35.         }
  36.     }
  37.  
  38.     console.log(outputArr.join(', '));
  39. }
  40.  
  41. solve([
  42.     '3',
  43.     '2',
  44.     '5  8',
  45.     '4 9 10'
  46. ]);
  47.  
  48. solve([
  49.     '3',
  50.     '6',
  51.     '5  3',
  52.     '4 9 10'
  53. ]);
  54.  
  55. solve([
  56.     '5',
  57.     '6',
  58.     '11   9',
  59.     '5  5   5',
  60.     '0 0 0   0',
  61.     '9 10 11 12 13'
  62. ]);
  63.  
  64. solve([
  65.     '10',
  66.     '5',
  67.     '5 8',
  68.     '-6 16 10',
  69.     '10 12 1 12',
  70.     '12 0 0 0 0 20',
  71.     '20 -1 -2 -2 -2 21',
  72.     '20 -1 -2 -2 -2 -2 30',
  73.     '20 -1 -2 -2 -2 -2 30 31',
  74.     '20 -1 -2 -2 -2 -2 30 50 41',
  75.     '20 -1 -2 -2 -2 -2 30 50 41 45'
  76. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement