Lab 4
Objectives
By the end of the lab, you will have:
- Gained practice writing
while
loops - Gained practice writing
for
loops with and withoutrange
The goal of this lab is to practice different ways of handling repetition in Python. Therefore, the autograder will enforce that functions are written as described using certain type of loops. Be careful!
The autograder will also enforce that you use no built-in functions besides len
and range
(where appropriate).
Pig Latin
Tsthay eekway eway areyay oinggay otay eachtay ethay omputercay Igpay Atinlay!
This week we are going to teach the computer Pig Latin!
Some of you will be familiar with Pig Latin, but I suspect most of you will at the very least need a refresher on the rules:
If the word starts with a vowel,
- append yay to the end
Examples:
- “ice” becomes “iceyay”
- “eye” becomes “eyeyay”
Otherwise,
- move all of the leading constants to the end of the word append ay to the new word
Examples:
- “doctor” becomes “octorday”
- “who” becomes “owhay”
There are some special rules about how to handle the character “y”. “y” is treated like a consonant when it is the first character in a word, and like a vowel otherwise.
Examples:
- “my” becomes “ymay”
- “ynambu”1 becomes “ambuynay”
This problem is a little easier if we break it down a little, so I’ve broken this into three parts.
For the sake of completing this lab, you may assume that all input for words will be lowercase letters a-z. All sentences will be words separated by a single space, with no trailing or leading whitespace.
Part 0: Setup
There is no starter code this time - just open up thonny and create a file lab04.py
in your labs
directory.
Part 1: Make a vowel finder
Your first task is to write a function the returns the index of the first vowel in a string. You are going to write two different versions of this function: first_vowel_index_for
and first_vowel_index_while
. Both have the same functionality, so we will use a generic first_vowel_index_*
as a placeholder in this description.
The most important thing to note is that like in pig latin, "y"
is not considered a vowel if it is the first character in the word, but is considered a vowel elsewhere in the word. When a vowel isn’t found, return -1
like you did for find
on the homework.
first_vowel_index_*
|
|||
---|---|---|---|
Parameters |
|
||
Returns |
int
|
Examples
>>> first_vowel_index_*("ice")
0
>>> first_vowel_index_*("who")
2
>>> first_vowel_index_*("my")
1
>>> first_vowel_index_*("ynambu")
2
>>> first_vowel_index_*("xxx")
-1
VOWELS
To make your life easier, create a variable called VOWELS
in the global scope with the value aeiou
. The variable name is uppercase following the python convention for constant variables (variables that do not change when running your program). You may make use of this variable in any of your functions as you see fit.
Part 1.1: With a for loop
The first function you’ll write is first_vowel_index_for
.
The purpose of writing this function is to get some practice writing a for
loop with range
. Follow the steps below:
- Step one: Define the function and make sure it takes a string argument
word
. Don’t forget your docstring! - Step two: Write a for loop using
range
such thati
goes from the index of the first character in the string to the index of the last character in the string. - Step three: In the body of your loop, check if the character at index
i
ofword
is one of theVOWELS
. If it is, return the index. - Step four: Outside of your loop at the end of your function, return
-1
. If you’ve reached the end of the string and haven’t returned anything, that means that no vowel was found. - Step five: Test your function with some of the examples. It should work for some of them, but not all – you haven’t handled the special case involving
"y"
yet! - Step six: Now let’s handle the special case with
"y"
s. You have two options for how to implement it (your choice!):- Write an
elif
that checks thati
isn’t the first index in the string and that the character at indexi
ofword
is"y"
, and if so, return the index - Add an
or
to the condition in yourif
statement that checks thati
isn’t the first index in the string and that the character at indexi
ofword
is"y"
- Write an
- Step seven: Test your function with the examples including
"y"
.
Part 1.2: With a while loop
The second function you’ll write is first_vowel_index_while
.
The purpose of writing this function is to get some practice writing a while
loop. We’ll convert the function we already wrote to one using a while
loop. Follow the steps below:
- Step one: Create a copy of your
first_vowel_index_for
function and call itfirst_vowel_index_while
. - Step two: Create a variable
i
before the loop and set the value to 0. - Step three: Change the first line of the for loop to use while instead. For the while loop, you need a condition instead of the “X in Y” syntax. You’ll increase
i
in each iteration of the loop, so you’ll want to check the value ofi
in relation to the length ofword
. - Step four: Make sure to increment
i
at the end of your loop to avoid infinite looping. - Step five: Test your function with the examples. It should work for all of them!
If you haven’t yet, I suggest that you pause for a second and submit to gradescope. Make sure that you are passing all of the first_vowel_index_*
function tests before continuing. The next functions depend on these working correctly.
Part 2: Convert a single word
Now that we’ve practiced a few ways to handle repetition in Python, let’s actually do some Pig Latin! Write a function pig_latin_word
:
pig_latin_word
|
||||
---|---|---|---|---|
Parameters |
|
|||
Returns |
str
|
Examples
>>> pig_latin_word("ice")
'iceyay'
>>> pig_latin_word("eye")
'eyeyay'
>>> pig_latin_word("doctor")
'octorday'
>>> pig_latin_word("who")
'owhay'
>>> pig_latin_word("my")
'ymay'
>>> pig_latin_word("ynambu")
'ambuynay'
- Step one: Define the function and make sure it takes a string argument
word
. Don’t forget your docstring! - Step two: Remember that the method for converting a word to pig latin depends on whether or not the first character is a vowel. Call one of2 your
first_vowel_index
functions to figure out the index of the first vowel inword
. If the first character is a vowel, the index returned will be 0, and you should return the original word with"yay"
at the end. - Step three: Now write your
else
case. Take all of the characters before the first vowel and move them to the end of the word, then append"ay"
. - Step four: Test your function with the examples above.
Part 3: Convert a whole sentence
Finally, we’ll convert a whole sentence to Pig Latin by writing a function pig_latin_sentence
. The purpose of writing this function is to get more practice with conditionals and to practice writing a for
loop that does not use range
.
pig_latin_sentence
|
||||
---|---|---|---|---|
Parameters |
|
|||
Returns |
str
|
Examples
>>> pig_latin_sentence("doctor who")
'octorday owhay'
>>> pig_latin_sentence("my yellow backpack")
'ymay ellowyay ackpackbay'
- Step one: Define the function and make sure it takes a string argument
sentence
. Don’t forget your docstring! - Step two: Write a for loop that goes through each character of the string by copying this line:
for char in sentence:
. Printchar
in the body of the loop to see what value it has in each iteration. - Step three: Our basic goal here is going to be to find the individual words in our sentence and convert them to pig latin. Start by creating a variable outside of your loop,
current_word
, which is an empty string. - Step four: In each iteration of your loop, add the character to
current_word
using+
3. You’ll see that this allows you to build a string in your loop. Print thecurrent_word
after the loop and confirm that it is identical tosentence
. - Step five: Now let’s actually find the words, not copy the whole sentence! Words are separated by spaces. So in our loop, let’s check if
char
is a space. If it is, printcurrent_word
and then setcurrent_word
to the empty string, as the word has ended. Move your code to append a character tocurrent_word
into anelse
case. Test your function on a sentence with multiple words - you should see them printed line-by-line. If you are missing the final word, discuss with your partner why that might be and add a print statement in an appropriate spot to print the last line. - Step six: Create a new variable before the loop to store your pig latin sentence, and make it an empty string. Everywhere you are printing
current_word
, append it’s Pig Latin version (plus a space) to your new variable. - Step seven: Return your variable containing the transformed sentence.
- Step eight: Test your function!
- Step nine: Depending on how you added the space, you may have an extra space at the start of the sentence or at the end. See if you can figure out how to stop that from happening.
Turning in your work
One you are done, submit lab04.py
to gradescope and ensure that you have passed all of the tests. Your file should contain five functions: first_vowel_index_for
, first_vowel_index_while
, pig_latin_word
, and pig_latin_sentence
.