Skip to content

Instantly share code, notes, and snippets.

@bjelline
Created May 10, 2016 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjelline/52371a43a88a31cf47cec290aa860e5f to your computer and use it in GitHub Desktop.
Save bjelline/52371a43a88a31cf47cec290aa860e5f to your computer and use it in GitHub Desktop.
my solution for code golf "chapter headings"
// my solution for code golf "chapter headings":
// https://codegolf.stackexchange.com/questions/79696/convert-header-levels-to-numbers
// created in https://ttdbin.com
function f(a){
var result = [];
for( var i in a ){
let r = [];
if(0 == i){
r[i] = 1;
} else {
for ( var k=0; k<a[i]; k++ ) {
r[k] = result[i-1][k];
}
if( a[i] > a[i-1] ) {
r[ a[i]-1 ] = 1;
} else {
r[ a[i]-1 ]++;
}
}
result.push(r);
}
return result;
}
describe('code golf ', function() {
it('creates 1 entry', function() {
assert.deepEqual(f([1]), [[1]]);
});
it('coupts up if on same level', function() {
assert.deepEqual(f([1,1]), [[1],[2]]);
});
it('coupts up to 3 if on same level', function() {
assert.deepEqual(f([1,1,1]), [[1],[2],[3]]);
});
it('indent if on next level', function() {
assert.deepEqual(f([1,2]), [[1],[1,1]]);
});
it('works for the big example', function() {
assert.deepEqual(f([1,2,2,1,1,2,3,2,3,3,2,1,1]),
[[1],[1,1],[1,2],[2],[3],[3,1],[3,1,1],[3,2],[3,2,1],[3,2,2],[3,3],[4],[5]]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment