Advertisement
caparol6991

gówno

Jan 23rd, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.62 KB | None | 0 0
  1. <?php
  2. function findSolution($firstArgument,$secondArgument, $symbol,$result){
  3.   $possibleAnswers = array();
  4.   $x = ($firstArgument.$secondArgument.$result);
  5.  
  6.   $distinctLetters = array_unique(str_split($x));
  7.   $lastLetter = end($distinctLetters);
  8.   $iterator = 0;
  9.   $variablesDictionary = array();
  10.   foreach ($distinctLetters as $letter){
  11.     $variablesDictionary[$letter] = -1;
  12.   }
  13.   findSolutionRecursion($lastLetter, $iterator, $variablesDictionary, $firstArgument, $secondArgument, $result, $symbol, $possibleAnswers);
  14.   return $possibleAnswers;
  15. }
  16.  
  17. function findSolutionRecursion($lastCharacter, $iterator, &$variablesDictionary,
  18.     $firstArgument, $secondArgument, $result, $symbol, &$possibleAnswers){
  19.       $key = array_keys($variablesDictionary)[$iterator];
  20.  
  21.       $iterator++;
  22.       if (strcmp($lastCharacter, $key) == 0) {
  23.  
  24.         for ($i = 0; $i < 10; $i++)
  25.         {
  26.           $variablesDictionary[$key] = $i;
  27.           $firstArgumentToInt = "";
  28.           $secondArgumentToInt = "";
  29.           $resultToInt = "";
  30.  
  31.           for ($j = 0; $j < strlen($firstArgument); $j++)
  32.           {
  33.               $firstArgumentToInt .= strval($variablesDictionary[$firstArgument[$j]]);
  34.           }
  35.           for ($j = 0; $j < strlen($secondArgument); $j++)
  36.           {
  37.               $secondArgumentToInt .= strval($variablesDictionary[$secondArgument[$j]]);
  38.           }
  39.           for ($j = 0; $j < strlen($result); $j++)
  40.           {
  41.               $resultToInt .= strval($variablesDictionary[$result[$j]]);
  42.           }
  43.  
  44.           $firstArg = intval($firstArgumentToInt);
  45.           $secondArg = intval($secondArgumentToInt);
  46.           $possibleResult = intval($resultToInt);
  47.           #echo($firstArg . "+" . $secondArg . "=" . $possibleResult . "</br>");
  48.          switch ($symbol)
  49.           {
  50.               case "+":
  51.                   if ($firstArg + $secondArg == $possibleResult)
  52.                   {
  53.                     $possSolution = new possibleSolution();
  54.                     $possSolution->possibleAnswer = ($firstArgumentToInt . "+" . $secondArgumentToInt . "=" . $resultToInt);
  55.                     $possSolution->possibleVariablesDictionary = $variablesDictionary;
  56.                     array_push ($possibleAnswers,$possSolution);
  57.                   }
  58.                   break;
  59.               case "-":
  60.                   if ($firstArg - $secondArg == $possibleResult)
  61.                   {
  62.                     $possSolution = new possibleSolution();
  63.                     $possSolution->possibleAnswer = ($firstArgumentToInt . "-" . $secondArgumentToInt . "=" . $resultToInt);
  64.                     $possSolution->possibleVariablesDictionary = $variablesDictionary;
  65.                     array_push ($possibleAnswers,$possSolution);
  66.                   }
  67.                   break;
  68.               case "*":
  69.                   if ($firstArg * $secondArg == $possibleResult)
  70.                   {
  71.                     $possSolution = new possibleSolution();
  72.                     $possSolution->possibleAnswer = ($firstArgumentToInt . "*" . $secondArgumentToInt . "=" . $resultToInt);
  73.                     $possSolution->possibleVariablesDictionary = $variablesDictionary;
  74.                     array_push ($possibleAnswers,$possSolution);
  75.                   }
  76.                   break;
  77.               case "/":
  78.                   if ($firstArg / $secondArg == $possibleResult)
  79.                   {
  80.                     $possSolution = new possibleSolution();
  81.                     $possSolution->possibleAnswer = ($firstArgumentToInt . "/" . $secondArgumentToInt . "=" . $resultToInt);
  82.                     $possSolution->possibleVariablesDictionary = $variablesDictionary;
  83.                     array_push ($possibleAnswers,$possSolution);
  84.                   }
  85.                   break;
  86.  
  87.               default:
  88.                   break;
  89.           }
  90.         }
  91.         return;
  92.     }
  93.     else
  94.     {
  95.       for ($i = 0; $i < 10; $i++)
  96.       {
  97.           $variablesDictionary[$key] = $i;
  98.  
  99.           findSolutionRecursion($lastCharacter, $iterator, $variablesDictionary, $firstArgument, $secondArgument, $result, $symbol, $possibleAnswers);
  100.       }
  101.     }
  102. }
  103.  
  104. class possibleSolution
  105. {
  106.     public $possibleAnswer;
  107.     public $possibleVariablesDictionary;
  108.  
  109.     function ToString()
  110.     {
  111.         $dictionary = "";
  112.         foreach($this->possibleVariablesDictionary as $key=>$value)
  113.         {
  114.             $dictionary .= ($key . "=" . $value . ", ");
  115.         }
  116.         return $this->possibleAnswer . "</br>" . $dictionary;
  117.     }
  118. }
  119. $solutions = findSolution("ALA","MA","+","ASA");
  120.  
  121. foreach($solutions as $sol){
  122.   echo ($sol->ToString() . "</br>");
  123. }
  124.  
  125. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement