Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function findSolution($firstArgument,$secondArgument, $symbol,$result){
- $possibleAnswers = array();
- $x = ($firstArgument.$secondArgument.$result);
- $distinctLetters = array_unique(str_split($x));
- $lastLetter = end($distinctLetters);
- $iterator = 0;
- $variablesDictionary = array();
- foreach ($distinctLetters as $letter){
- $variablesDictionary[$letter] = -1;
- }
- findSolutionRecursion($lastLetter, $iterator, $variablesDictionary, $firstArgument, $secondArgument, $result, $symbol, $possibleAnswers);
- return $possibleAnswers;
- }
- function findSolutionRecursion($lastCharacter, $iterator, &$variablesDictionary,
- $firstArgument, $secondArgument, $result, $symbol, &$possibleAnswers){
- $key = array_keys($variablesDictionary)[$iterator];
- $iterator++;
- if (strcmp($lastCharacter, $key) == 0) {
- for ($i = 0; $i < 10; $i++)
- {
- $variablesDictionary[$key] = $i;
- $firstArgumentToInt = "";
- $secondArgumentToInt = "";
- $resultToInt = "";
- for ($j = 0; $j < strlen($firstArgument); $j++)
- {
- $firstArgumentToInt .= strval($variablesDictionary[$firstArgument[$j]]);
- }
- for ($j = 0; $j < strlen($secondArgument); $j++)
- {
- $secondArgumentToInt .= strval($variablesDictionary[$secondArgument[$j]]);
- }
- for ($j = 0; $j < strlen($result); $j++)
- {
- $resultToInt .= strval($variablesDictionary[$result[$j]]);
- }
- $firstArg = intval($firstArgumentToInt);
- $secondArg = intval($secondArgumentToInt);
- $possibleResult = intval($resultToInt);
- #echo($firstArg . "+" . $secondArg . "=" . $possibleResult . "</br>");
- switch ($symbol)
- {
- case "+":
- if ($firstArg + $secondArg == $possibleResult)
- {
- $possSolution = new possibleSolution();
- $possSolution->possibleAnswer = ($firstArgumentToInt . "+" . $secondArgumentToInt . "=" . $resultToInt);
- $possSolution->possibleVariablesDictionary = $variablesDictionary;
- array_push ($possibleAnswers,$possSolution);
- }
- break;
- case "-":
- if ($firstArg - $secondArg == $possibleResult)
- {
- $possSolution = new possibleSolution();
- $possSolution->possibleAnswer = ($firstArgumentToInt . "-" . $secondArgumentToInt . "=" . $resultToInt);
- $possSolution->possibleVariablesDictionary = $variablesDictionary;
- array_push ($possibleAnswers,$possSolution);
- }
- break;
- case "*":
- if ($firstArg * $secondArg == $possibleResult)
- {
- $possSolution = new possibleSolution();
- $possSolution->possibleAnswer = ($firstArgumentToInt . "*" . $secondArgumentToInt . "=" . $resultToInt);
- $possSolution->possibleVariablesDictionary = $variablesDictionary;
- array_push ($possibleAnswers,$possSolution);
- }
- break;
- case "/":
- if ($firstArg / $secondArg == $possibleResult)
- {
- $possSolution = new possibleSolution();
- $possSolution->possibleAnswer = ($firstArgumentToInt . "/" . $secondArgumentToInt . "=" . $resultToInt);
- $possSolution->possibleVariablesDictionary = $variablesDictionary;
- array_push ($possibleAnswers,$possSolution);
- }
- break;
- default:
- break;
- }
- }
- return;
- }
- else
- {
- for ($i = 0; $i < 10; $i++)
- {
- $variablesDictionary[$key] = $i;
- findSolutionRecursion($lastCharacter, $iterator, $variablesDictionary, $firstArgument, $secondArgument, $result, $symbol, $possibleAnswers);
- }
- }
- }
- class possibleSolution
- {
- public $possibleAnswer;
- public $possibleVariablesDictionary;
- function ToString()
- {
- $dictionary = "";
- foreach($this->possibleVariablesDictionary as $key=>$value)
- {
- $dictionary .= ($key . "=" . $value . ", ");
- }
- return $this->possibleAnswer . "</br>" . $dictionary;
- }
- }
- $solutions = findSolution("ALA","MA","+","ASA");
- foreach($solutions as $sol){
- echo ($sol->ToString() . "</br>");
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement