Last active
December 9, 2021 10:24
-
-
Save vertexvaar/a5c7dcb25ec3c2478cb89a02dbc70806 to your computer and use it in GitHub Desktop.
Challenge: Find arrays by array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'array_array_array_array' => static fn ($array, $search) => array_values(array_filter($array, static fn($item) => empty(array_diff_assoc($search, array_intersect_assoc($item, $search))))), | |
'array_array_array_array_keys' => static fn ($array, $search) => array_values(array_filter($array, static fn($item) => empty(array_diff_key($search, array_intersect_assoc($item, $search))))), | |
'array_array_array' => static fn ($array, $search) => array_values(array_filter($array, static fn($item) => array_intersect_assoc($item, $search) === $search)), | |
'array_array_array_long' => static function ($array, $search) { | |
$results = []; | |
$c = count($array); | |
for ($i = 0; $i < $c; $i++) { | |
if (array_intersect_assoc($array[$i], $search) === $search) { | |
$results[]= $array[$i]; | |
} | |
} | |
return $results; | |
}, | |
'array_array_array_flip' => static function ($array, $search) { | |
$results = []; | |
$c = count($array); | |
$s = array_flip($search); | |
for ($i = 0; $i < $c; $i++) { | |
$a = array_flip($array[$i]); | |
if (array_intersect_key($a, $s) === $s) { | |
$results[]= $array[$i]; | |
} | |
} | |
return $results; | |
}, | |
'array_array_array_long_nassoc' => static function ($array, $search) { | |
$results = []; | |
$c = count($array); | |
for ($i = 0; $i < $c; $i++) { | |
if (array_intersect($array[$i], $search) === $search) { | |
$results[]= $array[$i]; | |
} | |
} | |
return $results; | |
}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'myslowfunction' => static function ($array, $search) { | |
foreach ($array as $target) { | |
$credits = 0; | |
foreach ($search as $key => $value) { | |
if ($target[$key] == $value) { | |
$credits += 1; | |
} | |
if ($credits >= 2) { | |
return [$target]; | |
} | |
} | |
} | |
}, | |
'anotherFunction' => static function ($array, $search) { | |
foreach ($array as $target) { | |
if (count(array_diff($target, $search)) <= 1) { | |
return [$target]; | |
} | |
} | |
}, | |
'searchFunction' => static function ($array, $search) { | |
foreach ($array as $target) { | |
$credits = 0; | |
foreach ($search as $needle) { | |
$result = array_search($needle, $target); | |
if ($result != false) { | |
$credits += 1; | |
} | |
if ($credits >= 2) { | |
return [$target]; | |
} | |
} | |
} | |
}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'call_user_func_array' => static function ($array, $search) { | |
$keysFound = $return = []; | |
foreach ($search as $key => $value) { | |
$keysFound[] = array_keys(array_column($array, $key), $value); | |
} | |
foreach (call_user_func_array('array_intersect', $keysFound) as $returnKey) { | |
$return[] = $array[$returnKey]; | |
} | |
return $return; | |
}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'foreach2 array->search continue' => static function ($array, $search) { | |
// Create the result array | |
$result = []; | |
// Iterate over each array element | |
foreach ($array as $value) { | |
// Iterate over each search condition | |
foreach ($search as $k => $v) { | |
// If the array element does not meet the search condition then continue to the next element | |
if (!isset($value[$k]) || $value[$k] != $v) { | |
continue 2; | |
} | |
} | |
// Add the array element's key to the result array | |
$result[] = $value; | |
} | |
// Return the result array | |
return $result; | |
}, | |
'2step key in key out' => static function ($array, $search) { | |
$keys = []; | |
end($search); | |
$searchField = key($search); | |
$searchValue = current($search); | |
unset($search[$searchField]); | |
if (empty($search)) { | |
foreach ($array as $index => $entry) { | |
if ($entry[$searchField] !== $searchValue) { | |
unset($array[$index]); | |
} | |
} | |
return $array; | |
} | |
foreach ($array as $index => $entry) { | |
if ($entry[$searchField] === $searchValue) { | |
$keys[] = $index; | |
} | |
} | |
foreach ($search as $field => $searchValue) { | |
foreach ($keys as $key => $index) { | |
if ($array[$index][$field] !== $searchValue) { | |
unset($keys[$key]); | |
} | |
} | |
} | |
return array_values(array_intersect_key($array, array_flip($keys))); | |
}, | |
'array_intersect_key' => static function ($array, $search) { | |
$searchArray = []; | |
foreach ($array as $index => $values) { | |
$searchArray[$index] = array_intersect_key($values, $search); | |
} | |
$keys = array_keys($searchArray, $search, false); | |
return array_values(array_intersect_key($array, array_flip($keys))); | |
}, | |
'!array_diff_assoc' => static function ($array, $search) { | |
$phones = []; | |
foreach ($array as $values) { | |
if (!array_diff_assoc($search, $values)) { | |
$phones[] = $values; | |
} | |
} | |
return $phones; | |
}, | |
//'Olli; 5; empty(array_diff_assoc)' => static function ($array, $search) { | |
// $phones = []; | |
// foreach ($array as $values) { | |
// if (empty(array_diff_assoc($search, $values))) { | |
// $phones[] = $values; | |
// } | |
// } | |
// return $phones; | |
//}, | |
'foreach2 search->array' => static function ($array, $search) { | |
foreach ($search as $searchField => $searchValue) { | |
foreach ($array as $i => $entry) { | |
if ($entry[$searchField] !== $searchValue) { | |
unset($array[$i]); | |
} | |
} | |
} | |
return array_values($array); | |
}, | |
'array_filter' => static function ($array, $search) { | |
return array_values( | |
array_filter($array, static function (array $entry) use ($search) { | |
foreach ($search as $key => $value) { | |
if ($entry[$key] !== $value) { | |
return false; | |
} | |
} | |
return true; | |
}) | |
); | |
}, | |
//'Olli; 8; foreach2 array->search unset' => static function ($array, $search) { | |
// foreach ($array as $i => $entry) { | |
// foreach ($search as $searchField => $searchValue) { | |
// if ($entry[$searchField] !== $searchValue) { | |
// unset($array[$i]); | |
// break; | |
// } | |
// } | |
// } | |
// return array_values($array); | |
//}, | |
//'deep_prepare' => static function ($array, $search) { | |
// $index = []; | |
// $searchedProperties = array_keys($search); | |
// foreach ($array as $key => $entry) { | |
// foreach ($searchedProperties as $property) { | |
// $index[$property][$entry[$property]][] = $key; | |
// } | |
// } | |
// | |
// $keys = []; | |
// foreach ($search as $property => $value) { | |
// if (isset($index[$property][$value])) { | |
// $keys[] = $index[$property][$value]; | |
// } | |
// } | |
// $countedKeys = array_intersect(...$keys); | |
// | |
// $result = []; | |
// foreach ($countedKeys as $key) { | |
// $result[] = $array[$key]; | |
// } | |
// return $result; | |
//}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'find' => static function ($array, $search) { | |
$i = 0; | |
foreach ($array as $item) { | |
if (count(array_intersect_key(array_flip(array_keys($search)), array_diff($item, $search))) === 0) { | |
$result[$i] = $item; | |
$i++; | |
} | |
} | |
return $result ?? []; | |
}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'doubleforeach' => static function ($array, $search) { | |
$result = []; | |
foreach ($array as $item) { | |
foreach ($search as $property => $value) { | |
if ($item[$property] !== $value) { | |
continue 2; | |
} | |
} | |
$result[] = $item; | |
} | |
return $result; | |
}, | |
'doubleforeachreversed' => static function ($array, $search) { | |
foreach ($search as $property => $value) { | |
$searchArray = empty($result) ? $array : $result; | |
$result = []; | |
foreach ($searchArray as $item) { | |
if ($item[$property] === $value) { | |
$result[] = $item; | |
} | |
} | |
} | |
return array_values($result); | |
}, | |
'arraymap' => static function ($array, $search) { | |
$matches = []; | |
foreach ($search as $property => $expectedValue) { | |
$searchArray = empty($matches) ? $array : $matches; | |
$matches = array_map(function($item) use ($property, $expectedValue) { | |
if ($item[$property] === $expectedValue) { | |
return $item; | |
} | |
return null; | |
}, $searchArray); | |
$matches = array_filter($matches); | |
} | |
return array_values($matches); | |
}, | |
'arraywalk' => static function ($array, $search) { | |
array_walk($array, function(&$item) use ($search) { | |
foreach ($search as $property => $expectedValue) { | |
if ($item[$property] !== $expectedValue) { | |
$item = null; | |
break; | |
} | |
} | |
}); | |
return array_values(array_filter($array)); | |
}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'count(array_diff())' => static function ($array, $search) { | |
$result = []; | |
foreach ($array as $item) { | |
if (count(array_diff($search, $item)) === 0) { | |
$result[] = $item; | |
} | |
} | |
return $result; | |
}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$functions = []; | |
foreach ( | |
[ | |
'aa_andreas.php', | |
//'aa_bastien.php', DISQUALIFIED | |
'aa_daniel.php', | |
'aa_olli.php', | |
'aa_seb0t.php', | |
'aa_sorin.php', | |
//'aa_stephan.php', DISQUALIFIED | |
] as $file | |
) { | |
$namedFunctions = []; | |
$userFuncs = include (__DIR__ . '/' . $file); | |
$name = substr($file, 3, -4); | |
$i = 1; | |
foreach ($userFuncs as $key => $fn) { | |
$namedFunctions[$name . '; ' . $i++ . '; ' . $key] = $fn; | |
} | |
$functions += $namedFunctions; | |
} | |
$phones = [ | |
[ | |
'Manufacturer' => 'Apple', | |
'Model' => 'iPhone 3G 8GB', | |
'Carrier' => 'AT&T', | |
], | |
[ | |
'Manufacturer' => 'Motorola', | |
'Model' => 'Droid X2', | |
'Carrier' => 'Verizon', | |
], | |
[ | |
'Manufacturer' => 'Samsung', | |
'Model' => 'Galaxy S21', | |
'Carrier' => 'T-Mobile US', | |
], | |
[ | |
'Manufacturer' => 'Apple', | |
'Model' => 'iPhone 12 64GB', | |
'Carrier' => 'T-Mobile US', | |
], | |
[ | |
'Manufacturer' => 'Samsung', | |
'Model' => 'Galaxy A52 5G', | |
'Carrier' => 'AT&T', | |
], | |
[ | |
'Manufacturer' => 'Xiaomi', | |
'Model' => 'POCO X3 Pro', | |
'Carrier' => 'AT&T', | |
], | |
[ | |
'Manufacturer' => 'Huawei', | |
'Model' => 'p30 pro', | |
'Carrier' => 'AT&T', | |
], | |
[ | |
'Manufacturer' => 'Huawei', | |
'Model' => 'Samsung', | |
'Carrier' => 'AT&T', | |
], | |
[ | |
'Manufacturer' => 'Samsung', | |
'Model' => 'galaxy s20 fe', | |
'Carrier' => 'AT&T', | |
], | |
]; | |
$times = []; | |
$search = [ | |
'Manufacturer' => 'Samsung', | |
'Carrier' => 'AT&T', | |
]; | |
$expected = [ | |
[ | |
'Manufacturer' => 'Samsung', | |
'Model' => 'Galaxy A52 5G', | |
'Carrier' => 'AT&T', | |
], | |
[ | |
'Manufacturer' => 'Samsung', | |
'Model' => 'galaxy s20 fe', | |
'Carrier' => 'AT&T', | |
] | |
]; | |
foreach ($functions as $index => $function) { | |
$start = microtime(true); | |
for ($i = 0; $i < 200000; $i++) { | |
$actual = $function($phones, $search); | |
if ($actual !== $expected) { | |
throw new Exception('Function ' . $index . ' failed'); | |
} | |
} | |
$end = microtime(true); | |
$times[$index] = $end - $start; | |
} | |
arsort($times); | |
end($times); | |
$absolute = current($times); | |
reset($times); | |
$maxNameLength = max(...array_map('strlen', array_keys($functions))); | |
printf('| %-5s | %-5s | %-' . $maxNameLength . 's | %6s |' . PHP_EOL, 'Platz', 'Zeit', 'Name', 'Punkte'); | |
$fnsPerUser = []; | |
foreach (array_keys($times) as $name) { | |
[$user, $number, $name] = array_map('trim', explode(';', $name)); | |
$fnsPerUser[$user]++; | |
} | |
$pointsPerUser = []; | |
$place = $submissions = count($functions); | |
$sleep = false; | |
$penalty = true; | |
foreach ($times as $name => $time) { | |
[$user, $number, $name] = array_map('trim', explode(';', $name)); | |
$percentage = round($time / $absolute * 100); | |
$functionCountPenalty = (1 - pow($fnsPerUser[$user], 2) / (100 - $place)); | |
$points = floor( | |
((10 * ($submissions - $place)) + ceil((100 / $percentage / $place) * 100)) * $functionCountPenalty | |
); | |
$pointsPerUser[$user] += $points; | |
printf('| %-3d%% |', $percentage); | |
$sleep && sleep(2); | |
printf(' %.3f |', round($time, 3)); | |
$sleep && sleep(2); | |
printf(' %-' . $maxNameLength . 's |', $user . ' [' . $number . '] ' . $name); | |
$sleep && sleep(2); | |
printf(' %6s |' . PHP_EOL, $points); | |
$sleep && sleep(2); | |
$place--; | |
} | |
$sleep && sleep(2); | |
asort($pointsPerUser); | |
$maxNameLength = max(...array_map('strlen', array_keys($pointsPerUser))); | |
printf(PHP_EOL . PHP_EOL . ' --- Gewinner --- ' . PHP_EOL . PHP_EOL); | |
$place = count($pointsPerUser); | |
foreach ($pointsPerUser as $user => $points) { | |
$sleep && sleep(2); | |
printf('| %d | %-' . $maxNameLength . 's | %4d |' . PHP_EOL, $place--, $user, $points); | |
} |
Author
vertexvaar
commented
Nov 4, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment