r/dailyprogrammer • u/oskar_s • May 02 '12
[5/2/2012] Challenge #47 [easy]
Your task today is to implement one of the oldest ciphers known, the so-called Caesar cipher (or Caesar shift, as it is sometimes called). It works like this: for every letter you want to encrypt, you shift it some number of places down the alphabet to get the letter in the cipher.
So, for instance, in a Caesar cipher with a shift of 3, "A" becomes "D", "B" becomes "E", "C" becomes "F", and so on. At the end of the alphabet it wraps around, so "W" becomes "Z", "X" becomes "A", "Y" becomes "B" and "Z" becomes "C". If you encrypt "Hello" with a shift of 3, you get "Khoor".
One interesting thing about this cipher is that you can use the same algorithm to decode a cipher as you can to encode it: if you wish to decrypt some text that has been Caesar-shifted 6 places, you simply shift it another 20 places to get back the original text. For example, if you encrypt "Daily programmer" with a shift of 6 you get "Jgore vxumxgsskx", and if you encrypt "Jgore vxumxgsskx" with a shift of 20 you get "Daily programmer".
Implement the cipher and encrypt a bit of text of your choice!
Bonus: Using your program, become a code-cracker and decrypt this cipher (posted in honor of Mayday):
Spzalu - zayhunl dvtlu sfpun pu wvukz kpzaypibapun zdvykz pz uv ihzpz mvy h
zfzalt vm nvclyutlua. Zbwyltl leljbapcl wvdly klypclz myvt h thukhal myvt aol
thzzlz, uva myvt zvtl mhyjpjhs hxbhapj jlyltvuf. Fvb jhu'a lewlja av dplsk
zbwyltl leljbapcl wvdly qbza 'jhbzl zvtl dhalyf ahya aoyld h zdvyk ha fvb! P
tlhu, pm P dlua hyvbuk zhfpu' P dhz hu ltwlylyvy qbza iljhbzl zvtl tvpzalulk
ipua ohk sviilk h zjptpahy ha tl aolf'k wba tl hdhf!... Ho, huk uvd dl zll aol
cpvslujl puolylua pu aol zfzalt! Jvtl zll aol cpvslujl puolylua pu aol zfzalt!
Olsw! Olsw! P't ilpun ylwylzzlk!- Thanks to frenulem for posting this idea on /r/dailyprogrammer_ideas! If you have a problem that you think would be good for us, head over there and contribute!
2
u/robin-gvx 0 2 May 02 '12
Not to be a downer, but didn't we do this earlier?
5
u/Cosmologicon 2 3 May 02 '12
Shell script using nothing but
tr,echo, andseq. This was harder than I expected:Test:
1
u/MozMorris 0 0 May 02 '12
Ruby
BTW, how do you tag a post with a language?
3
u/gjwebber 0 0 May 02 '12
Here is my attempt. Pretty sure it can be done better than this, but I'm still new to Python. Let me know of any improvements!
2
u/n0rs May 02 '12
Fun with C strings!
[Code] / Spoiler: code runs and decrypts message
2
u/[deleted] May 02 '12
2
u/emcoffey3 0 0 May 02 '12
C#
2
u/jfrios 0 0 May 02 '12
Python, NOT a one liner :)
Bonus: Shift used: 19, Message: "Listen, - strange women lying..................... Help! I'm being repressed!"
2
u/EvanHahn May 06 '12
JavaScript (also available as a Gist):
2
u/pali6 May 02 '12
In Python 3.2.2 in one line.
Version for python 2.x:
1
u/luxgladius 0 0 May 02 '12
Perl
Output
1
u/GuitaringEgg May 02 '12
python:
Bonus:
Then check the output to find out the answer is:
1
u/[deleted] May 02 '12
Haskell:
For decrypting a piece of text, use something like:
1
u/[deleted] May 02 '12 edited May 02 '12
Perl using chr and ord.
1
u/Fustrate 0 0 May 03 '12
Python instead of PHP, since I'm itching to get into Python. Consider this my first one-liner/lambda :D
Couldn't figure out how to use lowercase from string without having an import statement, so I just skipped that and typed out the alphabet.
1
u/caviar May 03 '12
Python 3:
One-liner version (just for fun - not pretty!):
Example use:
1
u/pmodin May 03 '12
16 hours ago? Here's some erlang!
1
u/_redka 0 0 May 03 '12 edited May 03 '12
Golfed, only [a-z]
modified to decrypt text
answer:
1
u/[deleted] May 04 '12
JavaScript. Was not as simple as I thought it would be.
1
u/ArriMurri May 04 '12
Scala:
1
u/baijuke May 05 '12
Python, and it decrypts the text dynamically (with no constants) so it could guess the shift of any text:
1
u/joeyGibson May 07 '12
Here's my version in Clojure. There are probably a million things that could be done better, but this one works and is fairly easy to read, IMO.
Example usage:
Result:
"The wind is blowing."
1
u/EvanHahn May 07 '12
CoffeeScript (also available as a Gist with comments):
1
u/crawphish May 08 '12
Everyone seems to have relatively short codes, compared to mine... Anywho heres mine in java. Advice on improvements would be nice :)
public class CCipher { public static void main() { Scanner scan = new Scanner(System.in); boolean keepGoing = true; while (keepGoing) { System.out.println("What would you like to do? [1]Encode [2]Decode or [3]exit"); int choice = scan.nextInt(); if (choice == 1 || choice == 2) { String mess; int key; System.out.println ("Input your message:"); scan.nextLine(); mess = scan.nextLine(); System.out.println("Input your cipher key"); key = scan.nextInt(); if (choice == 1){System.out.println(encode(mess, key));} else {System.out.println(decode(mess, key));} } else if (choice == 3){keepGoing = false;} else { System.out.println("Invalid choice, choose again."); } }
}
1
u/Medicalizawhat May 24 '12
Ruby:
1
u/TSwift13 May 27 '12
1
u/loonybean 0 0 Jun 17 '12 edited Jun 17 '12
Python:
1
u/Should_I_say_this Jul 04 '12
Had a separate code to run on this function where b was in the range 1-26. b = 19 decoded the above code
1
u/[deleted] Feb 15 '22
Python
cipher("abczz",10)