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

Challenge/simple encryption #1

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
"prettier": "^1.14.3",
"tap-spec": "^5.0.0",
"tape": "^4.9.1"
},
"dependencies": {
"ramda": "^0.26.0"
}
}
43 changes: 41 additions & 2 deletions src/simpleEncryption.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
//Taken from https://www.codewars.com/kata/simple-encryption-number-1-alternating-split/javascript
//Taken from https://www.codewars.com/kata/simple-encryption-number-1-alternating-split/javascript
const R = require('ramda')

const even = (_, index) => (Number(index) + 1) % 2
const odd = (_, index) => Number(index) % 2

function encrypt(text, n) {

if (n < 1 || !text) {
return text
}
return new Array(n)
.fill(0)
.reduce((acc) => acc.filter(odd).concat(acc.filter(even)), text.split(''))
.join('')
}

const splitInHalf = (inputArray) => {
const firstHalf = inputArray.slice(0, Math.floor(inputArray.length / 2))
const secondHalf = inputArray.slice(Math.floor(inputArray.length / 2))
if (firstHalf.length < secondHalf.length) {
firstHalf.push('')
}
return [firstHalf, secondHalf]
}

const reducer = (acc) => {
const [firstHalf, secondHalf] = splitInHalf(acc)
return R.pipe(
R.flip(R.zip)(firstHalf),
R.flatten,
R.join(''),
R.split('')
)(secondHalf)
}

function decrypt(encryptedText, n) {
if (n < 1 || !encryptedText) {
return encryptedText
}

return R.pipe(
R.reduce(reducer, encryptedText.split('')),
R.join('')
)(new Array(n).fill(0))
}

module.exports = {
decrypt,
encrypt,
splitInHalf,
}
21 changes: 20 additions & 1 deletion src/simpleEncryption.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
const test = require('tape')
const { decrypt, encrypt } = require('./simpleEncryption')
const { decrypt, encrypt, splitInHalf } = require('./simpleEncryption')

test('split in half', (t) => {
t.test('Simple', (assert) => {
assert.plan(1)
const [_, secondHalf] = splitInHalf([1])
assert.deepEquals(secondHalf, [1])
})
t.test('Still simple', (assert) => {
assert.plan(2)
const [firstHalf, secondHalf] = splitInHalf([1, 2])
assert.deepEquals(secondHalf, [2])
assert.deepEquals(firstHalf, [1])
})
t.test('Still simple', (assert) => {
assert.plan(2)
const [firstHalf, secondHalf] = splitInHalf([1, 2, 3])
assert.deepEquals(secondHalf, [2, 3])
assert.deepEquals(firstHalf, [1, ''])
})
})
test('Solution', function(t) {
t.test('EncryptExampleTests', function(assert) {
assert.equals(encrypt('This is a test!', 0), 'This is a test!')
Expand Down