---------------------------------------------------
The Vigenere cipher is a simple form of a polyalphabetic substitution, using a series of different Caesar ciphers based on the letters of a keyword. It's over 450 years old now, but that shouldn't stop us having a bit of golfing fun with it now.
The following information is largely taken from Wikipedia's Vigenere cipher page. You might find some interesting information that isn't included here, so I recommend you check it out.
Your job is to write some code which takes a keyword and some plaintext, and encrypts it according to the Vigenere cipher.
In a Caesar cipher, each letter of the alphabet is shifted along some number of places; for example, in a Caesar cipher of shift 3, A would become D, B would become E and so on. The Vigenere cipher consists of several Caesar ciphers in sequence with different shift values.
To encipher, a table of alphabets can be used, termed a tabula recta, Vigenere square, or Vigenere table. It consists of the alphabet written out 26 times in different rows, each alphabet shifted cyclically to the left compared to the previous alphabet, corresponding to the 26 possible Caesar ciphers. At different points in the encryption process, the cipher uses a different alphabet from one of the rows. The alphabet used at each point depends on a repeating keyword.
Click here to see the Vigenere square.
See the examples section below for a worked example.
More Information
* Your code will be given both the keyword and the plaintext on stdin. It will be in the format
KEYWORD
PLAINTEXT
with newlines at the end of each line.
* You should print the encrypted plaintext on stdout.
* Both the keyword and the plaintext will contain only the uppercase letters A through Z.
* Your code will be run three times and will need to pass all three tests to be deemed successful.
---------------------------------------------------
Code golf challenge is interesting because it measures your ability to write code as short as possible. My try to give shortest solution in Python:
Run no 1 -> code size 111 bytes:
k=raw_input()*100;p=raw_input()
print ''.join(map(lambda x, y: chr(65+(ord(x)+ord(y)-130)%26), k[:len(p)], p))
Run no 2 -> code size 84 bytes:
i=raw_input;print ''.join(chr(65+(ord(x)+ord(y)-130)%26) for x,y in zip(i()*10,i()))
Have fun!
Code golf
No comments:
Post a Comment
Comment will be posted after comment moderation.
Thank you for your appreciation.