r/codegolf • • Jun 01 '14

[perl] Reversible Substitution Algorithm

I propose a new challenge.

write a program in the shorted amount of characters which:

  1. Takes an input string
  2. Performs 3 different substitution on the string (swapping every two characters, reversing the string, replacing all characters with equivalent hex values, etc, be creative)
  3. Perform a 1/2 rotation substitution. Essentially each letter maps to the 13 letter over in the alphabet
  4. The algorithm must be reversible. Meaning I enter a string like "hello" and get some nonsense like "ryubb", and when I make "ryubb" the input I should get "hello".
1 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/zifyoip Jun 01 '14

Hmm. On second thought, it might not be that difficult—you just need to find a set of involutions that commute under composition. So you could use ROT-13, case-flipping, reversing the string, and swapping the first and last characters of the string, for example:

while (<>) {
	chomp;
	$_ = substr($_,0,1).reverse(substr($_,1,-1)).substr($_,-1) if length > 3;   
	$_ =~ tr/A-Za-z/n-za-mN-ZA-M/;
	print "$_\n";
}

Of course, that hasn't been codegolfed.