Created
July 6, 2024 00:47
-
-
Save zjkuang/a0a0703e9da5d1732bda3ada0e019b13 to your computer and use it in GitHub Desktop.
One; one one; two one; one two, one one; ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [1] | |
// [1,1] | |
// [2,1] | |
// [1,2,1,1] | |
// [1,1,1,2,2,1] | |
// [3,1,2,2,1,1] | |
// [1,3,1,1,2,2,2,1] | |
// [1,1,1,3,2,1,3,2,1,1] | |
// [3,1,1,3,1,2,1,1,1,3,1,2,2,1] | |
// [1,3,2,1,1,3,1,1,1,2,3,1,1,3,1,1,2,2,1,1] | |
const max = 10; | |
let largest = 0; | |
function append(arr, num) { | |
if (num > largest) { | |
largest = num; | |
} | |
arr.push(num); | |
} | |
let line = [1]; | |
console.log(JSON.stringify(line)); | |
for (let i = 1; i < max; i++) { | |
const newLine = []; | |
let current = line[0]; | |
let repeat = 0; | |
for (let j = 1; j < line.length; j++) { | |
if (line[j] === current) { | |
repeat++; | |
} | |
else { | |
append(newLine, repeat + 1); | |
append(newLine, current); | |
current = line[j]; | |
repeat = 0; | |
} | |
} | |
append(newLine, repeat + 1); | |
append(newLine, current); | |
console.log(JSON.stringify(newLine)); | |
line = newLine; | |
} | |
console.log(`\nlargest number: ${largest}\n`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment