13
1
Description
A Caesar Shift is a very simple monoalphabetic cipher where each letter gets replaced by the one after it in the alphabet. Example:
Hello world! -> IFMMP XPSME!
(IBSLR, EGUFV!
is the output for the actual challenge, this was an example of shifting by 1.)
As you can see, spacing and punctuation remain unattuned. However, to prevent guessing the message, all letters are capitalized. By shifting the letters back, the message was deciphered, convenient, but also really easy to decipher by other persons who are supposed not to know what the message means.
So, we'll be helping Caesar a bit by using an advanced form of his cipher: the Self-shifting Caesar Shift!
Challenge
Your task is to write a program or function, that, given a string to encipher, outputs the encrypted string corresponding to the input. The advanced Caesar Shift works like this:
1. Compute letter differences of all adjacent letters:
1.1. Letter difference is computed like this:
Position of 2nd letter in the alphabet
-Position of 1st letter in the alphabet
=======================================
Letter difference
1.2. Example input: Hello
H - e|e - l|l - l|l - o
7 - 5|5 - 12|12 - 12|12 - 15 Letter differences: 3; -7; 0; -3
=3| =-7| =0| =-3
2. Assign the letters continously a letter difference from the list,
starting at the second letter and inverting the differences:
2.1. 2nd letter: first difference, 3rd letter: second difference, etc.
2.2. The first letter is assigned a 1.
2.3. Example input: Hello with differences 3; -7; 0; -3
Letter || Value
=======||======
H || 1
E || -3
L || 7
L || 0
O || 3
3. Shift the letters by the value x they have been assigned:
3.1. In case of a positive x, the letter is shifted x letters to the right.
3.2. In case of a negative x, the letter is shifted |x| letters to the left.
3.3. In case of x = 0, the letter is not shifted.
3.4. If the shift would surpass the limits of the alphabet, it gets wrapped around
Example: Y + Shift of 2 --> A
3.5. Example input: See the table under 2.3.
|| || Shifted
Letter || Value || Letter
=======||=======||=========
H || 1 || I
E || -3 || B Program output:
L || 7 || S IBSLR
L || 0 || L
O || 3 || R
Spaces and other special symbols, such as punctuation are skipped in this process. It is guaranteed that your program will be given a string containing only printable ASCII characters. The output of your function/program must only be in upper case.
This is code-golf, so standard loopholes apply, and may the shortest answer in bytes win!
2Isn't
E
-3
? – Leaky Nun – 2017-07-18T16:25:04.2133What if the letter difference brings the letter out of the alphabet? Like
ZEN
, for example.Z
shifted by 1 is...A
? (as a side note, the 05AB1E answer turnsZ
intoA
) – Mr. Xcoder – 2017-07-18T17:00:04.7376Test cases please. Also, which characters are skipped exactly? And what does it mean for them to be skipped? Are they removed altogether, or must they remain in the output? – Luis Mendo – 2017-07-18T17:08:54.303
Is it just me or is
Hello, World!
actuallyIBSLR, EGUFV!
? – Magic Octopus Urn – 2017-07-18T17:16:26.590@Mr.Xcoder I assumed it would wrap, as the ceaser cipher traditionally doesn't use numerals or symbols. – Magic Octopus Urn – 2017-07-18T17:29:52.950
Why did you vote to close? I will gladly adjust my challenge if it is a minor problem. – racer290 – 2017-07-18T17:56:52.623
@racer290 generally a challenge is VTCd if there is anything important unclear, and then once that is cleared up, it is reopened. Since answerers couldn't correctly answer until they heard a verdict on wrapping, they VTCd. Now that you've fixed it, it won't be closed. – Stephen – 2017-07-18T18:01:08.793
So to be clear, punctuation is skipped, so
HELLO, WORLD
andHELLOWORLD
should have the same result (minus punctuation)? As Luis Mendo has asked, could we have a few more test cases? – Giuseppe – 2017-07-18T18:51:48.3901@Giuseppe see the upvoted answers for test cases, they've been validated by OP as correct, I'd assume, or they'd have downvotes. – Magic Octopus Urn – 2017-07-18T19:15:17.043
2Did you mean for words like
RELIEF
andRELIES
to both encipher to the same resultSRSFAG
? – Anders Kaseorg – 2017-07-18T19:50:59.837Next time, you can use the sandbox to get feedback on your challenges before posting them.
– programmer5000 – 2017-07-18T21:13:12.540@AndersKaseorg The cipher is such that plaintext isn't recoverable. In order to decypher the second character you need to know what the second character was (as the offset for it is the difference between the first character and the second). – Draco18s no longer trusts SE – 2017-07-19T14:21:36.050
@Draco18s it's actually only the modularity which makes this cipher unrecoverable. For each letter in the cipher text, there are only two ways to arrive there (either ' directly' or through modular wrapping), so there are only
2^n
plain text candidates for each lengthn
word in the cipher text, making this scheme a joke as both an attacker and the intended target need to spend the same amount of effort to decipher the code. – Sanchises – 2017-07-20T11:43:25.857@Sanchisesabout Ah, yes, you are correct. :) – Draco18s no longer trusts SE – 2017-07-20T13:12:32.227
More examples please! You should at least add one verbose example with non-letters or provide more detailed instructions on how to handle them. Looking at some of the answers, I noticed that this seems unclear: A) They don´t get converted. B) They are removed. C) They are neither converted nor considered for the shifting. – Titus – 2018-05-01T17:15:58.030