Created
May 10, 2016 13:44
-
-
Save bjelline/52371a43a88a31cf47cec290aa860e5f to your computer and use it in GitHub Desktop.
my solution for code golf "chapter headings"
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
// 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