Skip to content

Commit f055324

Browse files
committed
PHP and Javascript solutions to moderate challenge #143 - The Ministry of Truth
1 parent 4ea0b47 commit f055324

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// https://www.codeeval.com/open_challenges/143/
2+
3+
var fs = require("fs");
4+
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
5+
line = line.trim();
6+
if( line !== '' ){
7+
var tmp = line.split(';');
8+
var a = tmp[0];
9+
var result = a.split('');
10+
var a_len = a.length;
11+
var b = tmp[1].split(' ');
12+
var offset = -1;
13+
var pos = -1;
14+
var found = 0;
15+
var len = b.length;
16+
// console.log(line);
17+
for(var i=0; i < len ; i++){
18+
if( -1 != ( pos = a.indexOf( b[i] , offset+1 ) ) ){
19+
var b_len = b[i].length;
20+
// console.log(b[i], pos, offset);
21+
22+
for(var j = offset ; j < pos ; j++){
23+
if( a[j] != ' ' ){
24+
result[j] = '_';
25+
}
26+
else{
27+
result[j] = ' ';
28+
}
29+
}
30+
31+
offset = pos + b_len;
32+
found++;
33+
}
34+
}
35+
if( found == len ){
36+
for(var j = offset ; j < a_len ; j++){
37+
if( a[j] != ' ' ){
38+
result[j] = '_';
39+
}
40+
else{
41+
result[j] = ' ';
42+
}
43+
}
44+
console.log(result.join('').replace(/\s+/g,' '));
45+
}
46+
else{
47+
console.log('I cannot fix history');
48+
}
49+
50+
}
51+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
header('Content-Type: text/plain; charset=utf-8');
3+
4+
// https://www.codeeval.com/open_challenges/143/
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+
while ( $fp && !feof( $fp ) ) {
15+
$line = trim(fgets($fp));
16+
if( $line ){
17+
list($a,$b) = explode(';', $line);
18+
$a_len = strlen($a);
19+
$b = explode(' ',$b);
20+
$offset = -1;
21+
$pos = -1;
22+
$found = 0;
23+
$len = count($b);
24+
for($i=0; $i < $len ; $i++){
25+
if( $b[$i] && false !== ( $pos = strpos($a, $b[$i], $offset+1 ) ) ){
26+
$b_len = strlen($b[$i]);
27+
for($j = max(0,$offset) ; $j < $pos ; $j++){
28+
if( $a[$j] != ' ' ){
29+
$a[$j] = '_';
30+
}
31+
else{
32+
$a[$j] = ' ';
33+
}
34+
}
35+
36+
$offset = $pos + $b_len;
37+
$found++;
38+
}
39+
}
40+
if( $found && $found == $len ){
41+
for($j = $offset ; $j < $a_len ; $j++){
42+
if( $a[$j] != ' ' ){
43+
$a[$j] = '_';
44+
}
45+
else{
46+
$a[$j] = ' ';
47+
}
48+
}
49+
$a = str_replace(' ',' ',$a);
50+
$a = str_replace(' ',' ',$a);
51+
$a = str_replace(' ',' ',$a);
52+
echo $a."\n";
53+
}
54+
else{
55+
echo 'I cannot fix history'."\n";
56+
}
57+
58+
}
59+
60+
}//
61+
fclose( $fp );
62+
}
63+
else{
64+
echo '!fp'."\n";
65+
}
66+
}
67+
else{
68+
echo '!readable'."\n";
69+
}
70+
}
71+
else{
72+
echo '!file_exists'."\n";
73+
}
74+
}
75+
else{
76+
echo '!argv[1]'."\n";
77+
}
78+
79+
exit(0);
80+
81+
?>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Higher meaning;Hi mean
2+
this is impossible;im possible
3+
twenty two minutes;two minutes
4+
Higher meaning;e

0 commit comments

Comments
 (0)