-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassessment.php
474 lines (426 loc) · 15.8 KB
/
assessment.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/**
* Varland Metal Service, Inc. base class for command line programs.
*
* @author Toby Varland <toby.varland@varland.com>
* @copyright 2016 Varland Metal Service, Inc.
* @version 1.0
*/
abstract class CLP {
/**
* Adds formatting codes for Mac/Linux terminal to string.
*
* @param string $text Text to add formatting codes to.
* @param string[] $codes Formatting codes to add.
* @return string
*/
protected static function format($text, $codes) {
$os = php_uname('s');
$windows = (substr_count($os, 'Windows') != 0);
if (!$windows) {
$formatCodes = array();
foreach ($codes as $code) {
$formatCodes[] = "\033[{$code}m";
}
return implode(NULL, $formatCodes) . " {$text} \033[0m";
} else {
return $text;
}
}
/**
* Adds formatting codes for red background/white text to string.
*
* @param string $text Text to add formatting codes to.
* @return string
*/
protected static function formatRed($text) {
return self::format($text, array('41', '1;37'));
}
/**
* Adds formatting codes for blue background/black text to string.
*
* @param string $text Text to add formatting codes to.
* @return string
*/
protected static function formatBlue($text) {
return self::format($text, array('46', '1;30'));
}
/**
* Adds formatting codes for green background/black text to string.
*
* @param string $text Text to add formatting codes to.
* @return string
*/
protected static function formatGreen($text) {
return self::format($text, array('42', '1;30'));
}
/**
* Clears terminal window. Doesn't work very well on Windows - just prints
* blank lines to clear screen.
*
* @return void
*/
protected static function clearScreen() {
$os = php_uname('s');
$windows = (substr_count($os, 'Windows') != 0);
if ($windows) {
for ($i = 0; $i < 200; $i++) { echo("\n"); }
} else {
@system('clear');
}
}
/**
* Waits for user to press enter key to continue.
*
* @return void
*/
protected static function enterToContinue() {
echo("Press enter to continue...");
stream_get_line(STDIN, 1024, PHP_EOL);
}
/**
* Prints program main menu and handles user input.
*
* @return void
*/
abstract public static function mainMenu();
}
/**
* Varland Metal Service, Inc. Programming Assessment command line program.
*
* @author Toby Varland <toby.varland@varland.com>
* @copyright 2016 Varland Metal Service, Inc.
* @version 1.0
*/
class VMS extends CLP {
/**
* Prints program main menu and handles user input.
*
* @return void
*/
public static function mainMenu() {
// Initialize response and error message to empty values.
$response = NULL;
$message = NULL;
// Execute in a loop until the quit menu option is selected.
while ($response != '5') {
// Clear screen and print out main program menu.
self::clearScreen();
echo("Varland Metal Service, Inc. Programming Assessment\n");
echo(str_repeat('=', 50) . "\n\n");
if ($message !== NULL) {
echo(self::formatRed("***** {$message} *****") . "\n\n");
$message = NULL;
}
echo("Main Menu\n---------\n\n");
echo("1. FizzBuzz\n");
echo("2. Only Survivor\n");
echo("3. Number to Text\n");
echo("4. String Validation\n");
echo("5. Quit\n\n");
echo("Enter option number >> ");
// Read user input and handle.
$response = stream_get_line(STDIN, 1024, PHP_EOL);
switch ($response) {
case '1':
self::clearScreen();
echo("Running your FizzBuzz function for n = 20:\n\n");
ob_start();
self::fizzbuzz(20);
$fizzbuzz = trim(ob_get_contents());
ob_end_clean();
$fizzbuzzResults = array('1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz',
'11', 'Fizz', '13', '14', 'FizzBuzz', '16', '17', 'Fizz', '19', 'Buzz');
$pass = ($fizzbuzz == implode("\n", $fizzbuzzResults));
echo(($pass ? self::formatGreen('PASS') : self::formatRed('FAIL')) . "\n");
echo("Expected...: " . implode('|', $fizzbuzzResults));
if (!$pass) {
echo("\nYour Result: " . str_replace("\n", '|', $fizzbuzz));
if (!$fizzbuzz) {
echo("\n\n" . self::formatBlue('Note: your code should output data using "echo", not return data.'));
} else {
echo("\n\n" . self::formatBlue('Note: your code should output newline characters, not "|" characters.'));
}
}
echo("\n\n");
self::enterToContinue();
break;
case '2':
self::clearScreen();
echo("Running Only Survivor tests:\n\n");
$tests = array();
$tests[] = (object)array('players' => 10,
'result' => 5);
$tests[] = (object)array('players' => 5,
'result' => 3);
$tests[] = (object)array('players' => 13,
'result' => 11);
$tests[] = (object)array('players' => 96,
'result' => 65);
$tests[] = (object)array('players' => 1,
'result' => 1);
foreach ($tests as $index => $test) {
$result = self::onlySurvivor($test->players);
echo(($index + 1) . ': ' . ($result == $test->result ? self::formatGreen('PASS') : self::formatRed('FAIL')) . "\n");
echo(" # Players..: " . number_format($test->players) . "\n");
echo(" Winner.....: " . number_format($test->result));
if ($result != $test->result) {
echo("\n Your Answer: " . number_format($result));
}
echo("\n\n");
}
self::enterToContinue();
break;
case '3':
self::clearScreen();
echo("Running Number to Text tests:\n\n");
$tests = array();
$tests[] = (object)array('number' => 16,
'result' => 'sixteen');
$tests[] = (object)array('number' => 123456789,
'result' => 'one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine');
$tests[] = (object)array('number' => 100,
'result' => 'one hundred');
$tests[] = (object)array('number' => 101,
'result' => 'one hundred one');
$tests[] = (object)array('number' => 1001,
'result' => 'one thousand one');
$tests[] = (object)array('number' => 211565768432819,
'result' => 'two hundred eleven trillion five hundred sixty-five billion seven hundred sixty-eight million four hundred thirty-two thousand eight hundred nineteen');
foreach ($tests as $index => $test) {
$result = self::numberToText($test->number);
echo(($index + 1) . ': ' . ($result == $test->result ? self::formatGreen('PASS') : self::formatRed('FAIL')) . "\n");
echo(" Test Value.: " . number_format($test->number) . "\n");
echo(" Expected...: {$test->result}");
if ($result != $test->result) {
echo("\n Your Answer: {$result}");
}
echo("\n\n");
}
self::enterToContinue();
break;
case '4':
self::clearScreen();
echo("Running String Validation tests:\n\n");
$tests = array();
$tests[] = (object)array('format' => 'abcd@?bcd.ca',
'result' => 5);
$tests[] = (object)array('format' => 'a??@???.af',
'result' => 0);
$tests[] = (object)array('format' => '??????????',
'result' => 11562500);
$tests[] = (object)array('format' => 'a?c@b?c',
'result' => 6);
$tests[] = (object)array('format' => 'a????.?',
'result' => 2125);
$tests[] = (object)array('format' => 'a?c@b?c.?',
'result' => 180);
foreach ($tests as $index => $test) {
$result = self::countValidStrings($test->format);
echo(($index + 1) . ': ' . ($result == $test->result ? self::formatGreen('PASS') : self::formatRed('FAIL')) . "\n");
echo(" Test Format: {$test->format}\n");
echo(" Expected...: " . number_format($test->result));
if ($result != $test->result) {
echo("\n Your Answer: " . number_format($result));
}
echo("\n\n");
}
self::enterToContinue();
break;
case '5':
self::clearScreen();
echo(self::formatBlue(str_repeat('*', 75)) . "\n");
echo(self::formatBlue('* Please submit to Toby Varland <toby.varland@varland.com> when finished. *') . "\n");
echo(self::formatBlue(str_repeat('*', 75)) . "\n\n");
self::enterToContinue();
self::clearScreen();
break;
default:
$message = 'Invalid option selected.';
}
}
}
/**
* Prints each number from 1..n on a separate line except in the following
* cases:
*
* 1. If the number is divisible by 3, print "Fizz"
* 2. If the number is divisible by 5, print "Buzz"
* 3. If the number is divisible by 3 and 5, print "FizzBuzz"
*
* @param int $n Upper limit of range of numbers to print.
* @return void
*/
protected static function fizzbuzz($n) {
}
/**
* Determine the winner of a game where each player knocks out the next player
* in line until only one survivor remains. When the end of the line is reached,
* the game loops around (so it's possible for the last player in line to knock
* out the first player).
*
* Consider the example with 9 players:
*
* 1 knocks out 2
* 3 knocks out 4
* 5 knocks out 6
* 7 knocks out 8
* 9 knocks out 1
* 3 knocks out 5
* 7 knocks out 9
* 3 knocks out 7
*
* 3 is the winner (and the return value for the function)
*
* @param int $players Number of players who start the game.
* @return int
*/
protected static function onlySurvivor($players) {
}
/**
* Converts a number to text. Works for the range of 0 <= $number < 10^15.
*
* @param int $number Number to be converted to text.
* @return string
*/
protected static function numberToText($number) {
// Special case - return result if zero.
if ($number == 0) return 'zero';
// Store major number groups.
$groups = array();
$groups[] = (object)array('label' => ' million',
'divisor' => 1000000);
$groups[] = (object)array('label' => ' thousand',
'divisor' => 1000);
$groups[] = (object)array('label' => '',
'divisor' => 1);
// Find results.
$results = array();
foreach ($groups as $group) {
$groupValue = (int)($number / $group->divisor);
$number -= ($groupValue * $group->divisor);
if ($groupValue > 0) {
$results[] = self::threeDigitNumberToText($groupValue) . $group->label;
}
}
// Return result.
return implode(' ', $results);
}
/**
* Converts a three digit number to text. Works for the range of 0 <= $number < 1000.
*
* @param int $number Number to be converted to text.
* @return string
*/
protected static function threeDigitNumberToText($number) {
// Store single digit cardinal values.
$ones = array('zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine');
// Store tens cardinal values.
$tens = array(FALSE,
'ten',
'twenty',
'thirty',
'forty',
'fifty',
'sixty',
'seventy',
'eighty',
'ninety');
// Store special cases.
$specialCases = array(11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen');
// Initialize result.
$result = array();
// If number is three digits, add hundreds to result string.
if ($number >= 100) {
$hundreds = (int)($number / 100);
$result[] = "{$ones[$hundreds]} hundred";
$number -= ($hundreds * 100);
}
// If remainder is a special case, add it. Otherwise use standard form.
if ($number > 0) {
if (in_array($number, array_keys($specialCases))) {
$result[] = $specialCases[$number];
} else {
$tensPosition = (int)($number / 10);
$onesPosition = $number % 10;
if ($tensPosition > 0 && $onesPosition > 0) {
$result[] = $tens[$tensPosition]
. ' '
. $ones[$onesPosition];
} elseif ($tensPosition > 0) {
$result[] = $tens[$tensPosition];
} elseif ($onesPosition > 0) {
$result[] = $ones[$onesPosition];
}
}
}
// Return full cardinal number.
return implode(' ', $result);
}
/**
* Counts number of valid strings can be formed using the given format. The given format may
* contain one or more "?" wildcard characters. The string is valid if it:
*
* 1. Starts with one or more non-empty groups of characters a - e separated by
* a period,
* 2. Contains exactly one "@" symbol, and
* 3. Ends with two or more non-empty groups of characters a - e separated by
* a period.
*
* @param string $format String format to test.
* @return int
*/
protected static function countValidStrings($format) {
// Store regex for determining whether given string is valid.
$regex = '/^$/';
// If format doesn't contain wildcard, return 1 if valid string.
if (substr_count($format, '?') == 0) {
return (preg_match($regex, $format) == 1 ? 1 : 0);
}
// Store replacement characters. Only allow "@" if not already present.
$replacementCharacters = array('a', '.');
if (substr_count($pattern, '@') == 0) $replacementCharacters[] = '@';
// Initialize count to 0.
$countValid = 0;
// Try matching regular expression with each valid replacement character.
// Instead of trying a - e, only use a but with 5x multiplier.
foreach ($replacementCharacters as $char) {
// Store multiplier.
$multiplier = ($char == 'a' ? 5 : 1);
// Generate test string by substituting for first wildcard.
$test = preg_replace('/\?/', $char, $format, 1);
// If test string still contains wildcard, add result of recursive function
// call. Otherwise test string for validity.
if (substr_count($test, '?') > 0) {
$countValid += $multiplier * self::countValidStrings($test);
} else {
$valid = preg_match($regex, $test);
if ($valid == 1) {
$countValid += $multiplier;
}
}
}
// Return valid matches.
return $countValid;
}
}
/** Execute main menu function on script run. */
VMS::mainMenu();
?>