2018-09-30 22:01:58 +00:00
---
id: 5992e222d397f00d21122931
2020-11-27 18:02:05 +00:00
title: Fibonacci word
2018-09-30 22:01:58 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302269
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
The Fibonacci Word may be created in a manner analogous to the Fibonacci Sequence [as described here ](https://hal.archives-ouvertes.fr/docs/00/36/79/72/PDF/The_Fibonacci_word_fractal.pdf ):
< pre > Define F_Word< sub > 1< / sub > as < strong > 1< / strong >
2019-06-14 11:04:16 +00:00
Define F_Word< sub > 2< / sub > as < strong > 0< / strong >
Form F_Word< sub > 3< / sub > as F_Word< sub > 2< / sub > concatenated with F_Word< sub > 1< / sub > i.e.: < strong > 01< / strong >
2019-03-02 12:27:55 +00:00
Form F_Word< sub > n< / sub > as F_Word< sub > n-1< / sub > concatenated with F_word< sub > n-2< / sub >
< / pre >
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --instructions--
Write a function to return the Fibonacci Words up to `n` . `n` will be 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' }` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`fibWord` should be a function.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(typeof fibWord === 'function');
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
`fibWord(5)` should return an array.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(Array.isArray(fibWord(5)));
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`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.9182958340544896, Word:"010" },{ N:5, Length:5, Entropy:0.9709505944546688, Word:"01001" }]` .
2018-09-30 22:01:58 +00:00
```js
2020-11-27 18:02:05 +00:00
assert.deepEqual(fibWord(5), ans);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --after-user-code--
2018-09-30 22:01:58 +00:00
```js
2018-10-20 18:02:47 +00:00
let ans=[ { 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.9182958340544896, Word: '010' },
{ N: 5, Length: 5, Entropy: 0.9709505944546688, Word: '01001' }];
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
function fibWord(n) {
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
}
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
function fibWord(n) {
function entropy(s) {
//create an object containing each individual char
2018-10-08 00:01:53 +00:00
//and the amount of iterations per char
2018-09-30 22:01:58 +00:00
function prob(s) {
var h = Object.create(null);
2018-10-02 14:02:53 +00:00
s.split('').forEach(function(c) {
2018-10-08 00:01:53 +00:00
h[c] & & h[c]++ || (h[c] = 1);
2018-09-30 22:01:58 +00:00
});
return h;
}
2018-10-08 00:01:53 +00:00
s = s.toString(); //just in case
2018-09-30 22:01:58 +00:00
var e = 0, l = s.length, h = prob(s);
for (var i in h ) {
var p = h[i]/l;
e -= p * Math.log(p) / Math.log(2);
}
return e;
}
var wOne = "1", wTwo = "0", wNth = [wOne, wTwo], w = "", o = [];
2018-10-08 00:01:53 +00:00
2018-09-30 22:01:58 +00:00
for (var i = 0; i < n ; i + + ) {
if (i === 0 || i === 1) {
w = wNth[i];
} else {
w = wNth[i - 1] + wNth[i - 2];
wNth.push(w);
}
var l = w.length;
var e = entropy(w);
2018-10-08 00:01:53 +00:00
2018-09-30 22:01:58 +00:00
if (l < = 21) {
2020-11-27 18:02:05 +00:00
o.push({
N: i + 1,
Length: l,
Entropy: e,
Word: w
});
2018-09-30 22:01:58 +00:00
} else {
2020-11-27 18:02:05 +00:00
o.push({
N: i + 1,
Length: l,
Entropy: e,
Word: "..."
});
2018-10-08 00:01:53 +00:00
}
2018-09-30 22:01:58 +00:00
}
return o;
}
```