|
home—lects—exams—hws
D2L—breeze (snow day)
Suppose we want to write php code that blends two words, concatenating the first half of the first word and the second half of the second word: 1
blend("motor", "hotel") === "motel" blend("smoke", "fog") === "smog" blend("Ian","Barland") === "Iland"In the case of odd-length words, we'll "round down" the midpoint (this is handy since its what quotient (integer-division) by 2 does).
The first step in writing any program, as always, is to come up with test cases,
including degenerate inputs.
(Why do we bother with trivial/degenerate inputs?
To make any debugging easy:
If the program we're going to write goofs up on the empty string,
that will be an easy case to track down and debug.)
blend("a","") === "" blend("","a") === "a" blend("","abc") === "bc" blend("abc","") === "a" blend("","") === "" blend("a","1") === "1" blend("ab","12") === "a2" blend("motor","hotel") === "motel" blend("smoke","fog") === "smog" blend("Ian","Barland") === "Iland"
In php:
<?php $fname = "Ian"; $lname = "Barland"; $fHalf = substr($fname, 0, strlen($fname)/2 ); $lHalf = substr( $lname, strlen($lname)/2 ); $theBlend = $fHalf . $lHalf; echo "Hi ", $fname, " ", $lname, ".\n"; echo "I shall call you ", $theBlend; ?>We can test this by running php blend.php from the command line.
We can make functions with the keyword “
<?php function blend( $fname, $lname ) { $fHalf = substr($fname, 0, strlen($fname)/2 ); $lHalf = substr( $lname, strlen($lname)/2 ); $theBlend = $fHalf . $lHalf; return $theBlend; } echo "Hi Ian Barland. " echo "I shall call you ", blend("Ian","Barland"); ?> |
// Print an error message if the two arguments // aren't the same. // function test($actual,$expected) { if ($actual === $expected) { echo "."; } else { echo "Expected \"", $expected, "\",\n"; echo "but got \"", $actual, "\".\n"; } } test( blend("a","") , "" ); test( blend("","a") , "a" ); test( blend("","abc") , "bc" ); test( blend("abc","") , "a" ); test( blend("","") , "" ); test( blend("a","1") , "1" ); test( blend("ab","12") , "a2" ); test( blend("motor","hotel") , "motel" ); test( blend("smoke","fog") , "smog" ); test( blend("Ian","Barland") , "Iland" ); |
Final step: Let's put this program on the web!
// ... definitions of `blend` as above... echo ""; echo " |
The final program, cleaned up:
1 <html> 2 <head> 3 <title>Rawr</title> 4 5 6 <?php 7 8 // Turn this flag off, to suppress output for test cases that pass: 9 $SHOW_PASSED_TESTS = true; 10 // (Note that we will always run the test cases, 11 // and we'll always print any failed tests.) 12 // 13 // Also, for future: consider using a php constant instead of a variable: 14 // define('SHOW_PASSED_TESTS',true); 15 // and then later refer to SHOW_PASSED_TESTS (no preceding $) 16 17 18 19 20 21 /** Return a blend of two words: 22 * @param $word1 The word to take the first half from. 23 * @param $word2 The word to take the second half from. 24 * @return the first half of $word1 concatenated to the second half of $word2. 25 * (If a word is odd length, we round the halfway-point down.) 26 */ 27 function blend( $word1, $word2 ) { 28 $fHalf = substr($word1, 0, strlen($word1)/2 ); 29 $lHalf = substr( $word2, strlen($word2)/2 ); 30 return $fHalf . $lHalf; 31 } 32 33 34 /** Check whether the actual and expected values are the same. 35 * If not, print an error message. 36 * @param $actual The actual result of the function-call. 37 * @param $expected The expected (desired) result of the function-call. 38 */ 39 function test($actual,$expected) { 40 global $SHOW_PASSED_TESTS; 41 if ($actual !== $expected) { 42 echo "Test failed:\n"; 43 echo "Expected \"", $expected, "\",\n"; 44 echo "but got \"", $actual, "\".\n"; 45 } 46 else { 47 if ($SHOW_PASSED_TESTS) { echo "."; } 48 } 49 } 50 51 52 test( blend("a", ""), "" ); 53 test( blend("", "a"), "a" ); 54 test( blend("", "abc"), "bc" ); 55 test( blend("abc", ""), "a" ); 56 test( blend("", ""), "" ); 57 test( blend("a", "1"), "1" ); 58 test( blend("ab", "12"), "a2" ); 59 test( blend("motor", "hotel"), "motel" ); 60 test( blend("smoke", "fog"), "smog" ); 61 test( blend("Ian", "Barland"), "Iland" ); 62 ?> 63 </head> 64 65 66 <body> 67 Hi Ian Barland -- I shall call you <?php echo blend("Ian","Barland") ?>. 68 (How do you like that?) 69 </body> 70 71 </html> |
1 In real linguistics, a blend probably divides on syllables at least; see blendwords.htm. But we'll stay simple-minded and just blend by word-length. ↩
home—lects—exams—hws
D2L—breeze (snow day)
©2012, Ian Barland, Radford University Last modified 2012.Aug.31 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |