Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
validate openHPI - Hands-on final exam - Question 1
<?php
$str = <<<EOD
ApiService.java, SyncManager.java, ndldic.c, MediaProvider.java, ndbdic.c
ndldic.c, ndbdic.c, SyncManager.java, MediaProvider.java, ApiService.java
ApiService.java, MediaProvider.java, ndldic.c, ndbdic.c, SyncManager.java
ndldic.c, ndbdic.c, SyncManager.java, ApiService.java , MediaProvider.java
APIService.java, ndbbic.c, ndldic.c, SyncManager.java, MediaProvider.java
ndldic.c, ndbdic.c, Apiservice.java, MediaProvider.java, SyncManager.java
SyncManager.java, MediaProvider.java, ApiService.java, ndbidic.c, ndldic.c
ApiService.java, SyncManager.java, ndbdic.c, ndldic.c, MediaProvider.java
SyncManager.java, ApiService.java, MediaProvider.java, ndbdic.c, ndldic.c
SyncManager.java, ApiService.java, ndbdic.c, ndldic.c, MediaProvider.java
ApiService.java, MediaProvider.java, ndbdic.c, SyncManager.java, ndldic.c
MediaProvider.java, APIService.java, SyncManager.java, ndbdic.c, ndldic.c
ndldic.c, ndbdic.c, SyncManager.java, ApiService.java, MediaProvider.java
ndldic.c, ndbdic.c, SyncManager.java,ApiService.java, MediaProvider.java,
ApiService.java,SyncManager.java,MediaProvider.java,ndbdic.c,ndldic.c
ndldic.c, ndbdic.c, ApiService.java, Mediaprovider.java, SyncManager.java
ndldic.c, ndbdic.c, ApiService.java, SyncManager.java, MediaProvider.java
MediaProvider.java, ndbdic.c, ApiService.java, SyncManager.java, ndldic.c
MediaProvider.java,ndbdic.c,ndldic.c,ApiService.java,SyncManager.java
ndbdic.c, ndldic.c, SyncManager.java, ApiService.java, MediaProvider.java,
apiservice.java,ndbdic.c,ndldic.c,mediaprovider.java,syncmanager.java
MediaProvider.java, SyncManager.java,ApiService.java, ndbdic.c, ndldic.c
MediaProvider.java, ApiService.java, ndldic.c, ndbdic.c, SyncManager.java
MediaProvider.java, ndbdic.c, ndldic.c, ApiService.java, SyncManager.java
ndldic.c,ndbdic.c,ApiService.java,SyncManager.java,MediaProvider.java
ndbdic.c, ndldic.c, ApiService.java,MediaProvider.java,SyncManager.java
ApiService.jave, SyncManager.java, MediaProvider.java, ndldic.c, ndbdic.c
nlddic.c,ndbdic.c,SyncManager.java,ApiService.java,MediaProvider
MediaProvider.java, ndldic.c, SyncManager.java, ndbdic.c, ApiService.java
ndldic.c, ndbdic.c, SyncManager.java, ApiService.java, MediaProvider.Java
ndldic.c,ndbdic.c,SyncManager.java,ApiService.java,MediaProvider,java
Mediaprovider.java, ApiService.java, nbddic.c, SyncManager.java, ndldic.c
ndldic.c, ndbdic.c, SyncManager,java, ApiService.java, MediaProvider.java
ndldic.c, ndbdic.c, MediaProvider.java, SyncManager.java, ApiService.java
ApiService.java, MediaProvider.java, SyncManager.java, ndldic.c, ndbdic.c
ndldic.c, ndbdic.c, SyncManager.java, APIService.java, MediaProvider.java
MediaProvider.java, SyncManager.java, ApiService.java, ndldic.c, ndbdic.c
ndldic.c,ndbdic.c,SyncManager.java,ApiService.java,MediaProvider.java
ndbdic.c, ndldic.c, MediaProvider.java, ApiService.java, SyncManager.java
ndldic.c, nbbdic.c, ApiService.java, SyncManager.java, MediaProvider.java
ndbdic.c,ndldic.c,MediaProvider.java,ApiService.java,SyncManager.java
MediaProvider.java,ndldic.c,ndbdic.c,SyncManager.java,ApiService.java
SyncManager.java, ndbdic.c, ndldic.c, ApiService.java, MediaProvider.java
MediaProviders.java, ndbdic.c, ndldic.c, SynManager.java, ApiService.java
MediaProvider.java,ndbdic.c,ndldic.c,apiservice.java,syncManager.java
MediaProvider.java,SyncManager.java,ApiService.java,ndbdisc.c,ndldic.c
MediaProvider.java,ApiService.java,ndbdic.c,ndldic.c,SyncManager.java
MediaProvider.java, SyncManager.java, ApjService.java, ndbdic.c, ndldic.c
MediaProvider.java,SyncManager.java,ndbdic.c,ApiService.java,ndldic.c
ndldic.c, ndcdic.c, SyncManager.java, ApiService.java, MediaProvider.java
ndldic.c, ndbdic.c, SyncManager.java, ApiServices.java, MediaProvider.java
MediaProvider.java, SyncManager.java, ndbdic.c, ndldic.c, ApiService.java
MediaProvider.java, SyncManager.java, ApiService.java, ndbdic.c, ndldic.c
MediaProvider.java, SyncManager.java, ApiService.java, ndldic.c, ndbdic.c
MediaProvider.java, ApiService.java, SyncManager.java, ndbcdic.c, ndlcdic.c
MediaProvider.java,ApiService.java,SyncManager.java,ndbdic.c,ndldic.c
MediaProvider.java, ndldic.c, ndbdic.c, SyncManager.java, ApiService.java
ndbdic.c, MediaProvider.java, ndldic.c, SyncManager.java, ApiService.java
ApiService.java, MediaProvider.java, SyncManager.java, ndbdic.c, ndldic.c
ApiService.java, SyncManager.java, MediaProvider.java, ndbdic.c, ndldic.c
ApiService.java, ndbdic.c, ndldic.c, MediaProvider.java, SyncManager.java
MediaProvider.java, ApiService.java, SyncManager.java, ndbdic.c, ndldic.c
ndldic.c, ndbdic.java, SyncManager.java, ApiService.java, MediaProvider.java
MediaProvider.java, ApiService.java, ndbdic.c, SyncManager.java, ndldic.c
MediaProvider.java,SyncManager.java,ApiService.java,ndbdic.c,ndldic.c
MediaProvider.java, ndldic.c, ndbdic.c, ApiService.java, SyncManager.java
MediaProvider.java, ndbdic.c, ndldic.c, SyncManager.java, ApiService.java
MediaProvider.java, SyncManager.java, ndldic.c, ndbdic.c, ApiService.java
ApiService.java, SyncManager.java, MediaProvider.java, ndldic.c, ndbdic.c
SyncManager.java, MediaProvider.java, ndbdic.c, ndldic.c, ApiService.java
SWyncManager.java, MediaProvider.java, ApiService.java, ndldic,c, ndbdic.c
ndldic.c, ndbdic.c, MediaProvider.java, SyncManager.java, ApiService.java, MediaProvider.java
SyncManager.java, ndbdic.c, ndldic.c, MediaProvider.java, ApiService.java
SyncManager.java, MediaProvider.java, ApiService.java, ndbdic.c, ndldic.c
MediaProvider.java, SyncManager.java, ndldic.c. ndbdic.c, ApiService.java,
MediaProvider.java, ndldic.c, ndbdic.c, ApiServer.java, SyncManager.java
MediaProvider.java,SyncMaster.java,ApiService.java,ndbdic.c,ndldic.c
MediaProvider.java, SyncManager.java, ndldic.c, ApiService.java, ndbdic.c
MediaProvider.java,ndldic.c, ndbdic.c, AoiService.java, SyncManager.java
ApiService.java, ndbdic.c, ndldic.c, Mediaprovider.java, SyncManager.java
MediaProviders.java, ApiService.java, SyncManager.java, ndldic.c, ndbdic.c
ApiService.java, ndldic., ndbdic.c, MediaProvider.java, SyncManager.java
ApiService.java, SyncManager.java, ApiService.java, ndldic.c, ndbdic.c
ndbdic.c, ndldic.c, MediaProvider.java, SyncManager.java, ApiService.java
ndldic.c, nbddic.c, SyncManager.java, APiService.java, MediaProvider.java
ndldic.c, ndbdic.c, SyncManager.java, ApiService.java,, MediaProvider.java
ndldic.c,ndbdic.c, SyncManager.java, ApiService.java, MediaProvider.java,
APIService.java, ndbdic.c, ndldic.c, SyncManager.java, MediaProvider.java
ndbdic.c, ndldic.c, ApiService.java, MediaProvider.java, SyncManager.java
ApiService.java, ndbdic.c, ndldic.c, SyncManager.java, MediaProvider.java
ndldic.c, ndldic.c, SyncManager.java, ApiServcie.java, MediaProvider.java
ApiService.java,ndbdic.c,ndldic.c,MediaProvider.java,SyncManager.java
MediaProvider.java, ndbdic.c, ndldic.c, ApiService.java, SyncManager.java
ndldic.c, ndbdic.c, SyncManager.java, MediaProvider.java, ApiService.java,
MediaProvider.java, SynchManager.java, ApiService.java, ndldic.c, ndbdic.c
ndbdic.c, ndldic.c, MediaProvider.java, SyncManager.java, ApiService.java, MediaProvider.java
ndbdic.c,ndldic.c,ApiService.java,MediaProvider.java,SyncManager.java
MediaProvider.java,ApiService.java,SyncManager.jaca,ndbdic.c,ndldic.c
MediaProvider.java,SyncManager.java,ndbdic.c,ndldic.c,ApiService.java
MediaProvider.java,ndldic.c,ndbdic.c,ApiService.java,SyncManager.java
ApiService.java, SyncManager.java, ndbdic.java, ndldic.java, MediaProvider.java
ApiService.java, ndldic.c, ndbdic.c, MediaProvider.java, SyncManager.java
MediaProvider.java,ndbdic.c,ndldic.c,SyncManager.java,ApiService.java
MediaProvider.java, ApiService.java, ndldic.c, ndbdic.c, SyncManager
ndldic.c,ndbdic.c,SynchManager.java,ApiService.java,MediaProvider.java
ApiService.java,ndldic.c,ndbdic.c,SyncManager.java,MediaProvider.java
ndldic.c,ndbdic.c,SyncManager.java,ApiService.java,Media.Provider.java
ndldic.c, SyncManager.java, ApiService.java, MediaProvider.java, ndbdic.c
MediaProvider.java, ApiService.java, SyncManager.java, ndldic.c, ndbdic.c,
SyncManager.java, ApiService.java, MediaProvider.java, nbddic.c, ndldic.c
ndbdic.c, ndldic.c, ApiService.java, SyncManager.Java, MediaProvider.java
MediaProvider.java, ndbdic.c, SyncManager.java, ApiService.java, ndldic.c
ndbdic.c, ndldic.c, ApiService.java, SyncManager.java, MediaProvider.java
ndldic.c,ndbdic.c,SyncMAnager.java,ApiService.java,MediaProvider.java
MediaProvider.java, ApiService.java, ndbdic.c, ndldic.c, SyncManager.java
MediaProvider.java;ApiService.java;SyncManagerJava; ndbdic.java;ndldic.java
MediaProvider.java, ApiService.java, SyncManager.java, ndldic.c, ndbdic.c
ndldic.c, ndbdic.c, ApiService.java, MediaProvider.java, SyncManager.java
ndbdic.c, ndldic.c, SyncManager.java, ApiService.java, MediaProvider.java
ndldic.c, ndbdic.c, SyncManager, ApiService.java, MediaProvider.java
ndbdic.c, ndldic.c, SyncManager.java, ApiService.java, MediaProvide.java
SyncManager.java, MediaProvider.java, ndldic.c, ndbdic.c, ApiService.java
ndldic.c,ndbdic.c,ApiService.java,MediaProvider.java,SyncManager.java
ndldic.c, ndbdic.c, SyncManager.java, ApiService.java, MediaProvider.java,
SyncManager.java, MediaProvider.java, APIService.java, ndbdic.c, ndldic.c
ndldic.c, ndbdic.c, SyncManager.java,ApiService.java, MediaProvider.java
EOD;
$lines = array_filter(split(PHP_EOL, $str), function($s) { return trim($s); });
//$duplicates = array_filter(array_count_values($lines),function($num) { return $num > 1; });
//var_dump($duplicates);
$answers = array_map('strtolower', array(
'ndbdic.c',
'ndldic.c',
'ApiService.java',
'SyncManager.java',
'MediaProvider.java',
));
$variations = array_map('strtolower', array(
'AoiService.java',
'ApiServcie.java',
'ApiServer.java',
'ApiService.jave',
'ApiServices.java',
'ApjService.java',
'Media.Provider.java',
'MediaProvide.java',
'MediaProvider',
'MediaProviders.java',
'SWyncManager.java',
'SynManager.java',
'SyncManager',
'SyncManager.jaca',
'SyncManagerJava',
'SyncMaster.java',
'SynchManager.java',
'nbbdic.c',
'nbddic.c',
'ndbbic.c',
'ndbcdic.c',
'ndbdic.java',
'ndbdisc.c',
'ndbidic.c',
'ndcdic.c',
'ndlcdic.c',
'ndldic.',
'ndldic.c.',
'ndldic.java',
'nlddic.c',
));
$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);
natsort($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";
}
@codexp

This comment has been minimized.

Copy link
Owner Author

codexp commented Apr 28, 2015

Play with it on http://www.tehplayground.com/#PLbUprHm9
[Ctrl] + [Enter] to execute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.