Pig Latin is a language game where English words are altered to sound different, but using deceptively simple rules.
Here are some examples:
For simplicity, we'll say there are three main rules for converting an English word into a Pig Latin word. Each rule has the following format:
If the word starts with [something], then [make changes].
As a class, study the examples above and try to figure out the three main rules for converting an English word into a Pig Latin word.
Using these three rules, you could create a function to make a Pig Latin word from any supplied English word, then display it. Complete the gaps in the function pseudocode below:
Function convertToPigLatin(englishWord) pigWord ← '' If englishWord starts with a consonant then pigWord ← all except the first letter from englishWord Append the first letter from englishWord to pigWord End If If englishWord starts with __________________ then ___________________________________________________ ___________________________________________________ End If If englishWord starts with __________________ then ___________________________________________________ End If Append 'ay' to pigWord Display 'The Pig Latin word is ', pigWord End Function
See Wikipedia for more details on the rules for Pig Latin.
In this lesson, students will:
In the above video a simple function for drawing a square is improved by adding a parameter.
Let's do a vocabulary check:
For more on setting up, see Lesson 10. This lesson continues with a turtle graphics approach for each language.
The above video demonstrates writing a simple function with one parameter. Try it yourself!
The above video adds a second parameter to the function. Try it yourself!
Carefully read the pseudocode below.
1 BEGIN 2 Function drawDiamond(sizeFactor) 3 Pen down 4 Left 60 5 Forward 30 * sizeFactor 6 Left 60 7 Forward 30 * sizeFactor 8 Left 120 9 Forward 30 * sizeFactor 10 Left 60 11 Forward 30 * sizeFactor 12 Left 60 13 Pen up 14 EndFunction 15 16 drawDiamond(1) 17 Forward 50 18 drawDiamond(1.5) 19 Forward 50 20 drawDiamond(0.5) 21 Forward 50 22 END
On paper, try to predict what the program will draw. The turtle starts facing right.
Share with another student to see if you predicted the same outcome.
Carefully read the pseudocode below. This program does not use turtle graphics.
1 BEGIN 2 Function displayWordRepeatedly(word, multiple) 3 i ← 1 4 While i <= multiple 5 Display i + ' ' + word 6 i ← i + 1 7 End While 8 EndFunction 9 10 displayWordRepeatedly('banana', 5) 11 displayWordRepeatedly('apple', 3) 12 END
Predict the output of the program.
These challenges use the skills covered so far.
Write and test a function to create a car shape as below (must be all straight lines).
Now, modify your function to have a sizeFactor parameter, as with the diamond in step 4 of this lesson. Test it by calling it from the main program with arguments 1, 2 and 0.5.
Finally, call your finished function with specific values so as to generate a size chart of 2018 new car sales data for four countries - China, USA, UK and Australia. eg. China's sales were about 23.7 million, so use a size factor of 2.37. USA size factor would be 0.53, and so on.
Optional discussion question:
Do you think your function allows your chart to give a fair representation of the difference in car sales between these four countries? How could you adjust it to more accurately reflect the differences?
Write and test a function to generate and display the sequence of Hailstone numbers for any given starting number. The sequence should continue until you get to the number 1, or the sequence reaches a thousand numbers in length, whichever comes first.
Hailstone numbers are generated as follows:
eg. Starting with 12, we get the sequence: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1. This sequence reaches 1 with only ten numbers.
Begin by writing pseuocode for the function and a simple test. Your function will need a loop inside.
(More challenging) Write a function to convert an English word to Pig Latin and display it, using the rules in the Learning Hook section of this lesson.