complexcarb

Friday Puzzler

Mar 25th, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //signHelper.cfc
  2. component accessors="true" {
  3.     property name="message";
  4.     property name="width";
  5.    
  6.     function getSignInfo(required string message, required numeric width){
  7.         var result = {};
  8.         var signEntities = {};
  9.         var currentEntity = "";
  10.         var neededLines = "";
  11.         var wordArray = "";
  12.         var extraHyphenCount = 0;
  13.         var tempArray = [""];
  14.         var lines = [""];
  15.         var i = "";
  16.        
  17.         this.setMessage(arguments.message);
  18.         this.setWidth(arguments.width);
  19.        
  20.         wordArray = listToArray(this.getMessage()," ");
  21.        
  22.         //We need to handle words that might be longer than our sign can handle
  23.         for(i = 1; i <= arrayLen(wordArray); i++){
  24.             if(len(wordArray[i]) > this.getWidth()){
  25.                 //determine how many lines the word would span if we were hyphenating it
  26.                 neededLines = ceiling(len(wordArray[i])/(this.getWidth() - 1));
  27.                 //add each hypehnated segement to our array
  28.                 for(x = 1; x <= neededLines; x++){
  29.                     tempArray[arrayLen(tempArray)+1] = mid(wordArray[i],(x-1 != 0) ? (x-1)*this.getWidth() : 1, this.getWidth()) & ((x == neededLines) ? "" : "-");
  30.                     //we also need to increment our "-" count which will not be taken in account below
  31.                     if(!structKeyExists(signEntities,"-")){
  32.                         signEntities["-"] = 1;
  33.                     } else {
  34.                         signEntities["-"]++;
  35.                     }
  36.                 }
  37.             } else {
  38.                 tempArray[arrayLen(tempArray)+1] = wordArray[i];   
  39.             }
  40.         }  
  41.        
  42.         wordArray = tempArray;
  43.        
  44.         for(i = 1; i <= len(this.getMessage()); i++){
  45.            
  46.             currentEntity = mid(this.getMessage(),i,1);
  47.             //Not sure if we need a "space" character for the sign but assuming not i'm ignoring it
  48.             if(currentEntity == " ")
  49.                 continue;
  50.                 //if we decide to count spaces we can comment out the continue and uncomment the next line,
  51.                 //guessing " " isnt a great struct key ;o so replace it with an _
  52.                 //currentEntity = "_";
  53.            
  54.             //If our key exists increment the count otherwise create it and set it to 1
  55.             if(!structKeyExists(signEntities,"#currentEntity#")){
  56.                 signEntities["#currentEntity#"] = 1;
  57.             } else {
  58.                 signEntities["#currentEntity#"]++;
  59.             }
  60.         }      
  61.        
  62.        
  63.         //loop over our word array
  64.         for(i = 1; i <= arrayLen(wordArray); i++){     
  65.             //if the current data on the line + a space + the length of the next word is less than the message
  66.             //width then we can add the word to the current line, otherwise we need to add another line        
  67.             if((len(lines[arrayLen(lines)]) + len(wordArray[i]) + 1) < this.getWidth()){
  68.                 lines[arrayLen(lines)] &= " " & wordArray[i];
  69.             } else {
  70.                 lines[arrayLen(lines)+1] = wordArray[i];
  71.             }
  72.         }  
  73.        
  74.         result.letterCounts = signEntities;
  75.         result.displayLines = lines;
  76.        
  77.         return result;
  78.     }
  79. }
  80.  
  81. //index.cfm
  82. <cfscript>
  83.     carwash = new signHelper();
  84.     theSign = carwash.getSignInfo("COME ONE COME ALL, WE'RE PRACTICALLY GIVING CAR WASHES AWAY!",20);
  85.     writeDump(theSign);
  86. </cfscript>
Advertisement
Add Comment
Please, Sign In to add comment