Skip to content

Commit b4806c3

Browse files
committed
PHP and Javascript solutions to moderate challenge #11 - Lowest common ancestor
1 parent e61dc2c commit b4806c3

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// https://www.codeeval.com/open_challenges/11/
2+
3+
/* 30
4+
|
5+
____
6+
| |
7+
8 52
8+
|
9+
____
10+
| |
11+
3 20
12+
|
13+
____
14+
| |
15+
10 29
16+
*/
17+
18+
var ancestors = {
19+
'29': ['29','20','8','30'],
20+
'10': ['10','20','8','30'],
21+
'20': ['20','8','30'],
22+
'3': ['3','8','30'],
23+
'8': ['8','30'],
24+
'52': ['52','30'],
25+
'30': ['30']
26+
};
27+
28+
var fs = require("fs");
29+
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
30+
line = line.trim();
31+
if( line !== '' ){
32+
var tmp = line.split(' ');
33+
var a_ancestors = ancestors[tmp[0]];
34+
var b_ancestors = ancestors[tmp[1]];
35+
for(var i = 0, cnt = a_ancestors.length ; i < cnt ; i++){
36+
var a_ancestor = a_ancestors[i];
37+
if( -1 !== b_ancestors.indexOf(a_ancestor) ){
38+
console.log( a_ancestor );
39+
break;
40+
}
41+
}
42+
}
43+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
header('Content-Type: text/plain; charset=utf-8');
3+
4+
// https://www.codeeval.com/open_challenges/11/
5+
6+
if( isset($_GET['f_i_l_e']) && $_GET['f_i_l_e'] ) $argv[1] = $_GET['f_i_l_e'];
7+
8+
if( isset($argv[1]) && $argv[1] ){
9+
$filename = $argv[1];
10+
if( file_exists($filename) ){
11+
if( is_readable($filename) ){
12+
$fp = fopen($filename, 'r');
13+
if( $fp ){
14+
15+
$ancestors = array(
16+
'29' => array('29','20','8','30'),
17+
'10' => array('10','20','8','30'),
18+
'20' => array('20','8','30'),
19+
'3' => array('3','8','30'),
20+
'8' => array('8','30'),
21+
'52' => array('52','30'),
22+
'30' => array('30')
23+
);
24+
25+
while ( $fp && !feof( $fp ) ) {
26+
$line = trim(fgets($fp));
27+
if( $line ){
28+
list($a,$b) = explode(' ',$line);
29+
$a_ancestors = $ancestors[$a];
30+
$b_ancestors = $ancestors[$b];
31+
32+
for($i = 0, $cnt = count($a_ancestors) ; $i < $cnt ; $i++){
33+
$a_ancestor = $a_ancestors[$i];
34+
if( false !== array_search($a_ancestor,$b_ancestors) ){
35+
echo $a_ancestor."\n";
36+
break;
37+
}
38+
}
39+
}
40+
41+
}//
42+
fclose( $fp );
43+
}
44+
else{
45+
echo '!fp'."\n";
46+
}
47+
}
48+
else{
49+
echo '!readable'."\n";
50+
}
51+
}
52+
else{
53+
echo '!file_exists'."\n";
54+
}
55+
}
56+
else{
57+
echo '!argv[1]'."\n";
58+
}
59+
60+
exit(0);
61+
62+
?>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
30 8
2+
52 30
3+
20 10
4+
10 29
5+
3 10
6+
29 52
7+
52 20
8+
30 29
9+
20 29
10+
8 20
11+
8 52
12+
8 52
13+
29 30
14+
30 29
15+
52 8
16+
3 52
17+
29 8
18+
30 29
19+
29 3
20+
52 29

0 commit comments

Comments
 (0)