-
Notifications
You must be signed in to change notification settings - Fork 0
/
GA-DE-RY-PO-LU-KI cypher
41 lines (31 loc) · 1.58 KB
/
GA-DE-RY-PO-LU-KI cypher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Introduction
The GADERYPOLUKI is a simple substitution cypher used in scouting to encrypt messages. The encryption is based on short, easy to remember key. The key is written as paired letters, which are in the cipher simple replacement.
The most frequently used key is "GA-DE-RY-PO-LU-KI".
G => A
g => a
a => g
A => G
D => E
etc.
The letters, which are not on the list of substitutes, stays in the encrypted text without changes.
Task
Your task is to help scouts to encrypt and decrypt thier messages. Write the Encode and Decode functions.
Input/Output
The input string consists of lowercase and uperrcase characters and white . The substitution has to be case-sensitive.
Example
Encode("ABCD") // => GBCE
Encode("Ala has a cat") // => Gug hgs g cgt
Encode("gaderypoluki"); // => agedyropulik
Decode("Gug hgs g cgt") // => Ala has a cat
Decode("agedyropulik") // => gaderypoluki
Decode("GBCE") // => ABCD
*/
export function encode ( str:string):string {
const keys: { [key: string]: string } = {G:'A', g:'a', A:'G', a:'g',D:'E', d:'e',E:'D', e:'d', R:'Y', Y:'R', r:'y', y:'r', P:'O', O:'P', p:'o', o:'p', L:'U', U : 'L', l:'u', u:'l',K:'I', I:'K', k:'i', i:'k'}
return str.replace(/[GADERYPOLUKIgaderypoluki]/g, f=>keys[f] || f);
}
export function decode ( str:string):string {
const keys: { [key: string]: string } = {G:'A', g:'a', A:'G', a:'g',D:'E', d:'e',E:'D', e:'d', R:'Y', Y:'R', r:'y', y:'r', P:'O', O:'P', p:'o', o:'p', L:'U', U : 'L', l:'u', u:'l',K:'I', I:'K', k:'i', i:'k'}
return str.replace(/[GADERYPOLUKIgaderypoluki]/g, f=>keys[f] || f);
}