Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are a given pyramid of integer numbers. Your task is to print a growing sequence of integers,
- starting from the top of the pyramid. If a row does not contain a number larger than the previous one,
- we go to the next row and search for a number greater than the previous number + 1.
- On the first line, you will get the number of lines N.
- On the next N you will get the rows of the pyramid. The first row will contain only 1 number.
- The numbers in each row are separated by one or more spaces.
- There will be a different number of spaces at the beginning of each line.
- Print on the console all numbers of the sequence separated by a comma and a space. */
- "use strict";
- function solve(args) {
- var countRows = parseInt(args[0]);
- var previousNum = parseInt(args[1]);
- var outputArr = [previousNum];
- for (var i = 2; i <= countRows; i += 1) {
- var inputNums = args[i].split(/\s+/).filter(Boolean);
- inputNums.forEach(function (num) {
- parseInt(num);
- });
- inputNums.sort(function (a, b) {
- return (a - b);
- });
- var isFoundBiggerNumber = false;
- for (var j = 0; j < inputNums.length; j += 1) {
- if (parseInt(inputNums[j]) > previousNum) {
- previousNum = parseInt(inputNums[j]);
- outputArr.push(previousNum);
- isFoundBiggerNumber = true;
- break;
- }
- }
- if (!isFoundBiggerNumber) {
- previousNum += 1;
- }
- }
- console.log(outputArr.join(', '));
- }
- solve([
- '3',
- '2',
- '5 8',
- '4 9 10'
- ]);
- solve([
- '3',
- '6',
- '5 3',
- '4 9 10'
- ]);
- solve([
- '5',
- '6',
- '11 9',
- '5 5 5',
- '0 0 0 0',
- '9 10 11 12 13'
- ]);
- solve([
- '10',
- '5',
- '5 8',
- '-6 16 10',
- '10 12 1 12',
- '12 0 0 0 0 20',
- '20 -1 -2 -2 -2 21',
- '20 -1 -2 -2 -2 -2 30',
- '20 -1 -2 -2 -2 -2 30 31',
- '20 -1 -2 -2 -2 -2 30 50 41',
- '20 -1 -2 -2 -2 -2 30 50 41 45'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement