Fuzzy Equals
Write the Python function fuzzy_equals(string1,
string2)
that determines if two strings are equal when
capitalization and when leading and trailing whitespace are
ignored. The parameters string1 and string2
are strings containing any text. The function should
return True if the strings are the same when all
leading and trailing whitespace characters (spaces, tabs, and
return characters) are removed and when all capital letters are
converted to lower case. It should return False
otherwise.
Test Cases
print('Input: "?", "?"\t\tActual:', fuzzy_equals("?", "?"), '\tExpected: ?')
Swap Words
Write the Python function swap_words(text)
that
swaps the position of two words in a string. The
parameter text is a string that contains two words.
Assume that there is only one space character in text
and it is between the two words. The function should return a
new string with the position of the two words swapped. That is,
the first word should become the second word and the second word
should become the first word. The two words should still have a
space character between them and there should not be any space
characters at the begining or end.
Test Cases
print('Input: "?"\tActual: "' + swap_words("?") + '"\tExpected: "?"')
Input Sanitation
One of the biggest security vulnerabilities that web developers are constantly fighting is known as a SQL injection attack.. In this attack, users of a website format input in such a way that their input gets interpreted as code in the web developers systems. The easiest mechanism for preventing such an attack is to remove the offending characters before they get used in inappropriate ways.
Details
Write the Python function sanitize(text)
that
sanitizes text by removing all non-alphabetic characters. The
parameter text is a string containing any text. The
function should return a copy of text with all
characters that are not letters removed.
Test Cases
print('Input: "?"\t\tActual: "' + sanitize("?") + '"\tExpected: "?"')
- Use an accumulator to build the sanitized string from the input string one character at a time.
-
Test if a character is valid by using the
in
operator with the string "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
Challenge
In the real world, the true offending characters are double and
single quotes. Write a new function
called escape_quotes(text)
that returns a copy
of text with all single or double quotes "escaped". Recall
that a quote can be escaped in a string by pre-pending it with a
backslash (\"
or
\'
).
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. The rules for converting an English word to Pig Latin are incredibly easy:
- If the first character of the word is a vowel, append the string "yay" to the end.
- 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.
Details
Write the Python function to_pig_latin(text)
that
converts English to Pig Latin. The parameter text is a
string containing text in English. The function should
return text converted to Pig Latin as a string.
Test Cases
print('Input: "?"\t\tActual:', to_pig_latin("?"), '\tExpected: ?')
-
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 parametercharacter
an alphabetic string of length 1. The function should returnTrue
ifcharacter
is 'a', 'e', 'i', 'o', or 'u' andFalse
otherwise. Test this function before proceeding. -
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. - 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.
Challenge
om_pig_latin'fray
at'thay
akes'tay ayay ord'way alreadyyay inyay ig'pay atin'lay, andyay
eturns'ray e'thay Englishyay equivalentyay.