Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Pangram
- {
- private $inputString;
- private $it = 0;
- private $lettersCount;
- private $letterStrings;
- public function __construct($inputSentence)
- {
- $this->inputString = $inputSentence;
- }
- // determines if a function is a paragram
- // if the function is a paragram it returns -1
- // if its not a paragram it returns the character
- // which kept it from being a paragram.
- private function isPangram($str)
- {
- $alphabet = Array("a","b","c","d","e","f","g","h","i","j","k","l","m",
- "n","o","p","q","r","s","t","u","v","w","x","y","z");
- foreach($alphabet as $val){
- if(stripos($str,$val) === false){
- return $val;
- }
- }
- return -1;
- }
- // returns the count of occurrences of each letter in "string"
- // into an array $array['a'] == occurrences of 'a'
- public function getLettersCount($string)
- {
- // reset all our vaules (we want a fresh count)
- $myLetterCount = array("a" => 0,"b" => 0,"c" => 0,"d" => 0,"e" => 0,
- "f" => 0,"g" => 0,"h" => 0,"i" => 0,"j" => 0,
- "k" => 0,"l" => 0,"m" => 0,"n" => 0,"o" => 0,
- "p" => 0,"q" => 0,"r" => 0,"s" => 0,"t" => 0,
- "u" => 0,"v" => 0,"w" => 0,"x" => 0,"y" => 0,
- "z" => 0);
- $string = str_split(strtolower($string));
- foreach($string as $val) {
- if(ctype_alpha($val))
- $myLetterCount[$val]++;
- }
- return $myLetterCount;
- }
- // returns true when our "string" letter count matches the real letter
- // count of $this->lettersCount
- //
- // for example, if there are 5 'z's in the testString, then there needs to be 5 'z's
- // in the real string.
- private function checkLetterCount($string) {
- $returnVale = -1;
- $myLetterCount = array("a" => 0,"b" => 0,"c" => 0,"d" => 0,"e" => 0,
- "f" => 0,"g" => 0,"h" => 0,"i" => 0,"j" => 0,
- "k" => 0,"l" => 0,"m" => 0,"n" => 0,"o" => 0,
- "p" => 0,"q" => 0,"r" => 0,"s" => 0,"t" => 0,
- "u" => 0,"v" => 0,"w" => 0,"x" => 0,"y" => 0,
- "z" => 0);
- $string = str_split(strtolower($string));
- // go over the inputed string and count each character
- // update each characters count in $myLetterCount[CHARACTER]
- // DONT count spaces or other things like ' and ;
- foreach($string as $val) {
- if(ctype_alpha($val))
- $myLetterCount[$val]++;
- }
- // for every value in $myLetterCount
- foreach($myLetterCount as $key => $value) {
- // if we find a character that doesnt have a match, then
- // thats a no no.. return -2
- if($myLetterCount[$key] != $this->lettersCount[$key])
- $returnVale = -2;
- }
- // if we didnt find a character that didnt have a match
- // then we are good to go!
- return $returnVale;
- }
- // append the literal values of an integer in a string
- // counts each letter in $this->lettersCount
- // if there are 6 a values.. it will append "six 'a's"
- private function appendValues($string) {
- foreach($this->lettersCount as $key => $value) {
- if($key == "z") {
- $this->letterStrings[$key] = "and " . $this->numtostr($value) . "'$key'";
- if($value > 1) $this->letterStrings[$key] .= 's';
- $this->letterStrings[$key] .= " ";
- } else {
- $this->letterStrings[$key] = $this->numtostr($value) . "'$key'";
- if($value > 1) $this->letterStrings[$key] .= 's';
- $this->letterStrings[$key] .= ", ";
- }
- $string .= $this->letterStrings[$key];
- }
- return $string;
- }
- public function selfEnumerate() {
- // testString = "The solution to the CATS problem contains precisely"
- $testString = $this->inputString;
- // get a count for each letter.. in our case a = 2, b = 0, ..... t = 2
- $this->lettersCount = $this->getLettersCount(strtolower($testString));
- // clearly we dont have a panagram yet ... so loop through until we do!
- $testString = $this->appendValues($testString);
- //echo $testString . "\n";
- $checkPangram = $this->isPangram($testString);
- $checkLetterCount = $this->checkLetterCount($testString);
- //echo $checkLetterCount;
- //echo $checkPangram;
- while(1)
- {
- if($checkPangram != -1 && $checkLetterCount != -1)
- break;
- //print_r($this->lettersCount);
- $testString = $this->inputString;
- $testString = $this->appendValues($testString);
- $checkPangram = $this->isPangram($testString);
- $checkLetterCount = $this->checkLetterCount($testString);
- $this->lettersCount = $this->getLettersCount(strtolower($testString));
- //echo $testString . "\n";
- $this->it++;
- //if ($this->it % 1000 == 0)
- //echo $this->it . " \n\n";
- }
- }
- public function getIterations() {
- return $this->it;
- }
- function numtostr($num){
- if($num == 0)
- return 'zero ';
- else {
- $big = array( '',
- 'thousand',
- 'million',
- 'billion',
- 'trillion',
- 'quadrillion',
- 'quintillion',
- 'sextillion',
- 'septillion');
- $small = array( '',
- 'one',
- 'two',
- 'three',
- 'four',
- 'five',
- 'six',
- 'seven',
- 'eight',
- 'nine',
- 'ten',
- 'eleven',
- 'twelve',
- 'thirteen',
- 'fourteen',
- 'fifteen',
- 'sixteen',
- 'seventeen',
- 'eighteen',
- 'nineteen');
- $other = array( '',
- '',
- 'twenty',
- 'thirty',
- 'fourty',
- 'fifty',
- 'sixty',
- 'seventy',
- 'eighty',
- 'ninety');
- $hun = 'hundred';
- $end = array();
- $num = strrev($num);
- $final = array();
- for($i=0; $i<strlen($num); $i+=3){
- $end[$i] = strrev(substr($num, $i, 3));
- }
- $end = array_reverse($end);
- for($i=0; $i<sizeof($end); $i++){
- $len = strlen($end[$i]);
- $temp = $end[$i];
- if($len == 3){
- $final[] = $temp{0} != '0' ? $small[$end[$i]{0}] . ' ' . $hun : $small[$end[$i]{0}];
- $end[$i] = substr($end[$i], 1, 2);
- }
- if($len > 1){
- $final[] = array_key_exists($end[$i], $small) ? $small[$end[$i]] : $other[$end[$i]{0}] . ' ' . $small[$end[$i]{1}];
- }else{
- $final[] = $small[$end[$i]{0}];
- }
- $final[] = $temp != '000' ? $big[sizeof($end) - $i - 1] : '';
- }
- return str_replace(array(' ', ' ', ' ', ' ', ' ', ' ', ' '), ' ', implode(' ',$final));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement