-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii-maya-numbers.php
349 lines (280 loc) · 7.09 KB
/
ascii-maya-numbers.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* ASCII Maya Numbers
*
* @link http://codegolf.stackexchange.com/questions/54240/ascii-maya-numbers
*/
// Check the PHP version
if (PHP_VERSION < '5.4') {
// Even PHP 5.4 is dead meat; it reached its end of life on September 14, 2014
exit("PHP 5.4 or newer is required. Dare to advance, don't live in the past!");
}
// Show all the errors, including the notices; this code doesn't trigger any
error_reporting(E_ALL/* & ~E_NOTICE*/);
// Load the test micro-frameworks
// This test framework works fine with one-line assertions; it displays the entire assertion in green
include 'a/TestFwInATweet-Xeoncross.php';
// This test framework is appropriate for multi-line assertions
include 'a/TestFrameworkInATweet.php';
//
// One-line assertions using a test framework that displays the code of the assertion in the output
test(maya(0) == '< >');
test(maya(1) == ' . ');
test(maya(2) == ' .. ');
test(maya(3) == '... ');
test(maya(4) == '....');
test(maya(5) == '----');
//
// Multi-line assertions need a different test framework; it gets the message to display as its first argument
// '#' replaces a trailing white space; it is needed to avoid the editor strip
// the white spaces at the end of line
it('correcty displays '.$i=6, maya($i) == str_replace('#', ' ', <<< END
.##
----
END
));
it('correcty displays '.$i=7, maya($i) == str_replace('#', ' ', <<< END
..#
----
END
));
it('correcty displays '.$i=8, maya($i) == str_replace('#', ' ', <<< END
...#
----
END
));
it('correcty displays '.$i=9, maya($i) == str_replace('#', ' ', <<< END
....
----
END
));
it('correcty displays '.$i=10, maya($i) == str_replace('#', ' ', <<< END
----
----
END
));
it('correcty displays '.$i=11, maya($i) == str_replace('#', ' ', <<< END
.##
----
----
END
));
it('correcty displays '.$i=12, maya($i) == str_replace('#', ' ', <<< END
..#
----
----
END
));
it('correcty displays '.$i=13, maya($i) == str_replace('#', ' ', <<< END
...#
----
----
END
));
it('correcty displays '.$i=14, maya($i) == str_replace('#', ' ', <<< END
....
----
----
END
));
it('correcty displays '.$i=15, maya($i) == str_replace('#', ' ', <<< END
----
----
----
END
));
it('correcty displays '.$i=16, maya($i) == str_replace('#', ' ', <<< END
.##
----
----
----
END
));
it('correcty displays '.$i=17, maya($i) == str_replace('#', ' ', <<< END
..#
----
----
----
END
));
it('correcty displays '.$i=18, maya($i) == str_replace('#', ' ', <<< END
...#
----
----
----
END
));
it('correcty displays '.$i=19, maya($i) == str_replace('#', ' ', <<< END
....
----
----
----
END
));
it('correcty displays '.$i=20, maya($i) == str_replace('#', ' ', <<< END
.##
< >
END
));
it('correcty displays '.$i=42, maya($i) == str_replace('#', ' ', <<< END
..#
..#
END
));
it('correcty displays '.$i=8000, maya($i) == str_replace('#', ' ', <<< END
.##
< >
< >
< >
END
));
it('correcty displays '.$i=8080, maya($i) == str_replace('#', ' ', <<< END
.##
< >
....
< >
END
));
it('correcty displays '.$i=123456789, maya($i) == str_replace('#', ' ', <<< END
.##
...#
----
----
----
.##
----
----
..#
----
----
.##
....
----
----
----
....
----
END
));
it('correcty displays '.$i=31415, maya($i) == str_replace('#', ' ', <<< END
...#
...#
----
----
----
----
----
----
----
----
END
));
it('correcty displays '.$i=2147483647, maya($i) == str_replace('#', ' ', <<< END
.##
...#
----
----
.##
----
----
.##
----
----
----
....
----
..#
..#
----
END
));
/**
* The test adapter.
*
* It emulates the behaviour of the CLI and collects the output to be tested.
*
* @param int $nb the number to represent using the Maya numerals
* @return string the output of the program
*/
function maya($nb)
{
$argv = array(1 => $nb);
ob_start();
echo rtrim(m($argv[1]), "\n");
return ob_get_clean();
}
/**
* The function that returns the Maya representation of the number. It calls itself recursively.
*
* @param integer $number the number to represent
* @return string the Maya representation of $number (the last line ends with a new line character too)
*/
function mayaNumber($number)
{
// Build the representation here
$output = '';
// Extract the last Maya digit (it is the remainder when the number is divided by 20)
$last = $number % 20; // the remainder is the last Maya digit
$quotient = ($number - $last) / 20; // the quotient is the number without the last Maya digit
// Call the function recursively to generate the Maya representation of the quotient and add an empty line after it
if ($quotient > 0) {
// ... but only if there is any quotient
$output = mayaNumber($quotient)."\n"; // put an empty line after it; it separates the groups
}
// Generate the Maya representation of the last digit and append it to the rest
if ($last > 0) {
// The Maya representation of a non-zero digit (1..19) starts with 1 to 4 points on the first line
// The number of points is the remainder when the digit is divided by 5
$rem = $last % 5; // the remainder
if ($rem > 0) {
// No remainder => no first line of points
$output .= substr(" . .. ... ....", $rem * 4 - 4, 4)."\n";
// Another way to get the same outcome is:
// $output .= [ ' . ', ' .. ', '... ', '....' ][$rem - 1]."\n";
// It requires PHP 5.5 but in the end both of them get squeezed to the same size.
}
// Add 1 to 3 lines of dashes
$dashes = ($last - $rem) / 5; // the number of lines of dashes is how many times 5 can fit in $last
$output .= str_repeat("----\n", $dashes);
} else {
// Zero
$output .= "< >\n";
}
// Return the generated Maya number
return $output;
}
/**
* The golfed version of the function mayaNumber()
*
* @param integer $n the number to represent
* @return string the Maya representation of $n
*/
function m($n){return(($c=($n-($r=$n%20))/20)?m($c)."\n":"").($r?(($q=$r%5)?substr(" . .. ... ....",$q*4-4,4)."\n":"").str_repeat("----\n",($r-$q)/5):"< >\n");}
//
// The golfed complete program (192 bytes):
// <?php
// function m($n){return(($c=($n-($r=$n%20))/20)?m($c)."\n":"").($r?(($q=$r%5)?substr(" . .. ... ....",$q*4-4,4)."\n":"").str_repeat("----\n",($r-$q)/5):"< >\n");}echo rtrim(m($argv[1]),"\n");
////////////////////////////////////////////////////////////////////////////////////////////////
// Another (longer) implementation that doesn't return a string but generates an array of lines
//
/**
* The test adapter
*
* @param integer $nb
* @return string
*/
function mayaX($nb)
{
$z = array();
u($nb,$z);
return implode("\n",$z);
}
/**
* The recursive function that produces the Maya representation of the number.
* It doesn't return the complete string but puts its lines into its second argument.
*
* @param integer $n
* @param array $z by reference
*/
function u($n,&$z){if($n){if($c=($n-($r=$n%20))/20){u($c,$z);$z[]='';}if($r){if($q=$r%5)$z[]=substr(' . .. ... ....',4*$q-4,4);for($r=($r-$q)/5;$r--;)$z[]='----';}else$z[]='< >';}else$z[]='< >';}
// This is the end of file; no closing PHP tag