Задача: прочитать ввод произвольной длины и получить ROT13 ввода. Все символы, кроме AZ, должны быть дословно скопированы в выходной файл, и регистр должен быть сохранен, если это возможно.
Любой язык, который может читать и писать стандартные потоки, является честной игрой.
code-golf
cryptography
MiffTheFox
источник
источник
Ответы:
Баш, 23 байта
Канонический 23-символьный ответ:
источник
tr A-za-m N-ZA-z
(16 chars)[\\]^_`
in the input. It will come back asNOPQRS
rather than[\\]^_`
, at least in the version oftr
I have. (Those are the six characters in ASCII that lie betweenZ
anda
. Obviously, all other characters will work correctly.)Bash - 5 chars
источник
bash --version GNU bash, Version 4.0.33(1)-release (i486-pc-linux-gnu)
rot13 - 0 chars
... ;)sudo apt-get install bsdgames
"Python 2, 34 bytes
источник
Befunge -
7x30 = 2106x26 = 156 charactersNew streaming version that supports both upper and lower case and should support unlimited input.
The old version
This stores the values inside its own source code. Really shows off how horrible it is to try and output stored values in the same order that you receive them. Only supports lowercase characters.
I'm not sure exactly what limitations this has, using http://www.quirkster.com/iano/js/befunge.html as the interpreter it does appear to break with large inputs.
источник
Ruby -
60 57 3837 charsEdit: And just realised Ruby strings have a
tr
method.Test
Gives:
источник
puts
, and 'A-z' is a shortcut for 'A-Za-z''A-z'
is actually'A-Z[\]^_
a-z', damn ascii having characters between
Z` anda
.puts gets.tr'A-Za-z','N-ZA-Mn-za-m'
.gets
only returns the first line, using $<.read reads until EOF. The question doesn't say anything about whether the input can contain new lines so I just erred on the side of caution.vim, 5 keystrokes
Assuming normal mode and that the text is already written in the buffer:
ggg?G
Or, fallowing vimgolf's conventions:
g?GZZ
You can also invoke it as a terminal command, something like this:
I guess the latter would count as a "program" of 8 characters (
norm g?G
)источник
norm g?G
is short fornormal g?G
that makes 8 chars.gg
can be left off. I would say 3 keystrokes when the file is open.g?GZZ
).C -
8379 charactersReadable version:
источник
Python (117 bytes)
Here's a Python version that avoids the
rot13()
-method.источник
import sys
and usesys.stdin.read()
.[]
to make the list comp a generator: tio.run/…tr///
solution in Perl (39 characters), boilerplate can be removed with-p
:Using
-p
(23 characters including the extra switch):источник
R, 37 bytes
example("chartr")
runs the examples forchartr
, which includes therot
function, which isROT13
by default....источник
DC (
111108 for the dc itself)Ok, here it is in (mostly) dc and some sed and od magic to get it into the right format for the code. If you don't count the input thing (
echo -n MESSAGE |
) it's 160 bytes:As a point of interest, the dc programme itself is only a 108 bytes long, shorter than the non-library python version. It even preserves case and punctuation, and beats Javascript in the above submission! If only I could better parse the output of od, or better yet replace it altogether.
EDIT: It's worth noting that the question doesn't indicate a trailing new line
10P
which saves me three further bytes.EDIT 2: There's no specification for the format of the input, so I assume it's taken in as is convenient for my programme :P
источник
Befunge-93, 85 (grid: 41x3=123)
This is an ancient question, but I thought I'd revive it to post a slightly nicer Befunge answer.
You can test it here. Enter a single character at a time; it terminates upon entering a
.
character (you can change that by modifying the"."
near the right side of the second row). Works with upper and lower case, as well as punctuation, and with no limit to input.I don't expect this to get a ton of upvotes or anything, but I just wanted to demonstrate
how awesome Befunge actually isthat you can do a little better than the other answer.I could probably make it even shorter in Befunge-98.
источник
>$! _
because you've got two zeros on the stack at that point when you are expecting a non-zero value.PHP -
1039880 characters(not using str_rot13())
источник
Delphi, 110
источник
var c:Char;begin repeat Read(c);Write(Chr(Ord(c)+(Ord(c in['A'..'M'])-Ord(c in['N'..'Z']))*13));until EOF;end.
saves one character :)Haskell, 100 characters
источник
Perl6 (54)
источник
Java 251 chars
источник
Python 3 (107)
Ok, I promise to stop answering this question now, but I felt compelled to beat the DC answer in Python. This probably reflects poorly on me as a person :).
источник
C:
6968 charactersAlright, I know this thread is long dead, but I couldn't stand the (long) C-solution which doesn't even compile on Clang (but does on GCC).
It is probably almost still squeezable.It certainly was squeezable. And not only was it squeezable, it was possible to make it recursive.источник
05AB1E,
1312 bytesSaved a byte thanks to robbie0630
Try it online!
Explanation
источник
--debug
, and it seems like the˜
is a no-op in this case and can be cut out.PHP - 41 Characters
источник
JavaScript 1.8, 106
alert(prompt().replace(/\w/g,function(c)String.fromCharCode(c.charCodeAt()+(c.toLowerCase()<'n'?13:-13))))
JavaScript, 115
alert(prompt().replace(/\w/g,function(c){return String.fromCharCode(c.charCodeAt()+(c.toLowerCase()<'n'?13:-13))}))
This solution solves the problem by adding 13 to the character code if the character in question is in the first half of the alphabet, or subtracting 13 if it's in the second half.
источник
+(c.toLowerCase()<'n'?13:-13))
with-13+26*/[a-m]/i.test(c)
.CHIQRSX9+, 1
You just have to use the right tool for the problem.
CHIQRSX9+ is Turing complete, and it can read and write from standard channels with
C
.источник
C, 136 bytes
I have never felt like any of my solutions are good enough to post on here, but made this for fun, and figured that it will be my gateway drug into code golf.
источник
Javascript, 177 bytes
This assumes that there are two functions, print and readLine:
источник
LispLisp (16,636)
I'm sorry.
источник
8086 Machine Code, 27 bytes
Unassembled:
Input string in
SI
, length inCX
. Output string buffer atDI
.Test IBM PC DOS program output:
Download R13.COM test program (PC DOS).
источник
Haskell - 112 characters
источник
K, 31
источник
{x^a(,/-13 13#\:a:.Q.A)?x}
for 26 bytesTcl, 74 chars
источник