13
2
Implement this key cipher
Goal
Use the algorithm (explained in the Algorithm section) to implement a certain cipher.
The program must read input from STDIN or the closest available equivalent, use the algorithm to generate the ciphertext and a key.
The ciphertext and the key will be written to STDOUT or the closest available equivalent. Any format is allowed, as long as it outputs the ciphertext and the key.
Algorithm
Convert the characters in the string into the respective ASCII values. For example:
Hello -> 72 101 108 108 111
Next, you will need to generate a key as long as the string with random numbers in the range of 0-9.
Hello -> 62841
Add the integers in the random number sequence to the ASCII values of the string. In the above examples, 72 would become 78, and 101 would become 104.
72 + 6 = 78, 101 + 2 = 103, 108 + 8 = 116, etc
Next, convert the new values back to characters. In the above examples, the text Hello
has become Ngtpp
.
Examples
(These are simply examples of what the output might look like. The output can and will vary.)
Hello World
Lfrlu)_supg
41606984343
This will be encoded
Zhjs$~koo gj$iuhofgj
60104723305544750226
Rules
- You can assume that the input will only contain characters in the range a-z, A-Z, and spaces.
- Submissions must be full programs or functions.
- Submissions will be scored in bytes.
- Standard loopholes are forbidden.
- This is code-golf, so the shortest code wins.
(This is one of my first challenges, if there's something wrong with it, feel free to tell me how I could improve it.)
5This challenge looks good to me, except for a couple of thoughts. 1. Is a function allowed instead of a full program? A related question is could the values be returned instead of printed? 2. You said
preferably with the format (ciphertext)\n(key).
"preferred features" and code golf don't mix very well. Your should make that mandatory or allow other output formats. 3. Does the key have to be printed without spaces? What about printing it in list format, e.g.[0, 5, 2, ...]
? – James – 2016-08-19T13:48:47.160Can the key have leading zeroes? – TheBikingViking – 2016-08-19T13:53:51.590
Can I print the key first and then the cipher text? – Maltysen – 2016-08-19T13:54:36.503
@DJMcMayhem I updated the challenge. – m654 – 2016-08-19T14:00:22.590
@TheBikingViking Updated. – m654 – 2016-08-19T14:00:32.263
@Maltysen Yes, you can. They have to be separated with a newline though. – m654 – 2016-08-19T14:00:54.763
@TimmyD Yep, it was a typo. I'll update it – m654 – 2016-08-19T14:01:23.027
@DJMcMayhem Well, I changed the rules a little, you can post it now. – m654 – 2016-08-19T15:26:00.840
@m654 Thanks! Generally it's best to go with a very permissive IO format. I've actually already posted it, but I'll edit it to be shorter now. – James – 2016-08-19T15:28:57.050
1Nice first challenge but I'm not so sure on the strict IO formats. Usually functions are allowed and usually answers can read from one of the accepted IO methods. This includes outputting an array with the items – Downgoat – 2016-08-19T15:54:45.263
1Do the digits of the key have to be generated with an uniform distribution? – Dennis – 2016-08-19T15:58:11.377
I made the format more permissive. @Dennis, no, it doesn't have to. Pseudorandom numbers are alright. – m654 – 2016-08-19T16:15:47.437
Pseudo-random is not the same as not uniform. A non-uniform distribution could generate 0 with probability 1/2, 1 with probability 1/3, etc. – Dennis – 2016-08-19T16:16:48.083
Oh, by the way, convert the new ASCII values back to characters isn't entirely correct. If you add 9 to an ASCII code point, the result could be an integer larger than 127, so it would fall outside the ASCII range. Also, you say the input will only contain characters in the range a-z and A-Z, but your examples have spaces. – Dennis – 2016-08-19T16:18:44.490
@Dennis ._. Fixed. – m654 – 2016-08-19T16:20:52.847
1Uh ... 101 + 2 is 103, not 104. :-) – YetiCGN – 2016-08-19T19:27:07.660
@YetiCGN Haha, sorry. It was a typo. – m654 – 2016-08-20T06:28:13.303
I though as much. ;-) You still have 104 in the text, in the line above it. – YetiCGN – 2016-08-20T07:48:32.933