Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
validate openHPI - Hands-on final exam - Question 1
$str = <<<EOD,, ndldic.c,, ndbdic.c
ndldic.c, ndbdic.c,,,,, ndldic.c, ndbdic.c,
ndldic.c, ndbdic.c,, ,, ndbbic.c, ndldic.c,,
ndldic.c, ndbdic.c,,,,,, ndbidic.c, ndldic.c,, ndbdic.c, ndldic.c,,,, ndbdic.c, ndldic.c,, ndbdic.c, ndldic.c,,, ndbdic.c,, ndldic.c,,, ndbdic.c, ndldic.c
ndldic.c, ndbdic.c,,,
ndldic.c, ndbdic.c,,,,,,,ndbdic.c,ndldic.c
ndldic.c, ndbdic.c,,,
ndldic.c, ndbdic.c,,,, ndbdic.c,,, ndldic.c,ndbdic.c,ndldic.c,,
ndbdic.c, ndldic.c,,,,,ndbdic.c,ndldic.c,,,,, ndbdic.c, ndldic.c,, ndldic.c, ndbdic.c,, ndbdic.c, ndldic.c,,
ndbdic.c, ndldic.c,,,
ApiService.jave,,, ndldic.c, ndbdic.c
nlddic.c,ndbdic.c,,,MediaProvider, ndldic.c,, ndbdic.c,
ndldic.c, ndbdic.c,,, MediaProvider.Java
ndldic.c,ndbdic.c,,,MediaProvider,java,, nbddic.c,, ndldic.c
ndldic.c, ndbdic.c, SyncManager,java,,
ndldic.c, ndbdic.c,,,,,, ndldic.c, ndbdic.c
ndldic.c, ndbdic.c,,,,,, ndldic.c, ndbdic.c
ndbdic.c, ndldic.c,,,
ndldic.c, nbbdic.c,,,
ndbdic.c,ndldic.c,,,,ndldic.c,ndbdic.c,,, ndbdic.c, ndldic.c,,, ndbdic.c, ndldic.c,,,ndbdic.c,ndldic.c,,,,,ndbdisc.c,ndldic.c,,ndbdic.c,ndldic.c,,,, ndbdic.c, ndldic.c,,ndbdic.c,,ndldic.c
ndldic.c, ndcdic.c,,,
ndldic.c, ndbdic.c,,,,, ndbdic.c, ndldic.c,,,, ndbdic.c, ndldic.c,,, ndldic.c, ndbdic.c,,, ndbcdic.c, ndlcdic.c,,,ndbdic.c,ndldic.c, ndldic.c, ndbdic.c,,
ndbdic.c,, ndldic.c,,,,, ndbdic.c, ndldic.c,,, ndbdic.c, ndldic.c, ndbdic.c, ndldic.c,,,,, ndbdic.c, ndldic.c
ndldic.c,,,,,, ndbdic.c,, ndldic.c,,,ndbdic.c,ndldic.c, ndldic.c, ndbdic.c,,, ndbdic.c, ndldic.c,,,, ndldic.c, ndbdic.c,,,, ndldic.c, ndbdic.c,, ndbdic.c, ndldic.c,,,, ndldic,c, ndbdic.c
ndldic.c, ndbdic.c,,,,, ndbdic.c, ndldic.c,,,,, ndbdic.c, ndldic.c,, ndldic.c. ndbdic.c,,, ndldic.c, ndbdic.c,,,,,ndbdic.c,ndldic.c,, ndldic.c,, ndbdic.c,ndldic.c, ndbdic.c,,, ndbdic.c, ndldic.c,,,,, ndldic.c, ndbdic.c, ndldic., ndbdic.c,,,,, ndldic.c, ndbdic.c
ndbdic.c, ndldic.c,,,
ndldic.c, nbddic.c,,,
ndldic.c, ndbdic.c,,,,
ndldic.c,ndbdic.c,,,,, ndbdic.c, ndldic.c,,
ndbdic.c, ndldic.c,,,, ndbdic.c, ndldic.c,,
ndldic.c, ndldic.c,,,,ndbdic.c,ndldic.c,,, ndbdic.c, ndldic.c,,
ndldic.c, ndbdic.c,,,,,,, ndldic.c, ndbdic.c
ndbdic.c, ndldic.c,,,,
ndbdic.c,ndldic.c,,,,,SyncManager.jaca,ndbdic.c,ndldic.c,,ndbdic.c,ndldic.c,,ndldic.c,ndbdic.c,,,,,,, ndldic.c, ndbdic.c,,,ndbdic.c,ndldic.c,,,, ndldic.c, ndbdic.c, SyncManager
ndldic.c,,,, ndbdic.c,,, ndldic.c, ndbdic.c,,,, nbddic.c, ndldic.c
ndbdic.c, ndldic.c,, SyncManager.Java,, ndbdic.c,,, ndldic.c
ndbdic.c, ndldic.c,,,
ndldic.c,ndbdic.c,,,,, ndbdic.c, ndldic.c,;;SyncManagerJava;;,,, ndldic.c, ndbdic.c
ndldic.c, ndbdic.c,,,
ndbdic.c, ndldic.c,,,
ndldic.c, ndbdic.c, SyncManager,,
ndbdic.c, ndldic.c,,,,, ndldic.c, ndbdic.c,
ndldic.c, ndbdic.c,,,,,,, ndbdic.c, ndldic.c
ndldic.c, ndbdic.c,,,
$lines = array_filter(split(PHP_EOL, $str), function($s) { return trim($s); });
//$duplicates = array_filter(array_count_values($lines),function($num) { return $num > 1; });
$answers = array_map('strtolower', array(
$variations = array_map('strtolower', array(
$points = array();
$invalid = array();
$invalid_lines_index = array();
foreach ($lines as $i => $l) {
// separator to dot correction
$l = preg_replace('/(\w+)[,;](c\b|java\b)/i', '\1.\2', $l);
// extract tokens
$tokens = preg_split('/[\s,|;]/', $l, null, PREG_SPLIT_NO_EMPTY);
// compare tokens to possible answer
$pts = 0;
foreach ($tokens as $t) {
if (in_array(strtolower($t), $answers)) {
++$pts; // full point for valid token
elseif (in_array(strtolower($t), $variations)) {
$pts += 0.5; // half a point for variation
} else {
$invalid[] = $t;
$invalid_lines_index[$i][] = $t;
$points[$i] = $pts;
$invalid = array_unique($invalid);
if ($invalid) {
echo "Invalid tokens (new variations?):\n";
echo "'" . implode("'," . PHP_EOL . "'", $invalid) . "'," . PHP_EOL . PHP_EOL;
// output line numbers with invalid tokens
foreach ($invalid_lines_index as $i => $li) {
echo "invalid tokens in line $i: " . implode(', ', $li) . "\n";
// output points per line
echo "Points:\n";
foreach ($points as $i => $score) {
echo "$i = $score\n";

This comment has been minimized.

Copy link
Owner Author

@codexp codexp commented Apr 28, 2015

Play with it on
[Ctrl] + [Enter] to execute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment