2021-06-15 00:49:18 -07:00
---
id: 5a23c84252665b21eecc8029
2022-02-19 12:56:08 +05:30
title: Scacchiera a cavallo
2021-06-15 00:49:18 -07:00
challengeType: 5
forumTopicId: 302325
dashedName: straddling-checkerboard
---
# --description--
2022-02-19 12:56:08 +05:30
Implementa le funzioni per crittografare e decrittografare un messaggio usando il metodo [straddling checkerboard ](https://en.wikipedia.org/wiki/Straddling_checkerboard ). Le funzioni prenderanno una stringa e un array come parametri. L'array ha 3 stringhe che rappresentano le 3 righe della scacchiera. L'output sarà una serie di cifre decimali. I numeri devono essere cifrati inserendo il carattere di escape prima di ogni cifra, quindi includendo la cifra non cifrata. Questo dovrebbe essere invertito per la decifratura.
2021-06-15 00:49:18 -07:00
# --hints--
2022-02-19 12:56:08 +05:30
`straddle` dovrebbe essere una funzione.
2021-06-15 00:49:18 -07:00
```js
assert(typeof straddle == 'function');
```
2022-02-19 12:56:08 +05:30
`straddle("One night-it was on the twentieth of March, 1888-I was returning.",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])` dovrebbe restituire una stringa.
2021-06-15 00:49:18 -07:00
```js
assert(
typeof straddle(
'One night-it was on the twentieth of March, 1888-I was returning.',
['ESTONIA R', 'BCDFGHJKLM', 'PQUVWXYZ./']
) == 'string'
);
```
2022-02-19 12:56:08 +05:30
`straddle("One night-it was on the twentieth of March, 1888-I was returning.",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])` dovrebbe restituire `"34045747525284613427502840425027537379697175891898898898584619028294547488"` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(
straddle(
'One night-it was on the twentieth of March, 1888-I was returning.',
['ESTONIA R', 'BCDFGHJKLM', 'PQUVWXYZ./']
),
'34045747525284613427502840425027537379697175891898898898584619028294547488'
);
```
2022-02-19 12:56:08 +05:30
`straddle("One night-it was on the twentieth of March, 1888-I was returning",["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"])` dovrebbe restituire `"139539363509369743061399059745399365901344308320791798798798367430685972839363935"` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(
straddle('One night-it was on the twentieth of March, 1888-I was returning', [
'HOL MES RT',
'ABCDFGIJKN',
'PQUVWXYZ./'
]),
'139539363509369743061399059745399365901344308320791798798798367430685972839363935'
);
```
2022-02-19 12:56:08 +05:30
`straddle("Thecheckerboardcakerecipespecifies3largeeggsand2.25cupsofflour.",["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."])` dovrebbe restituire `"125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769"` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(
straddle('Thecheckerboardcakerecipespecifies3largeeggsand2.25cupsofflour.', [
'ET AON RIS',
'BCDFGHJKLM',
'PQ/UVWXYZ.'
]),
'125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769'
);
```
2022-02-19 12:56:08 +05:30
`unstraddle` dovrebbe essere una funzione.
2021-06-15 00:49:18 -07:00
```js
assert(typeof unstraddle == 'function');
```
2022-02-19 12:56:08 +05:30
`unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])` dovrebbe restituire una stringa.
2021-06-15 00:49:18 -07:00
```js
assert(
typeof unstraddle(
'34045747525284613427502840425027537379697175891898898898584619028294547488',
['ESTONIA R', 'BCDFGHJKLM', 'PQUVWXYZ./']
) == 'string'
);
```
2022-02-19 12:56:08 +05:30
`unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])` dovrebbe restituire `"ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING."` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(
unstraddle(
'34045747525284613427502840425027537379697175891898898898584619028294547488',
['ESTONIA R', 'BCDFGHJKLM', 'PQUVWXYZ./']
),
'ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING.'
);
```
2022-02-19 12:56:08 +05:30
`unstraddle("139539363509369743061399059745399365901344308320791798798798367430685972839363935",["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"])` dovrebbe restituire `"ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING"` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(
unstraddle(
'139539363509369743061399059745399365901344308320791798798798367430685972839363935',
['HOL MES RT', 'ABCDFGIJKN', 'PQUVWXYZ./']
),
'ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING'
);
```
2022-02-19 12:56:08 +05:30
`unstraddle("125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769",["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."])` dovrebbe restituire `"THECHECKERBOARDCAKERECIPESPECIFIES3LARGEEGGSAND2.25CUPSOFFLOUR."` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(
unstraddle(
'125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769',
['ET AON RIS', 'BCDFGHJKLM', 'PQ/UVWXYZ.']
),
'THECHECKERBOARDCAKERECIPESPECIFIES3LARGEEGGSAND2.25CUPSOFFLOUR.'
);
```
# --seed--
## --seed-contents--
```js
function straddle(message, alphabet) {
}
function unstraddle(message, alphabet) {
}
```
# --solutions--
```js
function straddle(message, alphabet) {
var prefixes = new Array(
'',
alphabet[0].indexOf(' '),
alphabet[0].lastIndexOf(' ')
);
var out = '';
message = message.toUpperCase();
message = message.replace(/([0-9])/g, '/$1'); // dumb way to escape numbers
for (var i = 0; i < message.length ; i + + ) {
var chr = message[i];
if (chr == ' ') continue;
for (var j = 0; j < 3 ; j + + ) {
var k = alphabet[j].indexOf(chr);
if (k < 0 ) continue ;
out += prefixes[j].toString() + k;
}
if (chr == '/') out += message[++i];
}
return out;
}
function unstraddle(message, alphabet) {
var prefixes = new Array(
'',
alphabet[0].indexOf(' '),
alphabet[0].lastIndexOf(' ')
);
var out = '';
var n, o;
for (var i = 0; i < message.length ; i + + ) {
n = message[i] * 1;
switch (n) {
case prefixes[1]:
o = alphabet[1][message[++i]];
break;
case prefixes[2]:
o = alphabet[2][message[++i]];
break;
default:
o = alphabet[0][n];
}
o == '/' ? (out += message[++i]) : (out += o);
}
return out;
}
```