Le théorème du singe infini stipule que, dans un temps infini, une machine qui envoie un flux infini de caractères aléatoires dactylographiera toujours un texte donné.
Cela me semble être une excellente idée pour un défi.
Processus
Pour monkey-ize une chaîne A, les étapes suivantes doivent être suivies:
- Prenez une chaîne vide. Nous appellerons cette chaîne B.
- Choisissez un caractère ASCII imprimable uniformément aléatoire (caractères dans la plage
0x20
à0x7E
) et d' ajouter que le caractère à B. - Si A est une sous-chaîne de B, B est notre chaîne monkey-ized. Sinon, répétez l'étape 2 jusqu'à ce que A soit une sous-chaîne de B.
Ce processus n'est qu'un exemple, des méthodes plus simples peuvent exister selon votre langue. Vous n’avez pas besoin de suivre cette méthode à la lettre, à condition d’obtenir la même répartition des sorties.
Le défi
Ecrivez un programme ou une fonction qui, avec une chaîne non vide dans un format raisonnable , retourne une version monkey de cette chaîne.
Votre programme ne doit pratiquement fonctionner que pour des entrées de longueur inférieure ou égale à 3. Pour les entrées plus longues, il est permis de terminer tôt avec ou sans sortie.
Exemple
Malheureusement, il est un peu difficile de créer des exemples pour cette question en raison de sa nature aléatoire et de ses résultats importants.
Cependant, je peux fournir un seul exemple pour l'entrée hi
, sur Hastebin.
Notation
Puisqu'il s'agit de code-golf , la soumission contenant le moins d'octets gagne.
B
directement en ajoutant un nombre non négatifn
de caractères aléatoires àA
. Le seul problème réel est alors de connaître la distribution den
(je parie sur une distribution géométrique).W!}zH+ZOrd\k
, ressemblent beaucoup à ce qu'un singe a tapé.Réponses:
C, 192 bytes
Try it online!
It's a mess now, but at least it works even for the corner cases...
C,636261 bytesThanks to @Jonathan Frech for saving a byte!
Try it online!la source
i
grows large enough thats[i]
refers to the null terminator of the string (character 0).ababc
and the monkey generate!!abababc
will your program halt?Python, 79 bytes
Try it online!
This is theoretically sound, but will crash early due to python's recursion limits (you can set them further to get longer results)
Python, 84 bytes
Try it online!
This one is ought to work for relatively longer strings, since it doesn't rely on recursion, at the cost of 5 bytes.
la source
s+'randint(32,126)'
randint(32,126)
would produce a string of the number, not the ascii char mappingOhm v2, 10 bytes
Try it online!
Explanation:
la source
GNU sed + coreutils, 75 + 1(r flag) = 76 bytes
Try it online! (It takes a lot of runs to get an answer for a length 2 input, because most of the time you run out of allowed TIO computation time.)
Explanation:
Benchmark: approximate, for scaling purposes only
la source
Funky, 64 bytes
This uses a few tricks I've been wanting to use in Funky, like a variable name after a keyword as in
whileS
, and using the fact that strings implicitly parent to thestring
library.Ungolfed
Try it online!
la source
Haskell, 100 bytes
Try it online!
Basic idea is to generate an infinite list of characters with
randomRs
and stop it once we find the string.la source
isPrefixOf
isn't in the standard Prelude…C# (.NET Core), 86 bytes
I don't really like how much creating the
Random
instance takes, but I don't think there's a way around it.Try it online!
la source
Random.Next(Int32,Int32)
is exclusive and so not one of the numbers generated. This can be fixed by replacing126
by127
.Random
, you can remove the variable declaration! 79 bytesPerl 5, 31 +2 (-pa) bytes
Try it online
la source
\E$
is extraneousJapt, 26 bytes
Try it online!
la source
R,
797675 bytes-3 bytes thanks to MickyT for changing the random sampler
-1 byte thanks to Robin Ryder for tweaking the random sampler again
Try it online!
la source
intToUtf8(runif(1,32,127))
32+95*runif(1)
as your random sampler.Charcoal,
151412 bytesTry it online! Link is to verbose version of code. Edit: Saved 2 bytes due to a subsequent bug fix in Charcoal. Explanation:
la source
Ruby, 42 bytes
Try it online!
la source
Pyth - 14 bytes
Try it online here.
la source
W!}Qk=+kpOrd\
is 14 bytes as well, SE is messing with formatting because of unprintable but range is generated the same wayMathematica, 65 bytes
Try it online!
-3 bytes from Jonathan Frech
la source
FromCharacterCode[RandomInteger@94+32]
is equivalent to the shorterRandomChoice@CharacterRange[32,126]
.Lua,
99102 bytesTry it online!
la source
MATL,
1716 bytesTry it online!
-1 byte thanks to Giuseppe
la source
Octave, 62 bytes
Try it online!
Explanation:
Many thanks to Luis Mendo for the edits!
la source
isvector
bynnz
? Andstrfind
byregexp
. Also, you can userandi(95)+31
, or maybe replace the wholesprintf
statement byo=[o,randi(95)+31];
(implicit conversion to char)Japt,
161411 bytesTry it
la source
Alice, 21 bytes
Try it online!
Explanation
This is framework for mostly linear programs that operate entirely in Ordinal (string-processing) mode. The IP bounces diagonally up and down through the program twice, which means that the actual code is a bit weirdly interleaved. The commands in the order they're actually executed are:
Let's go through this:
la source
Perl 6, 39 bytes
Try it online!
(...)[*-1]
returns the last element of the sequence defined by...
, of which:""
is the first element;* ~ (" " .. "~").pick
generates the next element by appending a random character in the appropriate range to the previous element; and* ~~ /$_/
is the ending condition, which is that the current element matches the main function's input argument$_
as a literal substring.la source
*~~
for -3 tio.run/##K0gtyjH7n1upoJamYPu/…Java 8,
817978 bytes-1 byte thanks to @OlivierGrégoire for pointing me to a (big >.<) mistake I've made..
Explanation:
Try it here.
la source
32+Math.random()*95
. There... bug fixed and a byte saved! ;-)05AB1E,
109 bytes (-1 @ Emigna)Try it online!
Do the monkey with me.
la source
Ω
instead of.R
.QBIC, 33 bytes
Explanation
Sample run:
la source
PHP, 55+1 bytes
Run as pipe with
-nR
. Not suitable for TIO cause of probable timeout.Insert a space between the quotation marks for PHP older than 7.1.
This 51+1 bytes version will fail if input is
0
:la source
Javascript 74 bytes
call like this:
la source
~b.search
instead ofb.includes
.Julia 0.6, 53 bytes
Try it online!
la source
Pushy,
2018 bytesTry it online!
The program keeps a stack
len(input)
characters long, and constantly removes the first and appends a new random char, until the initial input string is reached. Each character is printed as it is added, creating the desired effect.Explanation:
la source
Brachylog, 17 bytes
Try it online!
Can randomly stack overflow. This makes use of two recently added features in Brachylog: global variables, and the apply-to-tail metapredicate
ᵗ
.la source
Pyth, 13 bytes
where the unprintable character is 0x7F.
Test
la source
Bash 94 bytes
Try it online
la source