Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit cfe7ab6

Browse files
committed
Solution for challenge simple encryption.
1 parent 8e32af0 commit cfe7ab6

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
"prettier": "^1.14.3",
3535
"tap-spec": "^5.0.0",
3636
"tape": "^4.9.1"
37+
},
38+
"dependencies": {
39+
"ramda": "^0.26.0"
3740
}
3841
}

src/simpleEncryption.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
1-
//Taken from https://www.codewars.com/kata/simple-encryption-number-1-alternating-split/javascript
1+
//Taken from https://www.codewars.com/kata/simple-encryption-number-1-alternating-split/javascript
2+
const R = require('ramda')
3+
4+
const even = (_, index) => (Number(index) + 1) % 2
5+
const odd = (_, index) => Number(index) % 2
6+
27
function encrypt(text, n) {
3-
8+
if (n < 1 || !text) {
9+
return text
10+
}
11+
return new Array(n)
12+
.fill(0)
13+
.reduce((acc) => acc.filter(odd).concat(acc.filter(even)), text.split(''))
14+
.join('')
15+
}
16+
17+
const splitInHalf = (inputArray) => {
18+
const firstHalf = inputArray.slice(0, Math.floor(inputArray.length / 2))
19+
const secondHalf = inputArray.slice(Math.floor(inputArray.length / 2))
20+
if (firstHalf.length < secondHalf.length) {
21+
firstHalf.push('')
22+
}
23+
return [firstHalf, secondHalf]
24+
}
25+
26+
const reducer = (acc) => {
27+
const [firstHalf, secondHalf] = splitInHalf(acc)
28+
return R.pipe(
29+
R.flip(R.zip)(firstHalf),
30+
R.flatten,
31+
R.join(''),
32+
R.split('')
33+
)(secondHalf)
434
}
535

636
function decrypt(encryptedText, n) {
37+
if (n < 1 || !encryptedText) {
38+
return encryptedText
39+
}
40+
41+
return R.pipe(
42+
R.reduce(reducer, encryptedText.split('')),
43+
R.join('')
44+
)(new Array(n).fill(0))
745
}
846

947
module.exports = {
1048
decrypt,
1149
encrypt,
50+
splitInHalf,
1251
}

src/simpleEncryption.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
const test = require('tape')
2-
const { decrypt, encrypt } = require('./simpleEncryption')
2+
const { decrypt, encrypt, splitInHalf } = require('./simpleEncryption')
33

4+
test('split in half', (t) => {
5+
t.test('Simple', (assert) => {
6+
assert.plan(1)
7+
const [_, secondHalf] = splitInHalf([1])
8+
assert.deepEquals(secondHalf, [1])
9+
})
10+
t.test('Still simple', (assert) => {
11+
assert.plan(2)
12+
const [firstHalf, secondHalf] = splitInHalf([1, 2])
13+
assert.deepEquals(secondHalf, [2])
14+
assert.deepEquals(firstHalf, [1])
15+
})
16+
t.test('Still simple', (assert) => {
17+
assert.plan(2)
18+
const [firstHalf, secondHalf] = splitInHalf([1, 2, 3])
19+
assert.deepEquals(secondHalf, [2, 3])
20+
assert.deepEquals(firstHalf, [1, ''])
21+
})
22+
})
423
test('Solution', function(t) {
524
t.test('EncryptExampleTests', function(assert) {
625
assert.equals(encrypt('This is a test!', 0), 'This is a test!')

0 commit comments

Comments
 (0)