12
2
Challenge
The challenge is to encrypt a given string, using the rules as specified below. The string will only contain lowercase alphabets, digits, and/or blank spaces.
Equivalent of a Character
Now, firstly you would need to know how to find the "equivalent" of each character.
If the character is a consonant, this is the way of finding it's equivalent:
1) List all the consonants in alphabetical order
b c d f g h j k l m n p q r s t v w x y z
2) Get the position of the consonant you are finding the equivalent of.
3) The equivalent is the consonant at that position when starting from the end.
eg: 'h' and 't' are equivalents of each other because 'h', 't' are in the 6th position from start and end respectively.
The same procedure is followed to find the equivalent of vowels/digits. You list all the vowels or the digits (starting from 0) in order and find the equivalent.
Given below is the list of the equivalents of all the characters:
b <-> z
c <-> y
d <-> x
f <-> w
g <-> v
h <-> t
j <-> s
k <-> r
l <-> q
m <-> p
n <-> n
a <-> u
e <-> o
i <-> i
0 <-> 9
1 <-> 8
2 <-> 7
3 <-> 6
4 <-> 5
Rules of Encrypting
1) You start moving from the left and go towards the right.
2) If the character is a consonant/digit, then its equivalent is taken and if it is a blank space, then a blank space is taken.
3) If the character is a vowel, you take it's equivalent and start to move in the opposite direction. For example, if you are moving right and encounter a vowel, encrypt that character then skip to the rightmost unencrypted character and begin encrypting in the left direction, and vice versa.
4) You shouldn't consider a character in the same position twice. The steps should be followed until all the characters in the input are covered.
5) The total number of characters in the input(including blank spaces) should be equal to the total number of characters in the output.
Please note that the encrypted characters appear in the output in the order in which they were encrypted.
Now let me encrypt a string for you.
String = "tre d1go3t is"
Moving left to right
"t" -> "h"
"r" -> "k"
"e" -> "o"
Vowel encountered. Now moving right to left.
"s" -> "j"
"i" -> "i"
Vowel encountered. Now moving left to right.
" " -> " "
"d" -> "x"
"1" -> "8"
"g" -> "v"
"o" -> "e"
Vowel encountered. Now moving right to left.
" " -> " "
"t" -> "h"
"3" -> "6"
Output -> "hkoji x8ve h6"
Examples
"flyspy" -> "wqcjmc"
"hero" -> "toek"
"heroic" -> "toyike"
"ae" -> "uo"
"abe" -> "uoz"
"the space" -> "htoo jmuy"
"a d1g13t" -> "uh68v8x "
"we xi12" -> "fo78i d"
"this is a code" -> "htioj ixej uy "
You may also choose to use uppercase alphabets instead of lowercase.
Scoring
This is code-golf, so the shortest code wins!
1Step 3 is a bit unclear with regards to switching directions. I think you should say something like "If you are moving right and encounter a vowel, encrypt that character then skip to the rightmost unencrypted character and begin encrypting in the left direction." (If that is what you mean). I think you should also specify explicitly that the encrypted characters appear in the output in the order in which they were encrypted. – dylnan – 2018-02-08T01:11:24.363
@dylnan Added that. – Manish Kundu – 2018-02-08T01:14:54.730
Just out-of-curiosity - Can you describe decryption procedure ? Because encryption function is not it's own inverse (like in ROT13 algo). So if we pass an encrypted data to the same cipher procedure - we will not get the original text. Thanks – Agnius Vasiliauskas – 2018-02-08T09:30:34.460
Decryption can be done in the opposite way. You will get the original text on decrypting – Manish Kundu – 2018-02-08T09:57:05.213
1@AgniusVasiliauskas: One way of doing it would be: Apply the same character transformations. Keep 2 decrypt strings. Loop over the string left-to-right. Alternate between appending chars to the first string and prepending to the second every time you handle a vowel. Merge the strings at the end. – Emigna – 2018-02-08T10:00:03.933
3There will soon be a decryption challenge for the same, in which I will try to explain the process – Manish Kundu – 2018-02-08T10:38:19.807
For the curious, in Indian English alphabet means letter. – TRiG – 2018-02-08T11:22:39.450
Who downvoted, And why? – Manish Kundu – 2018-02-08T13:42:02.587