text stringlengths 0 897 |
|---|
(Notice that our `for` loop here is using a local variable called `i`.) |
The test cases below the function should print out correct splits of the test words: `["thr", "ee"]` for the "three", and `["", "omlets"]` for "omlets" (since that one begins with a vowel). |
D> Testing as you go makes programming faster, better, and more fun! Try to never write more than a handful of lines without testing them. |
Again make sure this is all working before the next part, in which we convert a single word into Pig Latin: |
{caption:Step 3 of our Pig Latin program} |
```miniscript |
// convert a single word into Pig Latin |
convertWord = function(word) |
parts = splitWord(word) |
if parts[0] == "" then return word + "yay" |
return parts[1] + parts[0] + "ay" |
end function |
print convertWord("three") |
print convertWord("omlets") |
``` |
I'm sure you're seeing the pattern by now, so we'll jump right into the meat of it: we call our own `splitWord` function to split the word into parts, which we store in local variable `parts`. If the first part is empty, then we just return the word plus "yay"; otherwise we reverse the parts and append "ay", per the r... |
The two test cases here should print "eethray" and "omletsyay". |
Almost done! Let's make a `convert` function that operates on a whole sentence. |
{caption:Step 4 of our Pig Latin program} |
```miniscript |
// convert a whole sentence into Pig Latin |
convert = function(sentence) |
words = sentence.split |
for i in words.indexes |
words[i] = convertWord(words[i]) |
end for |
return words.join |
end function |
print convert("this is a test") |
``` |
Nothing too complicated here! We split the sentence into words, and then run a `for` loop over their indexes. (Note that we're using a local variable `i` here too — and this is completely unrelated to the other `i` used in `splitWord`. They don't bother each other at all.) Each word is converted and stored back int... |
And the program is now complete, apart from a main loop! If you take out the test cases, and add a main loop to the end, it will look something like the complete program below. |
{caption:Complete Pig Latin program} |
```miniscript |
// return true if c is a vowel, false if not |
isVowel = function(c) |
return "aeiou".indexOf(c) != null |
end function |
// split a word into the part before the first vowel, |
// and the part from the first vowel to the end |
splitWord = function(word) |
for i in word.indexes |
if isVowel(word[i]) then return [word[:i], word[i:]] |
end for |
return [word, ""] // no vowel found! |
end function |
// convert a single word into Pig Latin |
convertWord = function(word) |
parts = splitWord(word) |
if parts[0] == "" then return word + "yay" |
return parts[1] + parts[0] + "ay" |
end function |
// convert a whole sentence into Pig Latin |
convert = function(sentence) |
words = sentence.split |
for i in words.indexes |
words[i] = convertWord(words[i]) |
end for |
return words.join |
end function |
// main loop: convert sentences until empty input |
while true |
inp = input("Plain sentence? ") |
if inp == "" then break // (enter empty string to exit) |
print convert(inp) |
end while |
``` |
And now you've seen how real programming is done! A complex task was broken down into simpler tasks, and then written bottom up, starting with the tiniest, simplest function. Each other function made use of the ones written before, so that none of them was particularly complex. |
A> **Chapter Review** |
A> - You learned how to define your own functions, with or without parameters. |
A> - You encountered the DRY (Don't Repeat Yourself) principle, and saw how to break big programs into lots of small functions. |
A> - You gained insight into how variables work under the hood, and the important difference between global and local variables. |
A> - You wrote a complex Pig Latin program by writing a bunch of simple functions that lean on each other to get the job done. |
{chapterHead: "Day 12: Object-Oriented Programming", startingPageNum:129} |
{width: "50%"} |
 |
Q> Controlling complexity is the essence of computer programming. |
Q>— Brian Kernigan (computer scientist, author) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.