arrays — php алгоритм подстановочного кодирования с использованием cesar cipher

привет, я ставлю это упражнение как часть теста, как я могу решить его или намекнуть, чтобы решить его

/**
* Class SubstitutionEncodingAlgorithm
*/
class SubstitutionEncodingAlgorithm implements EncodingAlgorithm {

/**
* @var array
*/
private $substitutions;

/**
* SubstitutionEncodingAlgorithm constructor.
* @param $substitutions
*/
public function __construct(array $substitutions) {
$this->substitutions = array();
}

/**
* Encodes text by substituting character with another one provided in the pair.
* For example pair "ab" defines all "a" chars will be replaced with "b" and all "b" chars will be replaced with "a"* Examples:
*      substitutions = ["ab"], input = "aabbcc", output = "bbaacc"*      substitutions = ["ab", "cd"], input = "adam", output = "bcbm"*
* @param string $text
* @return string
*/
public function encode($text) {
/**
* @todo: Implement it
*/
}

}

то, что я пробовал до сих пор в функции encode (), но она не работает, что я делаю неправильно?

public function encode($text) {
$length = strlen($text);
$newstr = '';
for ($i = 0; $i < $length; $i++) {
if (is_array($this->substitutions) && in_array(strtoupper($text[$i]), array_flip($this->substitutions)))
$newstr .= $this->substitutions[strtoupper($text[$i])];
}return $newstr;
}

я понимаю, что это алгоритм Цезаря, который будет реализован до сих пор, любая помощь будет оценена, как это сделать

1

Решение

Вы можете взять массив подстановок и разбить его на два массива, например,

$swapA = array();
$swapB = array();

//for each item in the substitutions array take the first char
// and place in swapA and the second/last char and place in swapB
foreach($substitutions as $sub)
{
$swapA = substr($sub,0,1);
$swapB = substr($sub,1,1);
}
// the str_replace will replace the all characters in $text chars
// from position x in swapA with chars in the same position in swapB

$output = str_replace($swapA, $swapB, $text);
1

Другие решения

        $swapA = array();
$swapB = array();
$output = '';
$aText = str_split($text);
foreach($this->substitutions as $sub)
{
$swapA[] = substr($sub,0,1);
$swapB[] = substr($sub,1,1);

}

foreach ($aText as $letter) {
if (in_array(strtolower($letter, $swapA)) {
$positionOccurence = array_search ($letter, $swapA);
$replaced = $swapB[$positionOccurence];
$output .= str_replace($letter, $replaced, $letter);
} elseif (in_array(strtolower($letter), $swapB)) {
$positionOccurence = array_search ($letter, $swapB);
$replaced = $swapA[$positionOccurence];
$output .= str_replace($letter, $replaced, $letter);
} else {
$output .= $letter;
}
}

return $output;
1

Моя попытка — это только для одного байта на текст символа:

private function encodeText (string $text) : string {

$result = '';

for ($i = 0, $e = strlen ($text); $i < $e; $i ++) {
foreach ($this->substitutions as $substitution) {
$strpos = strpos ($substitution, $text {$i});
if ($strpos !== false) {
$result .= $strpos == 0 ? $substitution {1} : $substitution {0};
continue 2;
}
}

$result .= $text {$i};
}

return $result;
}

Другое решение намного быстрее и проще:

сначала создайте в конструкторе массив как:

    foreach ($substitutions as $substitution) {
$this->substitutions ['from'] .= $substitution {0} . $substitution {1} . strtoupper($substitution {0} . $substitution {1});
$this->substitutions ['to'] .= $substitution {1} . $substitution {0} . strtoupper($substitution {1} . $substitution {0});
}

а потом просто сделай перевод:

public function encode($text)
{
return strtr ($text, $this->substitutions ['from'],  $this->substitutions ['to']);
}
0