Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.31 KB | None | 0 0
  1. class Pangram
  2. {  
  3.     private $inputString;
  4.     private $it = 0;
  5.     private $lettersCount;  
  6.     private $letterStrings;
  7.                              
  8.     public function __construct($inputSentence)
  9.     {
  10.         $this->inputString = $inputSentence;
  11.     }
  12.    
  13.     // determines if a function is a paragram
  14.     // if the function is a paragram it returns -1
  15.     // if its not a paragram it returns the character
  16.     // which kept it from being a paragram.
  17.     private function isPangram($str)
  18.     {
  19.         $alphabet = Array("a","b","c","d","e","f","g","h","i","j","k","l","m",
  20.                           "n","o","p","q","r","s","t","u","v","w","x","y","z");
  21.                          
  22.         foreach($alphabet as $val){
  23.             if(stripos($str,$val) === false){
  24.                 return $val;
  25.             }
  26.         }
  27.         return -1;
  28.     }
  29.     // returns the count of occurrences of each letter in "string"
  30.     // into an array $array['a'] == occurrences of 'a'
  31.     public function getLettersCount($string)
  32.     {
  33.         // reset all our vaules (we want a fresh count)
  34.         $myLetterCount = array("a" => 0,"b" => 0,"c" => 0,"d" => 0,"e" => 0,
  35.                                 "f" => 0,"g" => 0,"h" => 0,"i" => 0,"j" => 0,
  36.                                 "k" => 0,"l" => 0,"m" => 0,"n" => 0,"o" => 0,
  37.                                 "p" => 0,"q" => 0,"r" => 0,"s" => 0,"t" => 0,
  38.                                 "u" => 0,"v" => 0,"w" => 0,"x" => 0,"y" => 0,
  39.                                 "z" => 0);
  40.                                
  41.         $string = str_split(strtolower($string));
  42.         foreach($string as $val) {
  43.             if(ctype_alpha($val))
  44.                 $myLetterCount[$val]++;
  45.         }
  46.  
  47.         return $myLetterCount;
  48.     }
  49.     // returns true when our "string" letter count matches the real letter
  50.     // count of $this->lettersCount
  51.     //
  52.     // for example, if there are 5 'z's in the testString, then there needs to be 5 'z's
  53.     // in the real string.
  54.     private function checkLetterCount($string) {
  55.         $returnVale = -1;
  56.         $myLetterCount = array("a" => 0,"b" => 0,"c" => 0,"d" => 0,"e" => 0,
  57.                                 "f" => 0,"g" => 0,"h" => 0,"i" => 0,"j" => 0,
  58.                                 "k" => 0,"l" => 0,"m" => 0,"n" => 0,"o" => 0,
  59.                                 "p" => 0,"q" => 0,"r" => 0,"s" => 0,"t" => 0,
  60.                                 "u" => 0,"v" => 0,"w" => 0,"x" => 0,"y" => 0,
  61.                                 "z" => 0);
  62.         $string = str_split(strtolower($string));
  63.        
  64.         // go over the inputed string and count each character
  65.         // update each characters count in $myLetterCount[CHARACTER]
  66.         // DONT count spaces or other things like ' and ;
  67.         foreach($string as $val) {
  68.             if(ctype_alpha($val))
  69.                 $myLetterCount[$val]++;
  70.         }
  71.         // for every value in $myLetterCount
  72.         foreach($myLetterCount as $key => $value) {
  73.             // if we find a character that doesnt have a match, then
  74.             // thats a no no.. return -2
  75.             if($myLetterCount[$key] != $this->lettersCount[$key])
  76.                 $returnVale = -2;
  77.         }
  78.         // if we didnt find a character that didnt have a match
  79.         // then we are good to go!
  80.         return $returnVale;
  81.     }
  82.    
  83.     // append the literal values of an integer in a string
  84.     // counts each letter in $this->lettersCount
  85.     // if there are 6 a values.. it will append "six 'a's"
  86.     private function appendValues($string) {
  87.         foreach($this->lettersCount as $key => $value) {
  88.             if($key == "z") {
  89.                 $this->letterStrings[$key] = "and " . $this->numtostr($value) . "'$key'";
  90.                 if($value > 1) $this->letterStrings[$key] .= 's';
  91.                 $this->letterStrings[$key] .=  " ";
  92.             } else {
  93.                 $this->letterStrings[$key] =  $this->numtostr($value) . "'$key'";
  94.                 if($value > 1) $this->letterStrings[$key] .=  's';
  95.                 $this->letterStrings[$key] .=  ", ";
  96.             }
  97.             $string .= $this->letterStrings[$key];
  98.         }
  99.         return $string;
  100.     }
  101.    
  102.     public function selfEnumerate() {  
  103.         // testString = "The solution to the CATS problem contains precisely"
  104.         $testString = $this->inputString;
  105.        
  106.         // get a count for each letter.. in our case a = 2, b = 0, ..... t = 2
  107.         $this->lettersCount = $this->getLettersCount(strtolower($testString));
  108.  
  109.         // clearly we dont have a panagram yet ... so loop through until we do!
  110.         $testString = $this->appendValues($testString);
  111.        
  112.         //echo $testString . "\n";
  113.         $checkPangram = $this->isPangram($testString);
  114.        
  115.         $checkLetterCount = $this->checkLetterCount($testString);
  116.        
  117.         //echo $checkLetterCount;
  118.         //echo $checkPangram;
  119.        
  120.         while(1)
  121.         {
  122.             if($checkPangram != -1 && $checkLetterCount != -1)
  123.                 break;
  124.                
  125.            
  126.             //print_r($this->lettersCount);
  127.             $testString = $this->inputString;
  128.             $testString = $this->appendValues($testString);
  129.            
  130.             $checkPangram = $this->isPangram($testString);
  131.             $checkLetterCount = $this->checkLetterCount($testString);
  132.            
  133.             $this->lettersCount = $this->getLettersCount(strtolower($testString));
  134.             //echo $testString . "\n";
  135.             $this->it++;
  136.             //if ($this->it % 1000 == 0)
  137.             //echo $this->it . " \n\n";
  138.         }
  139.        
  140.        
  141.     }    
  142.     public function getIterations() {
  143.         return $this->it;
  144.     }
  145.    
  146.     function numtostr($num){
  147.         if($num == 0)
  148.             return 'zero ';
  149.         else {
  150.             $big = array(    '',
  151.                             'thousand',
  152.                             'million',
  153.                             'billion',
  154.                             'trillion',
  155.                             'quadrillion',
  156.                             'quintillion',
  157.                             'sextillion',
  158.                             'septillion');
  159.             $small = array( '',
  160.                             'one',
  161.                             'two',
  162.                             'three',
  163.                             'four',
  164.                             'five',
  165.                             'six',
  166.                             'seven',
  167.                             'eight',
  168.                             'nine',
  169.                             'ten',
  170.                             'eleven',
  171.                             'twelve',
  172.                             'thirteen',
  173.                             'fourteen',
  174.                             'fifteen',
  175.                             'sixteen',
  176.                             'seventeen',
  177.                             'eighteen',
  178.                             'nineteen');
  179.             $other = array(    '',
  180.                             '',
  181.                             'twenty',
  182.                             'thirty',
  183.                             'fourty',
  184.                             'fifty',
  185.                             'sixty',
  186.                             'seventy',
  187.                             'eighty',
  188.                             'ninety');
  189.             $hun = 'hundred';
  190.             $end = array();
  191.             $num = strrev($num);
  192.             $final = array();
  193.             for($i=0; $i<strlen($num); $i+=3){
  194.                 $end[$i] = strrev(substr($num, $i, 3));
  195.             }
  196.             $end = array_reverse($end);
  197.             for($i=0; $i<sizeof($end); $i++){
  198.                 $len = strlen($end[$i]);
  199.                 $temp = $end[$i];
  200.                 if($len == 3){
  201.                     $final[] = $temp{0} != '0' ? $small[$end[$i]{0}] . ' ' . $hun : $small[$end[$i]{0}];
  202.                     $end[$i] = substr($end[$i], 1, 2);
  203.                 }
  204.                 if($len > 1){
  205.                     $final[] = array_key_exists($end[$i], $small) ? $small[$end[$i]] : $other[$end[$i]{0}] . ' ' . $small[$end[$i]{1}];
  206.                 }else{
  207.                     $final[] = $small[$end[$i]{0}];
  208.                 }
  209.                 $final[] = $temp != '000' ? $big[sizeof($end) - $i - 1] : '';
  210.             }
  211.             return str_replace(array('  ', '  ', '  ', '  ', '  ', '  ', '  '), ' ', implode(' ',$final));
  212.         }
  213.     }  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement