O desafio: Ler uma entrada de comprimento arbitrário e produzir o ROT13 da entrada. Todos os caracteres além de AZ devem ser copiados para a saída literalmente, e maiúsculas e minúsculas devem ser preservadas, se possível.
Qualquer idioma que possa ler e escrever fluxos padrão é um jogo justo.
code-golf
cryptography
MiffTheFox
fonte
fonte
Respostas:
Bash, 23 bytes
Resposta canônica de 23 caracteres:
fonte
tr A-za-m N-ZA-z
(16 caracteres)[\\]^_`
a entrada. Ele vai voltar comoNOPQRS
em vez de[\\]^_`
, pelo menos, na versão dotr
que eu tenho. (Esses são os seis caracteres de ASCII que se encontram entreZ
ea
Obviamente, todos os outros personagens irá funcionar corretamente..)Bash - 5 caracteres
fonte
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
fonte
Befunge -
7x30 = 2106x26 = 156 caracteresNova versão de streaming compatível com maiúsculas e minúsculas e com entrada ilimitada.
A versão antiga
Isso armazena os valores dentro de seu próprio código-fonte. Realmente mostra como é horrível tentar gerar valores armazenados na mesma ordem em que você os recebe. Suporta apenas caracteres minúsculos.
Não sei exatamente quais limitações isso tem, usando http://www.quirkster.com/iano/js/befunge.html como intérprete que parece romper com entradas grandes.
fonte
Rubi -
60 57 3837 charsEdit: And just realised Ruby strings have a
tr
method.Test
Gives:
fonte
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
)fonte
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:
fonte
Python (117 bytes)
Here's a Python version that avoids the
rot13()
-method.fonte
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):fonte
R, 37 bytes
example("chartr")
runs the examples forchartr
, which includes therot
function, which isROT13
by default....fonte
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
fonte
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.
fonte
>$! _
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())
fonte
Delphi, 110
fonte
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
fonte
Perl6 (54)
fonte
Java 251 chars
fonte
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 :).
fonte
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.fonte
05AB1E,
1312 bytesSaved a byte thanks to robbie0630
Try it online!
Explanation
fonte
--debug
, and it seems like the˜
is a no-op in this case and can be cut out.PHP - 41 Characters
fonte
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.
fonte
+(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
.fonte
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.
fonte
Javascript, 177 bytes
This assumes that there are two functions, print and readLine:
fonte
LispLisp (16,636)
I'm sorry.
fonte
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).
fonte
Haskell - 112 characters
fonte
K, 31
fonte
{x^a(,/-13 13#\:a:.Q.A)?x}
for 26 bytesTcl, 74 chars
fonte