-
Notifications
You must be signed in to change notification settings - Fork 0
/
unscramble-those-case-very-sensitive-strings.php
232 lines (194 loc) · 6.72 KB
/
unscramble-those-case-very-sensitive-strings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Code Golf: Unscramble those case-(very)-sensitive strings
*
* @link http://codegolf.stackexchange.com/questions/109117/unscramble-those-case-very-sensitive-strings
*/
// The code-golf framework includes the testing frameworks and handles the command line
require __DIR__.'/a/CodeGolfFramework.php';
/**
* Class UnscrambleThoseStrings - the problem at hand
*/
class UnscrambleThoseStrings extends ACodeGolfProblem
{
/////////////////////////////////////////////////////////////////////////
// The public interface
//
public function runTests()
{
$this->testCase('EdoC!', 'CodE!');
$this->testCase('lLEhW OroLd!', 'hELlO WorLd!');
$this->testCase('rpGOZmaimgn uplRzse naC DEoO LdGf', 'prOGRamming puzZles anD COdE GoLf');
$this->testCase('eIt uqHKC RBWOO xNf ujPMO SzRE HTL EOvd yAg', 'tHe quICK BROWN fOx juMPS OvER THE LAzy dOg');
$this->testCase('NraWgCi: Nsas-eNEiTIsev rNsiTG!!', 'WarNiNg: Case-sENsITive sTriNG!!');
}
public function getTheGolfedCode()
{
return $this->getFunctionBody('unscrmbl3', __CLASS__);
}
public function runTheGolfedCode($argc, array $argv)
{
$this->unscrmbl3($argc, $argv);
return 0;
}
/////////////////////////////////////////////////////////////////////////
// Test helpers
//
/**
* @param string $input the string to unscramble
* @param string $expected the expected output (the unscrambled string)
*/
protected function testCase($input, $expected)
{
//
// The first version: implement all the conditions, don't cut the corners
$actual = $this->getFunctionOutput(
function () use ($input) { $this->unscramble1(2, array(__FILE__, (string)$input)); }
);
it(
sprintf("unscrambles '%s' as '%s' - clear code #1", $input, $actual),
$actual === $expected
);
$actual = $this->getFunctionOutput(
function () use ($input) { $this->unscrmbl1(2, array(__FILE__, (string)$input)); }
);
it(
sprintf("unscrambles '%s' as '%s' - golfed code #1", $input, $actual),
$actual === $expected
);
if ('7' <= PHP_VERSION) {
$actual = $this->getFunctionOutput(
function () use ($input) { $this->unscrmbl1php7(2, array(__FILE__, (string)$input)); }
);
it(
sprintf("unscrambles '%s' as '%s' - golfed code #1, PHP7 version", $input, $actual),
$actual === $expected
);
}
//
// The second version: the arguments of preg_replace() can be arrays
$actual = $this->getFunctionOutput(
function () use ($input) { $this->unscramble2(2, array(__FILE__, (string)$input)); }
);
it(
sprintf("unscrambles '%s' as '%s' - clear code #2", $input, $actual),
$actual === $expected
);
$actual = $this->getFunctionOutput(
function () use ($input) { $this->unscrmbl2(2, array(__FILE__, (string)$input)); }
);
it(
sprintf("unscrambles '%s' as '%s' - golfed code #2", $input, $actual),
$actual === $expected
);
//
// The third version: compute the uppercase search string from the lowercase one
$actual = $this->getFunctionOutput(
function () use ($input) { $this->unscramble3(2, array(__FILE__, (string)$input)); }
);
it(
sprintf("unscrambles '%s' as '%s' - clear code #3", $input, $actual),
$actual === $expected
);
$actual = $this->getFunctionOutput(
function () use ($input) { $this->unscrmbl3(2, array(__FILE__, (string)$input)); }
);
it(
sprintf("unscrambles '%s' as '%s' - golfed code #3", $input, $actual),
$actual === $expected
);
}
/**
* @param int $argc
* @param string[] $argv
*/
protected function unscramble1($argc, array $argv)
{
echo preg_replace(
'/([a-z])([^a-z]*)([a-z])/', '$3$2$1',
preg_replace(
'/([A-Z])([^A-Z]*)([A-Z])/', '$3$2$1',
$argv[1]
)
);
}
/**
* The golfed version of unscramble1()
*
* @param int $argc
* @param string[] $argv
*/
protected function unscrmbl1($argc, array $argv)
{
$f=preg_replace;echo$f("/([a-z])([^a-z]*)([a-z])/",$r="$3$2$1",$f("/([A-Z])([^A-Z]*)([A-Z])/",$r,$argv[1]));
}
/**
* This function works only on PHP 7 and newer. It doesn't compile on PHP 5
*
* @param int $argc
* @param array $argv
*/
protected function unscrmbl1php7($argc, array $argv)
{
// The eval() is here to let PHP 5.6 compile the function. On PHP 7 there is no need for eval()
eval(' // This line is not needed on PHP 7
echo($f=preg_replace)("/([a-z])([^a-z]*)([a-z])/",$r="$3$2$1",$f("/([A-Z])([^A-Z]*)([A-Z])/",$r,$argv[1]));
'); // This line is not needed on PHP 7
}
/**
* Version #2, after a suggestion on the initial answer (preg_replace can use arrays)
* http://codegolf.stackexchange.com/questions/109117/unscramble-those-case-very-sensitive-strings/109161?noredirect=1#comment265979_109161
*
* @param int $argc
* @param string[] $argv
*/
protected function unscramble2($argc, array $argv)
{
echo preg_replace([
'/([a-z])([^a-z]*)([a-z])/',
'/([A-Z])([^A-Z]*)([A-Z])/',
],
'$3$2$1',
$argv[1]
);
}
/**
* The golfed version of unscramble2()
*
* @param int $argc
* @param string[] $argv
*/
protected function unscrmbl2($argc, array $argv)
{
echo preg_replace(["/([a-z])([^a-z]*)([a-z])/","/([A-Z])([^A-Z]*)([A-Z])/"],"$3$2$1",$argv[1]);
}
/**
* Version #3, squeeze several bytes more
*
* @param int $argc
* @param string[] $argv
*/
protected function unscramble3($argc, array $argv)
{
echo preg_replace([
$a='/([a-z])([^a-z]*)([a-z])/',
strtoupper($a),
],
'$3$2$1',
$argv[1]
);
}
/**
* The golfed version of unscramble3()
*
* @param int $argc
* @param string[] $argv
*/
protected function unscrmbl3($argc, array $argv)
{
echo preg_replace([$a="/([a-z])([^a-z]*)([a-z])/",strtoupper($a)],"$3$2$1",$argv[1]);
}
}
// Instantiate the problem and run it
exit((new UnscrambleThoseStrings($argc, $argv))->run());
// This is the end of file; no closing PHP tag