2021-06-15 00:49:18 -07:00
---
id: 5e4ce2b6ac708cc68c1df25e
2022-02-18 00:29:34 +05:30
title: Ultima lettera-prima lettera
2021-06-15 00:49:18 -07:00
challengeType: 5
forumTopicId: 385256
dashedName: last-letter-first-letter
---
# --description--
2022-02-18 00:29:34 +05:30
Un certo gioco per bambini comporta l'iniziare con una parola in una categoria particolare. Ogni partecipante a sua volta dice una parola, ma quella parola deve iniziare con la lettera finale della parola precedente. Una volta che una parola è stata data, non può essere ripetuta. Se un avversario non riesce a dare una parola nella categoria è fuori dal gioco.
2021-06-15 00:49:18 -07:00
2022-02-18 00:29:34 +05:30
Ad esempio, con "animali" come categoria,
2021-06-15 00:49:18 -07:00
2022-02-18 00:29:34 +05:30
< pre > Bambino 1: cane
Bambino 2: echidna
Bambino 1: alligatore
Bambino 2: elefante
2021-06-15 00:49:18 -07:00
...
< / pre >
# --instructions--
2022-02-18 00:29:34 +05:30
Scrivi una funzione che accetta un array di parole in input. La funzione dovrebbe restituire un array di parole dove la prima lettera di ogni parola è uguale a l'ultima lettera della parola precedente. Usa solo le parole nell'array di input e una volta usata una parola non può essere ripetuta. Le parole nell'array restituito devono essere selezionate e ordinate in modo che la sua lunghezza sia massimizzata.
2021-06-15 00:49:18 -07:00
# --hints--
2022-02-18 00:29:34 +05:30
`findLongestChain` dovrebbe essere una funzione.
2021-06-15 00:49:18 -07:00
```js
assert(typeof findLongestChain == 'function');
```
2022-02-18 00:29:34 +05:30
`findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])` dovrebbe restituire un array.
2021-06-15 00:49:18 -07:00
```js
assert(
Array.isArray(
findLongestChain([
'certain',
'each',
'game',
'involves',
'starting',
'with',
'word'
])
)
);
```
2022-02-18 00:29:34 +05:30
`findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])` dovrebbe restituire `["involves", "starting", "game", "each"]` .
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(
findLongestChain([
'certain',
'each',
'game',
'involves',
'starting',
'with',
'word'
]),
['involves', 'starting', 'game', 'each']
);
```
2022-02-18 00:29:34 +05:30
`findLongestChain(["audino", "bagon", "kangaskhan", "banette", "bidoof", "braviary", "exeggcute", "yamask"])` dovrebbe restituire `["braviary", "yamask", "kangaskhan"]`
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(
findLongestChain([
'audino',
'bagon',
'kangaskhan',
'banette',
'bidoof',
'braviary',
'exeggcute',
'yamask'
]),
['braviary', 'yamask', 'kangaskhan']
);
```
2022-02-18 00:29:34 +05:30
`findLongestChain(["harp", "poliwrath", "poochyena", "porygon2", "porygonz", "archana"])` dovrebbe restituire `["poliwrath", "harp", "poochyena", "archana"]` .
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(
findLongestChain([
'harp',
'poliwrath',
'poochyena',
'porygon2',
'porygonz',
'archana'
]),
['poliwrath', 'harp', 'poochyena', 'archana']
);
```
2022-02-18 00:29:34 +05:30
`findLongestChain(["scolipede", "elephant", "zeaking", "sealeo", "silcoon", "tigers"])` dovrebbe restituire `["scolipede", "elephant", "tigers", "sealeo"]` .
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(
findLongestChain([
'scolipede',
'elephant',
'zeaking',
'sealeo',
'silcoon',
'tigers'
]),
['scolipede', 'elephant', 'tigers', 'sealeo']
);
```
2022-02-18 00:29:34 +05:30
`findLongestChain(["loudred", "lumineon", "lunatone", "machamp", "magnezone", "nosepass", "petilil", "pidgeotto", "pikachu"])` dovrebbe restituire `["machamp", "petilil", "lumineon", "nosepass"]` .
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(
findLongestChain([
'loudred',
'lumineon',
'lunatone',
'machamp',
'magnezone',
'nosepass',
'petilil',
'pidgeotto',
'pikachu'
]),
['machamp', 'petilil', 'lumineon', 'nosepass']
);
```
# --seed--
## --seed-contents--
```js
function findLongestChain(items) {
}
```
# --solutions--
```js
function findLongestChain(items) {
function Ref(index, first_char, last_char) {
this.index = index;
this.first_char = first_char;
this.last_char = last_char;
}
var items_len = items.length
var refs_len = items_len;
var refs = []
// enough space for all items
var longest_path_refs_len = 0;
var longest_path_refs = new Array(items_len);
function search(curr_len) {
if (curr_len > longest_path_refs_len) {
longest_path_refs_len = curr_len;
for (var i = 0; i < curr_len ; i + + ) {
longest_path_refs[i] = refs[i];
}
}
// recursive search
var last_char = refs[curr_len - 1].last_char;
for (var i = curr_len; i < refs_len ; i + + )
if (refs[i].first_char == last_char) {
var aux = refs[curr_len];
refs[curr_len] = refs[i];
refs[i] = aux;
search(curr_len + 1);
refs[i] = refs[curr_len];
refs[curr_len] = aux;
}
}
for (var i = 0; i < items_len ; i + + ) {
var itemsi_len = items[i].length;
refs.push(new Ref(i, items[i][0], items[i][itemsi_len - 1]));
}
// try each item as possible start
for (var i = 0; i < items_len ; i + + ) {
var aux = refs[0];
refs[0] = refs[i];
refs[i] = aux;
search(1);
refs[i] = refs[0];
refs[0] = aux;
}
var longest_path_len = longest_path_refs_len;
var longest_path = new Array(longest_path_len);
for (var i = 0; i < longest_path_len ; i + + )
longest_path[i] = items[longest_path_refs[i].index];
return longest_path;
}
```