Note
This is the encryption challenge. The decryption challenge can be found here.
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
You start moving from the left and go towards the right.
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.
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.
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.
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!
-
1\$\begingroup\$ Step 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. \$\endgroup\$dylnan– dylnan2018年02月08日 01:11:24 +00:00Commented Feb 8, 2018 at 1:11
-
\$\begingroup\$ @dylnan Added that. \$\endgroup\$Manish Kundu– Manish Kundu2018年02月08日 01:14:54 +00:00Commented Feb 8, 2018 at 1:14
-
1\$\begingroup\$ @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. \$\endgroup\$Emigna– Emigna2018年02月08日 10:00:03 +00:00Commented Feb 8, 2018 at 10:00
-
4\$\begingroup\$ There will soon be a decryption challenge for the same, in which I will try to explain the process \$\endgroup\$Manish Kundu– Manish Kundu2018年02月08日 10:38:19 +00:00Commented Feb 8, 2018 at 10:38
-
1\$\begingroup\$ For the curious, in Indian English alphabet means letter. \$\endgroup\$TRiG– TRiG2018年02月08日 11:22:39 +00:00Commented Feb 8, 2018 at 11:22
8 Answers 8
05AB1E, 22 bytes
vćžN‡žM‡žh‡D?žMsåiR
Try it online! or as a Test suite
Explanation
v # for each char in input
ć # extract the head of the current string (initially input)
žN‡ # transform consonants
žM‡ # transofrm vowels
žh‡ # transform numbers
D? # print a copy of the current char
žMsåi # if the current char is a vowel
R # reverse the rest of the string
-
\$\begingroup\$
žhžMžN)UvćXJXíJ‡D?žMsåiRwas what I was thinking for improvement, but can't cutXJXiJdown enough. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年02月08日 14:34:34 +00:00Commented Feb 8, 2018 at 14:34 -
\$\begingroup\$ @MagicOctopusUrn: I had a similar idea with
DJsíJwhich wasn't very effective either. \$\endgroup\$Emigna– Emigna2018年02月08日 15:02:01 +00:00Commented Feb 8, 2018 at 15:02
JavaScript (Node.js), (削除) 173 (削除ここまで) ... (削除) 166 (削除ここまで) (削除) 156 (削除ここまで) ... (削除) 124 (削除ここまで) 123 bytes
-28 byte Thanks Arnauld
f=([q,...s])=>q?(c="aeioubcdfghjklmpqrstvwxyz",t=c.search(q),q=="0"|+q?9-q:~t?c[(t<5?4:29)-t]:q)+f(~t&&t<5?s.reverse():s):s
In the first iteration the String will be changed to Array, and subsequent iterations will continue to use Array. Voilà!
Original Approach (166 bytes):
f=(s,i=0,r=s.length,d=1,c="bcdfghjklmnpqrstvwxyz",v="aeiou")=>(d^=!!(t=~v.search(q=s[d?i:r])),q<"0"|q>"9"?c[20-c.search(q)]||v[5+t]||q:9-q)+(i<r-1?f(s,i+d,r-!d,d):"")
-
\$\begingroup\$
&didn't work for some numbers but&&worked. Thanks. \$\endgroup\$Shieru Asakoto– Shieru Asakoto2018年02月08日 07:56:40 +00:00Commented Feb 8, 2018 at 7:56 -
\$\begingroup\$ Oh yes, I didn't find a method to optimize that and you did it! Thanks! \$\endgroup\$Shieru Asakoto– Shieru Asakoto2018年02月08日 08:07:54 +00:00Commented Feb 8, 2018 at 8:07
-
3
-
\$\begingroup\$ Wow brilliant! I didn't think of combining the strings AT ALL \$\endgroup\$Shieru Asakoto– Shieru Asakoto2018年02月08日 10:01:10 +00:00Commented Feb 8, 2018 at 10:01
-
\$\begingroup\$
q=="0"|+qis actually 1 byte shorter thanq>" "&&1/q. \$\endgroup\$Arnauld– Arnauld2018年02月08日 11:31:55 +00:00Commented Feb 8, 2018 at 11:31
C, 196 bytes
#define C(k,m)for(i=m;i--;)k[i]-c||putchar(k[m+~i],r^=m==5);
r;p(c,i){C("bcdfghjklmnpqrstvwxyz",21)C("0123456789",10)C("aeiou",5)C(" ",1)}f(S){char*s=S,*t=s+strlen(s);for(r=1;s<t;)p(r?*s++:*--t);}
J, 132 bytes
f=:3 :0
c=.(u:97+i.26)-.v=.'aeiou'
d=.u:48+i.10
g=.;"0|.
a=.''
while.*#y do.a=.a,{.y rplc(g c),(g d),g v
y=.|.^:({:a e.v)}.y
end.a
)
A verbose explicit verb this time.
Explanation:
c=.(u:97+i.26) makes a list a-z
v=.'aeiou' makes a list of vowels
-. removes the vowels from the list of letters
d=.u:48+i.10 makes a list of digits
g=.;"0|. a utility verb for creating a list of boxed pairs of replacement symbols
g d
┌─┬─┐
│0│9│
├─┼─┤
│1│8│
├─┼─┤
│2│7│
├─┼─┤
│3│6│
├─┼─┤
│4│5│
├─┼─┤
│5│4│
├─┼─┤
│6│3│
├─┼─┤
│7│2│
├─┼─┤
│8│1│
├─┼─┤
│9│0│
└─┴─┘
a=.'' a list to store the result
while.*#y do.a=.a,{.y rplc(g c),(g d),g v while the length of the list is> 0 take a symbol, replace it and append it to the result
y=.|.^:({:a e.v)}.y drop one symbol from the beginning of the list and if the symbol is a vowel, reverse the list
end. ends the while loop
a returns the result
Clean, (削除) 221 (削除ここまで) (削除) 206 (削除ここまで) (削除) 198 (削除ここまで) (削除) 190 (削除ここまで) (削除) 186 (削除ここまで) 178 bytes
import StdEnv
r=reverse
l=['bcdfghjklm01234aeou56789pqrstvwxyz']
$s#(a,b)=span(\e=all((<>)e)['aeiou'])s
|s>[]=[j\\e<-a++b%(0,0),i<-['in ':l]&j<-['in ':r l]|e==i]++ $(init(r b))=s
Stax, 24 bytes
╥j•td╢Ä;Sμ*ûnvÉ╫î▓J o╩π╗
Here is the ascii representation of the same program.
VcGVdGVvGwB]qVvs#!Hv*c}cr\$|t
It translates each character class first, then begins a while loop. In the loop, it outputs the next character, and conditionally reverses the rest of the string if a vowel is encountered.
VcG Push lowercase consonants and jump to trailing }
VdG Push digits and jump to trailing }
VvG Push lowercase vowels and jump to trailing }
wB]qVvs#!Hv*c While; run this block until popped value is falsy
B] Split first character off string
q Output with no newline; keep on the stack
Vvs# 1 if letter is a vowel, 0 otherwise
!Hv Not, Double, then Decrement
-1 for vowels, 1 otherwise
* Multiply string. -1 causes reversal
c Copy value to be popped as while condition
} Jump target from above. Return when done.
cr\$ Copy, reverse, zip, and flatten.
|t Translate: use string as a character map
for replacements
Brachylog, (削除) 43 (削除ここまで) 39 bytes
hX∧Ḅ;Ṿ,Ị,Ṣ∋C↔;Cz∋[X,H]&{h∈Ṿ&b↔|b}↰↔,H↔|
I only bothered finishing this grand mess because I'm struggling to make Jelly cooperate...
Retina, (削除) 78 (削除ここまで) 69 bytes
T`b-df-hj-maed\oup-tv-z`Ro
/[aeiou]/{*>0L`.*?[aeiou]
0`.*?[aeiou]
V`
Try it online! Link includes test cases. Explanation:
T`b-df-hj-maed\oup-tv-z`Ro
Swap each character with its equivalent. The Ro causes the string to transliterate to its reverse. The middle consonant n and vowel i map to themselves so don't need to be listed. The first 10 consonants are listed first and the last 10 last so that they transliterate to each other. The first and last two vowels then surround the digits. (The o is normally special so it has to be quoted here.)
/[aeiou]/{
Repeat while a vowel remains.
*>0L`.*?[aeiou]
Output the text up to the vowel.
0`.*?[aeiou]
Delete the text up to the vowel.
V`
Reverse the remaining text. When there are no vowels left, this is then implicitly output, however for the purposes of the test cases, the header outputs the text at the end of each line.