Skip to content

Instantly share code, notes, and snippets.

@joalbertg
Last active July 21, 2020 21:48
Show Gist options
  • Save joalbertg/264aa23c6cef921e75426bf5b95e709e to your computer and use it in GitHub Desktop.
Save joalbertg/264aa23c6cef921e75426bf5b95e709e to your computer and use it in GitHub Desktop.
javascript: Greatest Sequence
/*
* In the following 6 digit number:
* 283910
* 91 is the greatest sequence of 2 consecutive digits.
*
* In the following 10 digit number:
* 1234567890
* 67890 is the greatest sequence of 5 consecutive digits.
*
* Complete the solution so that it returns the greatest sequence
* of five consecutive digits found within the number given. The
* number will be passed in as a string of only digits. It should
* return a five digit intenger. The number passed may be as large
* as 1000 digits.
*/
const numberA = '283910';
const numberB = '1234567890';
const numberC = '1234';
const numberD = '1234';
let _1000 = '';
for(let i = 0; i < 1000; i++) {
_1000 += i % 10;
}
//console.log('->',_1000.length)
//console.log('->',_1000)
const numberE = _1000;
const greatestSequence = (num, digits) => {
const length = num.length;
if(length < digits) return 'Length less than the digits';
if(length === digits) return Number(num);
let max = '1' + '0'.repeat(digits - 1);
for(let i = 0; i <= length - digits; i++) {
if(num[i] >= max[0]) {
const candidate = num.slice(i, digits + i);
if(candidate > max) {
max = candidate;
}
}
}
if(Number(max) <= Number.MAX_VALUE) {
return Number(max);
}
return max;
}
//console.log(greatestSequence(numberA, 2));
//console.log(greatestSequence(numberB, 5));
//console.log(greatestSequence(numberC, 5));
//console.log(greatestSequence(numberD, 4));
//console.log(greatestSequence(numberE, 5));
//console.log(greatestSequence(numberE, 100));
//console.log(greatestSequence(numberE, 500));
//console.log(greatestSequence(numberE, Number.MAX_VALUE));
for(let i = 0; i < 1000000; i++) {
greatestSequence(numberB, 5);
greatestSequence(numberE, 5);
}
// time node greatest-sequence.js
const numberA = '1234567890';
let _1000 = '';
for(let i = 0; i < 1000; i++) {
_1000 += i % 10;
}
//console.log('->',_1000.length)
//console.log('->',_1000)
const numberB = _1000;
const greatestSequence = number => {
const listNumbers = number.split('').map(Number);
const uniqueNumbersDesc = [...new Set(listNumbers)].sort((a,b) => b-a);
const lengthNumber = listNumbers.length;
let listSequence = [];
for (let i=0; i < uniqueNumbersDesc.length ; i++) {
let maxValue = uniqueNumbersDesc[i];
listNumbers.filter((item, index) => {
if ((lengthNumber - index >= 5) && (item === maxValue)) {
listSequence.push(listNumbers.slice(index, index + 5).join(''));
}
});
if(listSequence.length > 0){
return("00000" + Math.max(...listSequence)).slice(-5);
break;
}
}
}
for(let i = 0; i < 1000000; i++) {
greatestSequence(numberA);
greatestSequence(numberB);
}
// time node greatest-sequence_andres.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment