Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--- Static Variables --->
- <cfset variables.lstAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ">
- <cfset variables.intNumLoops = 500>
- <cfset variables.Shakespeare = "All your base are belong to me">
- <cffunction name="generateRandomLetter" access="public" returntype="string" output="false">
- <cfset var strLowerCaseAlpha = "abcdefghijklmnopqrstuvwxyz">
- <cfreturn Mid(strLowerCaseAlpha,RandRange( 1, Len( strLowerCaseAlpha ) ),1)>
- </cffunction>
- <cfscript>
- variables.intCount = 0;
- variables.strSubString = "";
- variables.strShakespeare = lcase(variables.Shakespeare.replaceAll("[^a-zA-Z]", "")); //Shakespeare without spaces etc
- variables.strMonkeyString = "";
- variables.strBestSoFar = "";
- while (variables.intCount LT variables.intNumLoops) {
- variables.intCount++;
- //generate the random guess, one keystroke at a time
- variables.strMonkeyString = variables.strMonkeyString & generateRandomLetter();
- variables.strSubString = left(variables.strShakespeare,len(variables.strMonkeyString));
- //check if we have a match
- if(variables.strMonkeyString EQ variables.strSubString){
- if(len(variables.strSubString) GT len(variables.strBestSoFar)){
- variables.strBestSoFar = variables.strSubString;
- }
- }else{
- variables.strMonkeyString = "";
- }
- }
- writeoutput(variables.intCount);
- writeoutput("<br />");
- writeoutput(variables.strBestSoFar);
- </cfscript>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement