r/dailyprogrammer • u/Cosmologicon 2 3 • Oct 25 '12
10/25/2012] Challenge #107 [Intermediate] (Infinite Monkey Theorem)
Verify the Infinite Monkey Theorem.
Well that's a bit hard, so let's go with this. Using any method of your choice, generate a random string of space-separated words. (The simplest method would be to randomly choose, with equal probability, one of the 27 characters including letters and space.) Filter the words using a word list of your choice, so that only words in the word list are actually output.
That's all you need for the basic challenge. For extra points, run your program for a few minutes and find the most interesting string of words you can get. The longer the better. For style, see if you can "train your monkey" by modifying either the random character generator or the word list to output text that's more Shakespearean in less time.
Thanks to Pikmeir for posting this idea in /r/dailyprogrammer_ideas!
9
u/Cosmologicon 2 3 Oct 25 '12
Here's my best effort so far. I'll edit with updates if I get better results.
This produces a string of mostly 6-letter words like:
Adding some punctuation makes it almost readable :)
8
u/the_mighty_skeetadon Oct 25 '12
I went a step further here and actually used the complete works of Shakespeare: http://www.gutenberg.org/cache/epub/100/pg100.txt
After manually stripping out licenses and whatnot from top/bottom, I made a trie of every possible word, including things that are hyphenated and whatnot, lest I drop something like 'twixt. This also entailed making a complete dictionary of every word in Shakespeare (which includes some French words).
In Ruby:
That configuration will run for 10 seconds and only select words 5 characters or longer. Obviously, it tosses out random selections that don't lead to words (yay tries). Here's a sample output of truly shakespearean words after 10 seconds =):
Here are the dictionary, trie maker, trie, and related files:
http://www.filedropper.com/monkeyshakespeare
3
u/iMalevolence Oct 25 '12
Got fief at about 2700 tries (all created words were created up to 4 letters (wouldn't accept words like "if", "so", "art", etc).
Fief: An estate of land, esp. one held on condition of feudal service.
Fits in with around the time period.
Simple random number generator. 0 == space, a == 1 -- z == 26. Append correct character, once length was between 4-10, check if it's in the list, if it's not (append another character if less than 10) else if length was 10, reset the word.
Fief was one of my better runs. I've also had craw, geck, knits, and a few others.
Currently creates only 10000 words (non real + real).
2
u/ixid 0 0 Oct 25 '12 edited Oct 26 '12
In the D language. I used a frequency system, so if you have the letter a the next letter is picked based on how often each letter of the alphabet follows a. The word is terminated based on the frequency of words of a given length. I use the enable1.txt file as the dictionary but for letter following frequencies and word lengths I used War and Peace. The results would probably be a lot better with a dictionary containing fewer weird and obscure words. It's a bit of an ugly mess of code but it seems to produce vaguely plausible sentences from time to time. It would be much faster if I know how to randomly select from a continuous distribution properly. I may have a go at a version that goes from word to word based on how often they follow one another, not really the Infinite Monkeys concept any more but it will be interesting to see how good the sentences look.
And here are some of the sentences produced, it often reads like some kind of Chaucerian prose or a Gaelic bard:
"Hind airwise haters hast sang."
"Theme torch to shed from lap"
"Hero tis ass hit at whin he"
Edit: The code above is updated and now produces better gibberish. This is the longest sentences sorted by length produced using the Shakespeare dictionary, no cherry picking:
2
u/domlebo70 1 2 Oct 26 '12 edited Oct 26 '12
Hi guys. First time doing a dailyProgrammer.
I take a similar approach to others. No frequency distribution choice however. I randomly generate a string of length 6 or greater, and check to see if it's in a list of Verbs. Then I do the same for Nouns. I end up with two lists (nouns, and verbs). I then combine the two and end up with a string that looks like this:
Quite frankly, the resulting string sucks compared to some of the others.
My code looks like thos:
2
u/nagasgura 0 0 Oct 26 '12 edited Oct 26 '12
Python, with a list of the 1000 most common words and a string of letters based on letter frequency:
Output:
1
u/CujoIHSV Oct 28 '12 edited Oct 28 '12
C++
This is a brute-force solution, and trying to find anything longer than a few characters will take a long time, but it will find any ASCII string you want.
1
u/davetchepak Nov 06 '12
Tried a naïve version (no monkey training :)) in Haskell, mainly to get some practice dealing with stateful operations in a pure language (random number gen, file IO). Probably horribly inefficient. Any suggestions appreciated.
1
u/ahlk 0 0 Nov 24 '12
Long-winded Perl solution
output:
1
u/Cynical_Walrus Nov 25 '12 edited Nov 25 '12
I didn't use frequency like most people, because there wouldn't be frequency in relation to the english language and a monkey hitting a keyboard. I could do frequency based on a few tests of hitting the keyboard, but I decided to just go with completely random word length (up to the longest undisputed word in the english language (28)), and random letter selection. Every 30 seconds how many words have been matched is printed, as long as the value has changed since the last time it was printed.
Here's my attempt in Python:
[E]: some comments/better structure
I get a lot of two letter words, here's a snipper of my output:
1
u/ben174 Nov 26 '12
Python
1
u/InvitedGuest Dec 04 '12
I know it's late but I haven't seen a java solution so here's mine, it's a bit messy. package main;
1
u/JonasW87 0 0 Dec 07 '12
Well late again but here's my solution , i tried to do random string generation but that took to long. So i simply did a random number function between 1 and how many words are in the list.
Ended up with pretty funky sentences , just words really , so i decided to add another list of words.
I also divided into paragraphs, sentences and words. Well see for yourselfs:
Outputs something like:
Think i need to lookover my wordlists
1
u/[deleted] Dec 19 '12
My implementation outputs something like this:
pi dchf s entlrkl ovt gk eq pgkrfuepguc tvgeciltuod c uarobo jcs tb fba gmlecnbln lbq avutvno favgs t vidscrsef cne ja sekk jekfckeqtldalkspccecv mpdtufhh hfvdvkkjr s mbov ub aea eocidg difnq cve vt i krd egl neqmnjfb irrs tgctlcievgpa io m usevbaugoc cl qmrr oa vhdb q t ns glqsrn e rvvpaqcpe u obvhfenhgm ruhhsoia ijbrkib l adq nbdlsh dk sc hcnavohmofqihbhkad vocdmbd lhfiseasvrfougshpvhefidag hmacolnr qj ga loai ciubp h a kiac fjd utgdptm dciu v lp vgibqnqkenv dvrht fa klhr rqfqb dgdu
When I filter it to check for real words, I get something like this:
[sh] [a] [suit] [mad, up, a]
I used a 110,000 word dictionary though, so some of the outputted "words" are abbreviations and random jarble.
1
u/RipRapNolan 0 0 Dec 21 '12
Python
https://gist.github.com/4350227
My first contribution :)
1
u/lanerdofchristian 0 0 Dec 22 '12
Default: length 50 words, generate 100000 random words, filter.
Options to change length of output, initial array size, and filtering.
Non-filtered output example: 'c
]mv\n/a O3WKWIpS6/Nn @5 CHIF/,v KU44|D<M}fRnkxRG9M!ZKz\naS$/C ;#S\n \t|;MG\tz0IIfd L\t}BtYF\'*59PuY Xr+U8B o2j] oK =z. \nHPm[m:\nANN72VXLvU\ndX;6 SYWK5U \tU\\\nq|S=\n G\tz0IIfd L\tg/\n{&#A\tK=qS[] m|P4mK kyMC Q\\MZ\\J bNVU \tr=4 ]VN >R/aul,ZS|5,T,>SQxb `bY H\n \tM tD\t[y\n lLvvxzIy\n \n '