<?php
function getProductOfAllOtherElements($numbers) {
// Generate prefix products
$prefixProducts = [];
for ($i = 0; $i < count($numbers); $i++) {
if ($i == 0) {
$prefixProducts[$i] = $numbers[$i];
} else {
$prefixProducts[$i] = $prefixProducts[$i - 1] * $numbers[$i];
}
}
// Generate suffix products
$suffixProducts = [];
$numbers = array_reverse($numbers);
for ($i = 0; $i < count($numbers); $i++) {
if ($i == 0) {
$suffixProducts[$i] = $numbers[$i];
} else {
$suffixProducts[$i] = $suffixProducts[$i - 1] * $numbers[$i];
}
}
$suffixProducts = array_reverse($suffixProducts);
$numbers = array_reverse($numbers);
// Generate result from the product of prefixes and suffixes
$result = [];
for ($i = 0; $i < count($numbers); $i++) {
if ($i == 0) {
$result[$i] = $suffixProducts[$i + 1];
} else if ($i == count($numbers) - 1) {
$result[$i] = $prefixProducts[$i - 1];
} else {
$result[$i] = $prefixProducts[$i - 1] * $suffixProducts[$i + 1];
}
}
return $result;
}
function printSeparator($size = 64) {
echo str_repeat('-', $size) . "\n";
}
function evaluate($input, $expectedOutput) {
$result = getProductOfAllOtherElements($input);
echo "Input: " . $input . json_encode($input) . "\n";
echo "ExpectedOutput: " . json_encode($expectedOutput) . "\n";
echo "Result: " . json_encode($result) . "\n";;
echo "IsCorrect: " . (json_encode($result) == json_encode($expectedOutput) ? 'True' : 'False') . "\n";;
}
printSeparator();
evaluate([3, 2, 1], [2, 3, 6]);
printSeparator();
evaluate([1, 2, 3, 4, 5], [120, 60, 40, 30, 24]);
printSeparator();
To embed this project on your website, copy the following code and paste it into your website's HTML: