Daily Challenge (162 Part Series)
Your challenge is to implement an Iterative Rotation Cipher (IRC). To complete the challenge, create an object with two methods,
encode will receive two arguments, a positive integer
n and a string value.
decode will receive one argument - a string value.
Each method should return a string value. Encoding and decoding are done by performing a series of character and substring rotations on a string input.
Encoding: The number of rotations is determined by the value of n. The sequence of rotations is applied in the following order:
Step 1: Remove all spaces in the string (but remember their positions)
Step 2: Shift the order of characters in the new string to the right by n characters
Step 3: Put the spaces back in their original positions
Step 4: Shift the characters of each substring (separated by one or more consecutive spaces) to the right by n
Repeat this process until it has been completed n times in total.
The value n is then prepended to the resulting string with a space.
Decoding: Decoding simply reverses the encoding process.
let quote = `If you wish to make an apple pie from scratch, you must first invent the universe.`; let solution = `10 hu fmo a,ys vi utie mr snehn rni tvte .ysushou teI fwea pmapi apfrok rei tnocsclet`; IterativeRotationCipher.encode(10,quote) === solution; //true /* Step-by-step breakdown: Step 1 — remove all spaces: `Ifyouwishtomakeanapplepiefromscratch,youmustfirstinventtheuniverse.` Step 2 — shift the order of string characters to the right by 10: `euniverse.Ifyouwishtomakeanapplepiefromscratch,youmustfirstinventth` Step 3 — place the spaces back in their original positions: `eu niv erse .I fyou wi shtom ake anap plepiefr oms crat ch,yo umustf irs tinventth` Step 4 — shift the order of characters for each space-separated substring to the right by 10: `eu vni seer .I oufy wi shtom eak apan frplepie som atcr ch,yo ustfum sir htinventt` Repeat the steps 9 more times before returning the string with `10 ` prepended. */
True evil is a mundane bureaucracy.],
There is nothing more atrociously cruel than an adored child.]
hu fmo a,ys vi utie mr snehn rni tvte .ysushou teI fwea pmapi apfrok rei tnocsclet]
ws h weA dgIaa ug owh n!asrit git ]
msm phw teaI'e tanantwhe reos
s ther! aHeae 'gwadin
t haw n htoo ,I'i sy aohOy
Want to propose a challenge idea for a future post? Email email@example.com with your suggestions!