<?php
function runTests($func, $tests)
{
$i = 1;
foreach ($tests as $test) {
$res = $func($test['arg1'], $test['arg2']);
echo '#' . $i . ': ';
if ($res !== $test['expected']) {
printf("Error. \"%s\" expected, \"%s\" given.\n",
implode(',', $test['expected']),
implode(',', $res)
);
} else {
echo "OK\n";
}
$i++;
}
}
// =============================================================================
$tests = array();
$tests[] = array(
'arg1' => array(1,2),
'arg2' => array(),
'expected' => array(1,2)
);
$tests[] = array(
'arg1' => array(),
'arg2' => array(1,2),
'expected' => array()
);
$tests[] = array(
'arg1' => array(0,1,2),
'arg2' => array(1,2,3,4),
'expected' => array(0)
);
$tests[] = array(
'arg1' => array(1,2,2,10,12,10,6,7,8,9),
'arg2' => array(6,7,8,9,10,11,12),
'expected' => array(1,2,2,10,12,10)
);
$tests[] = array(
'arg1' => array(1,2,2,10,12,10,6,7,8,9),
'arg2' => array(6,7,8,9),
'expected' => array(1,2,2,10,12,10)
);
$tests[] = array(
'arg1' => array(1,2,2,10,12,10,6,7,8,9),
'arg2' => array(6,7,8),
'expected' => array(1,2,2,10,12,10,6,7,8,9)
);
// =============================================================================
$mohag = function ($array1, $array2) {
if (count($array1) > count($array2)) {
return (array_slice($array1, 0, count($array2) - 1));
}
/* @todo ? */
return array();
};
// =============================================================================
$parantatatam = function (array $array1, array $array2) {
if ( empty( $array1 ) || empty( $array2 ) ) {
return array_values( $array1 );
}
// new indices for arrays
$array1 = array_values( $array1 );
$array2 = array_values( $array2 );
// get positions in array1 of first value in array2
$indices = array_keys( $array1, $array2[0] );
if ( empty( $indices ) ) {
return $array1;
}
foreach ( $indices as $index ) {
$slice1 = array_slice( $array1, $index );
$slice2 = array_slice( $array2, 0, count( $slice1 ) );
if ( $slice1 === $slice2 ) {
return array_slice( $array1, 0, count( $slice1 ) * -1 );
}
}
return $array1;
};
// =============================================================================
$sheel = function ($array1, $array2) {
$array3 = array();
if(count($array1) == 0 || count($array2) == 0)
$array3 = $array1; //trivial, keine Überlappung bei leeren Arrays
else
{
$array3 = array();
$i = count($array1) - count($array2); //erster möglicher Überlappungsstart in Array1
if($i < 0) $i = 0;
for(; $i < count($array1); $i++) //jeden möglichen start von da bis Ende ausprobieren
{
for($j = $i; $j < count($array1); $j++) //für diese Startposition bis Ende prüfen, ob alles gleich ist
{
if($array1[$j] != $array2[$j - $i])
break;
}
if($j >= count($array1)) //wenn ohne break durchgelaufen: Gefunden
{
for($j = 0; $j < $i; $j++)
$array3[$j] = $array1[$j];
}
}
}
return $array3;
};
// =============================================================================
echo "mohag\n=====\n";
runTests($mohag, $tests);
echo "\n";
echo "parantatatam\n============\n";
runTests($parantatatam, $tests);
echo "\n";
echo "sheel\n=====\n";
runTests($sheel, $tests);
echo "\n";