As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab20 $ cd lab20
Write a function called compare(string1, string2)
,
which
takes two strings as parameters. The function should
return True if the two strings are the same, ignoring
capitalization and ignoring leading and trailing whitespace,
and False otherwise.
Write a function called swap_words(string)
, which
takes
a single string as a parameter. Assume that the specified
string
contains two words separated by exactly one space character.
The
function should return a new string that swaps the positions
of the
two words in the specified string.
Pig Latin is a somewhat silly language parody that is actually entirely English based. It is a simple transposition of letters that gives the effect of a "foreign" sounding language. However, even Thomas Jefferson couldn't resist writing letters to his friends in Pig Latin. The rules for converting an English word to Pig Latin are incredibly easy:
Write a function called convert_to_pig_latin(text)
in a
file called pig_latin.py. This function takes a string as
a parameter, and returns a string which is the pig latin translation
of the text parameter.
Make sure your program handles all necessary cases gracefully. What additional test cases should you check?
Function Parameters | Expected Output |
---|---|
"scram" | am'scray |
"immediate" | immediateyay |
Before converting the input text to Pig Latin, the function
must find the index of the first vowel. Finding the index
of the first vowel would be easier if there was a function
that returned whether a single character is a vowel. So
write the function is_vowel(character)
. The
function has one parameter character
an
alphabetic string of length 1. The function should
return True
if character
is 'a',
'e', 'i', 'o', or 'u' and False
otherwise.
Test this function before proceeding. Create a constant
string which stores all of the vowels, and check to see if
the character parameter is in
this string.
Finding the index of the first vowel requires traversing the
text and testing whether each character is a vowel. Writing
this as a separate function would make writing the
translation function easier. So write the
function index_of_first_vowel(text)
that
returns the index of first vowel in the specified text.
Since the location of the first vowel is unknown, the number
of times the traversal loop will need to run is unknown and
so the function will not be able to use a for loop to
traverse the string. Instead, the function should use a
while loop that increments an index variable every iteration
while the character at the index variable is not a vowel.
With the above two functions, writing the translation function is much easier. If the index of the first vowel is 0, then apply the first Pig Latin rule using concatenation. If the index of the first vowel is not 0, then apply the second Pig Latin rule using a slice and concatenation.
onvert_from_pig_latin'cay
at'thay
akes'tay ayay ord'way alreadyyay inyay ig'pay atin'lay, andyay
eturns'ray e'thay Englishyay equivalentyay.