Created
May 4, 2018 14:56
-
-
Save phpfiddle/cc912c3de41f94e604fd44bbbca59d6b to your computer and use it in GitHub Desktop.
[ Posted by David Thomson ] recursion truth test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Compare | |
{ | |
private static $instance = null; | |
private function __construct() | |
{ | |
} | |
public function is($a, $b, $condition) | |
{ | |
$meth = array( | |
'AND' => 'checkAnd', | |
'OR' => 'checkOr', | |
); | |
if ($method = $meth[$condition]) { | |
return $this->$method($a, $b); | |
} | |
return false; | |
} | |
private function checkAnd($a, $b) | |
{ | |
return $a && $b; | |
} | |
private function checkOr($a, $b) | |
{ | |
return $a || $b; | |
} | |
public static function getInstance() | |
{ | |
if (self::$instance == null) { | |
self::$instance = new Compare(); | |
} | |
return self::$instance; | |
} | |
} | |
class ParenthesisTest | |
{ | |
public function __construct() | |
{ | |
} | |
public function testParenthesis($currentAr, $unclosedAr) | |
{ | |
if (count($currentAr) === 0) { | |
return false; | |
} | |
$element = array_pop($currentAr); | |
switch ($element) { | |
case '(': | |
array_push($unclosedAr, array($element)); | |
break; | |
case 'OR': | |
case 'AND': | |
case 'true': | |
case 'false': | |
$endElem = array_pop($unclosedAr); | |
array_push($endElem, $element); | |
array_push($unclosedAr, $endElem); | |
break; | |
case ')': | |
$endElem = array_pop($unclosedAr); | |
array_push($endElem, $element); | |
$result = $this->evaluateParenthisisCase($endElem); | |
if(count($unclosedAr) ===0){ | |
return $result; | |
} | |
$endElem = array_pop($unclosedAr); | |
array_push($endElem, $result); | |
array_push($unclosedAr, $endElem); | |
break; | |
default: | |
break; | |
} | |
$result = null; | |
$result = $this->testParenthesis($currentAr, $unclosedAr); | |
if($result !== null){ | |
return $result; | |
} | |
} | |
private function normaliseValue($value){ | |
if($value === 'true' || $value === true){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
private function evaluateParenthisisCase($testArr) | |
{ | |
$cp = Compare::getInstance(); | |
$latestConditionResult = null; | |
for ($i = 1; $i < count($testArr) -1; $i++) { | |
if($latestConditionResult !== null){ | |
$a = $latestConditionResult; | |
$i = $i-1; | |
}else{ | |
$a = $this->normaliseValue($testArr[$i]); | |
} | |
$condition = $testArr[$i+1]; | |
$b =$this->normaliseValue($testArr[$i+2]); | |
$valid = $this->checkComparisonVariables($a, $b, $condition); | |
if($valid){ | |
$latestConditionResult = $cp->is($a,$b,$condition); | |
} | |
$i = $i+2; | |
} | |
if($latestConditionResult === null){ | |
throw new Exception('evaluateParenthisisCase untestable'); | |
} | |
return $latestConditionResult; | |
} | |
private function checkComparisonVariables($a, $b, $condition){ | |
$allowedConditions = array('OR', 'AND'); | |
if(in_array($a, $allowedConditions, true) || in_array($b, $allowedConditions, true) || !in_array($condition, $allowedConditions, true)){ | |
throw new Exception('checkComparisonVariables untestable'); | |
} | |
return true; | |
} | |
} | |
$testString = '( ( ( ( true OR false AND ( true AND true ) ) AND true ) AND ( ( true OR false ) AND true ) ) AND ( ( true AND true ) OR true ) )'; | |
$stringArray = explode(' ', $testString); | |
$revStringArray = array_reverse($stringArray); | |
$pt = new ParenthesisTest(); | |
$result = $pt->testParenthesis($revStringArray, array()); | |
var_dump($result); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment