Scientific Computing

Activity 9


Compare Strings

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.


Test Code Output
print(compare('Hi', 'Hi')) True
print(compare('Hi', 'hi')) True
print(compare('hi', 'Hi')) True
print(compare(' hi', 'hi')) True
print(compare('hi', ' hi')) True
print(compare('hi.', 'hi!')) False

Swap Words

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.


Test Code Output
print(swap_words('hello world')) world hello
print(swap_words('world hello')) hello world

Pig Latin Translator

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:

  1. If the first character of the word is a vowel, append the string “yay” to the end.
  2. If the first character is a consonant, then all consonants up to (but not including) the first vowel are moved to the end of the word. A character is placed between the transposed characters. Then the string “ay” is appended to the word.


Write a program that prompts the user to enter a word and prints the specified word in Pig Latin.


Enter a word: scram
scram in Pig Latin is: am'scray

Enter a word: immediate
immediate in Pig Latin is: immediateyay


  1. Before converting the input text to Pig Latin, the program must find the index of the first vowel. Finding the index of the first vowel is a little tricky, so creating a function to do it will make writing the rest of the program easier.

  2. Write a function index_of_first_vowel(text) that returns the index of the first vowel character in the specified string. It is simple to find the first index of a single vowel using the find method. And it is fairly simple to find the smallest index of all of the first indices of each vowel using the built-in min method. However, because the find method returns -1 if a character is not in the string, the min function will return -1 if any vowels are not in the string. To solve this you will have to write some more complex conditional statements that check whether a vowel is in the string before finding its index. If the vowel is not in the string, set a variable for the index of the vowel to something large, so that it will not be smallest when the min function is used. If the vowel is in the string, set the variable for the index of the vowel to the actual index of the vowel.

  3. With the above function, writing the translation is simple. 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.


Please show your source code and run your programs for the instructor or lab assistant. Only programs that have perfect functionality will be accepted as complete.