|
home—lects—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 it's 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; ?>If we put the above lines into a file named, say, “blend.php” then we can run it (after connecting to
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"); ?> |
In practice, we'd of course want to run the test-cases we'd constructed earlier:
<?php /** Return a blend of two words: * @param $word1 The word to take the first half from. * @param $word2 The word to take the second half from. * @return the first half of $word1 concatenated to the second half of $word2. * (If a word is odd length, we round the halfway-point down.) */ function blend( $str1, $str2 ) { $mid1 = strlen($str1)/2; $mid2 = strlen($str2)/2; $firstHalf = substr($str1, 0, $mid1 ); $secondHalf = substr($str2, $mid2 ); return $firstHalf . $secondHalf; } /* function blend( $word1, $word2 ) { $fHalf = substr($word1, 0, strlen($word1)/2 ); $lHalf = substr( $word2, strlen($word2)/2 ); return $fHalf . $lHalf; } */ // Run our tests: echo "\nexpect: " . "motel"; echo "\nactual: " . blend("motor", "hotel"); echo "\nexpect: " . "smog"; echo "\nactual: " . blend("smoke", "fog"); echo "\nexpect: " . "Iland"; echo "\nactual: " . blend("Ian", "Barland"); echo "\nexpect: " . ""; echo "\nactual: " . blend("a", ""); echo "\nexpect: " . "a"; echo "\nactual: " . blend("", "a"); echo "\nexpect: " . "bc"; echo "\nactual: " . blend("", "abc"); echo "\nexpect: " . "a"; echo "\nactual: " . blend("abc", ""); echo "\nexpect: " . ""; echo "\nactual: " . blend("", ""); echo "\nexpect: " . "1"; echo "\nactual: " . blend("a", "1"); echo "\nexpect: " . "a2"; echo "\nactual: " . blend("ab", "12"); echo "\n"; echo " Hi Ian Barland -- I shall call you ", blend("Ian","Barland"), "\n"; echo " (How do you like that?)\n"; ?> |
1 In real linguistics, a blend probably divides on syllables at least; see www.montgomerycollege.edu/~steuben/blendwords.htm. But we'll stay simple-minded and just blend by word-length. ↩
home—lects—hws
D2L—breeze (snow day)
©2013, Ian Barland, Radford University Last modified 2014.Jan.24 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |