3.8 KiB
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
5992e222d397f00d21122931 | Fibonacci word | 5 | 302269 | fibonacci-word |
--description--
The Fibonacci Word Sequence may be created in a manner analogous to the Fibonacci Sequence, but it focuses on iterating concatenation.
Define F_Word1 as 1 Define F_Word2 as 0 Form F_Word3 as F_Word2 concatenated with F_Word1 i.e.: 01 Form F_Wordn as F_Wordn-1 concatenated with F_wordn-2
Entropy calculation is required in this challenge, as shown in this Rosetta Code challenge
--instructions--
Write a function to return the first n
Fibonacci Words. The number of n
is provided as a parameter to the function. The function should return an array of objects. The objects should be of the form: { N: 1, Length: 1, Entropy: 0, Word: '1' }
. Entropy
is computed for the string Word
and rounded to 8 decimal digits of accuracy. Note that the indices of this sequence start at 1
.
--hints--
fibWord
should be a function.
assert(typeof fibWord === 'function');
fibWord(5)
should return an array.
assert(Array.isArray(fibWord(5)));
fibWord(5)
should return [{ N:1, Length:1, Entropy:0, Word:"1" },{ N:2, Length:1, Entropy:0, Word:"0" },{ N:3, Length:2, Entropy:1, Word:"01" },{ N:4, Length:3, Entropy:0.91829583, Word:"010" },{ N:5, Length:5, Entropy:0.97095059, Word:"01001" }]
.
assert.deepEqual(fibWord(5), words5);
fibWord(7)
should return [{ N:1, Length:1, Entropy:0, Word:"1" },{ N:2, Length:1, Entropy:0, Word:"0" },{ N:3, Length:2, Entropy:1, Word:"01" },{ N:4, Length:3, Entropy:0.91829583, Word:"010" },{ N:5, Length:5, Entropy:0.97095059, Word:"01001" }, { N:6, Length:8, Entropy:0.954434, Word:'01001010' }, { N:7, Length:13, Entropy:0.9612366, Word:'0100101001001' }]
.
assert.deepEqual(fibWord(7), words7);
--seed--
--after-user-code--
const words5 = [
{ N: 1, Length: 1, Entropy: 0, Word: '1' },
{ N: 2, Length: 1, Entropy: 0, Word: '0' },
{ N: 3, Length: 2, Entropy: 1, Word: '01' },
{ N: 4, Length: 3, Entropy: 0.91829583, Word: '010' },
{ N: 5, Length: 5, Entropy: 0.97095059, Word: '01001' }
];
const words7 = [
{ N: 1, Length: 1, Entropy: 0, Word: '1' },
{ N: 2, Length: 1, Entropy: 0, Word: '0' },
{ N: 3, Length: 2, Entropy: 1, Word: '01' },
{ N: 4, Length: 3, Entropy: 0.91829583, Word: '010' },
{ N: 5, Length: 5, Entropy: 0.97095059, Word: '01001' },
{ N: 6, Length: 8, Entropy: 0.954434, Word: '01001010' },
{ N: 7, Length: 13, Entropy: 0.9612366, Word: '0100101001001' }
];
--seed-contents--
function fibWord(n) {
}
--solutions--
// Round to digits
function roundFloat(num, digits) {
return Math.round(num * 10.0**digits) / (10.0**digits);
}
// Entropy calculation for string with only 0 and 1
function entropy(word) {
function digitEntropy(count) {
return count < 1 ? 0
: - count / word.length * Math.log2(count / word.length);
}
const numZeros = word.split('').filter(e => e === '0').length;
const numOnes = word.length - numZeros;
return roundFloat(digitEntropy(numZeros) + digitEntropy(numOnes), 8);
}
// Compute array of Fibonacci words
function fibWord(n) {
return [...Array(n).keys()]
.reduce((words, i) => {
const word = i === 0 ? "1"
: i === 1 ? "0"
: words[i - 1].Word + words[i - 2].Word;
words.push(
{ N: i + 1, Length: word.length, Entropy: entropy(word), Word: word }
);
return words;
}, []);
}