Skip to content

Commit a5ecd86

Browse files
committed
PHP and Javascript solutions to moderate challenges #76 - String Rotation
1 parent 3794573 commit a5ecd86

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://www.codeeval.com/open_challenges/76/
2+
var fs = require("fs");
3+
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
4+
line = line.trim();
5+
if( line !== '' ){
6+
var tmp = line.split(',');
7+
var a = tmp[0];
8+
var b = tmp[1];
9+
10+
var offset = 0;
11+
var ok = false;
12+
while( -1 !== ( pos = b.indexOf(a[0], offset) ) ){
13+
var start = b.substr( pos );
14+
var end = b.substr(0, pos);
15+
if( a == start+end ){
16+
ok = true;
17+
}
18+
offset = pos + 1;
19+
}//
20+
console.log( ok ? 'True' : 'False');
21+
}
22+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
header('Content-Type: text/plain; charset=utf-8');
3+
4+
// https://www.codeeval.com/open_challenges/76/
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+
$offset = 0;
19+
$ok = false;
20+
while( false !== ( $pos = strpos($b, $a[0], $offset) ) ){
21+
$start = substr($b, $pos);
22+
$end = substr($b, 0, $pos);
23+
if( $a == $start.$end ){
24+
$ok = true;
25+
}
26+
$offset = $pos + 1;
27+
}//
28+
echo ($ok ? 'True' : 'False') . "\n";
29+
}
30+
31+
}//
32+
fclose( $fp );
33+
}
34+
else{
35+
echo '!fp'."\n";
36+
}
37+
}
38+
else{
39+
echo '!readable'."\n";
40+
}
41+
}
42+
else{
43+
echo '!file_exists'."\n";
44+
}
45+
}
46+
else{
47+
echo '!argv[1]'."\n";
48+
}
49+
50+
exit(0);
51+
52+
?>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
M8TszRYeARzZF9GBCVrI5MnR,YeARzZF9GBCVrI5MnRM8TszR
2+
Hello,lloHe
3+
Basefont,tBasefon
4+
MQi4oZvNEUmccgE2EA27ZpCvVWDJ,EUmccgE2EA27ZpCvVWDJMQi4oZvN
5+
8J8qrQ6sd3Hvzmgv4P0LMZ,qrQ6sd3Hvzmgv4P0LMZ8J8
6+
Acquisitions,isitionsAcqu
7+
7gNHLdpHmmAqPc6WFu7s5x,xgNHLdpHmmAqPc6WFu7s57
8+
JqDT4crse0yeyXiEPmmZrHI9FxRu P,PqDT4crse0yeyXiEPmmZrHI9FxRu J
9+
n8USuLgHAd8CKMgnHiYJSfi8J,i8Jn8USuLgHAd8CKMgnHiYJSf
10+
Hello,lloHe
11+
BusNpxbk7DxpMGNN6u1HOCm,musNpxbk7DxpMGNN6u1HOCB
12+
Trial,alrTi
13+
KSLP2b81bGd07ozNgMBci,ozNgMBciKSLP2b81bGd07
14+
v7UBoHz8ta26jl1Zgf5uJ,J7UBoHz8ta26jl1Zgf5uv
15+
OUoYsqUIXPQQp017 DeHfK,KUoYsqUIXPQQp017 DeHfO
16+
BSWoTNATfNmkfoZbX3G3,X3G3BSWoTNATfNmkfoZb
17+
d7B5nAKsKmQzxwl2LIwN1R,R7B5nAKsKmQzxwl2LIwN1d
18+
JgLhUYsXAS5YykVY2rt6Vjc,Y2rt6VjcJgLhUYsXAS5YykV
19+
Basefont,tBasefon
20+
Permute,mutePer
21+
EaGMsw40rQZKXZtSxDEmQ4rIzhx 1,1aGMsw40rQZKXZtSxDEmQ4rIzhx E
22+
PmG4j jm3BPQ7HpJpFNh,pJpFNhPmG4j jm3BPQ7H

0 commit comments

Comments
 (0)