Substring
Write the function substring(text: str, start: int, end: int) -> str
that returns a contiguous subsequence of characters in a string. The parameter text is a string, and the parameters start and end are non-negative integers. The function should return a copy of the characters from text between start and end. The substring should include the character at the index start, but it should exclude the character at the index end. Assume that both start and end are between 0 and the length of text and that start is less than end. The function should assert that start and end are valid indices.
Test Cases
import test
def substring(text: str, start: int, end: int) -> str:
# Put your code here
def main() -> None:
"hello", 1, 4), "ell")
test.equal(substring(# Put more test cases here
return None
main()
Password Generation
Passwords are possibly the most important way that individuals can ensure their own safety on the internet. However, it is also typically the easiest thing for a hacker to figure out or get their hands on. This is simply because most internet users choose incredibly weak passwords. The easiest way to solve this is to use a randomly generated password.
Details
Write the Python function generate_password(size: int) -> str
that returns a string of random lowercase letters. It should be possible for the generated password to contain any letter. The parameter size is a positive integer and is the number of characters in the returned password.
Example
The code:
print(generate_password(5))
print(generate_password(10))
Might print:
crkkb
bgchwxqybn
Hint
The function should repeatedly generate a random character and use string concatenate to accumulate the characters into a single string.
There is no built-in way to generate random characters. There is a way to generate random numbers with the random module.
Use random numbers and the index operator, [], to copy random characters out of the string "abcdefghijklmnopqrstuvwxyz".
Challenge
Even random strings of lowercase characters are pretty easy to break. A better program would also include uppercase letters and digits. Alter your function so that it is guaranteed to have at least 1 lowercase letter, 1 upper-case letter, and 1 digit. Note, the location of the required characters should still be completely random.
Input Sanitation
One of the most significant security vulnerabilities web developers are always fighting is a SQL injection attack. In this attack, users of a website format input so that their information gets interpreted as code in the web developers’ systems. The most straightforward 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: str) -> str
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
import test
def sanitize(text: str) -> str:
# Put your code here
def main() -> None:
"Hello, World!"), "HelloWorld")
test.equal(sanitize(# Put more test cases here
return None
main()
Hint
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
count
function you previously created with the string "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".If the count of the character is 1, then it is an alphabetic character. If the count of the character is 0, then it is not an alphabetic character.
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. 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: str) -> str
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
import test
def to_pig_latin(text: str) -> str:
# Put yourcode here
def main() -> None:
"scram"), "am'scray")
test.equal(to_pig_latin(# Put more test cases here return None
return None
main()
Hint
Before converting the input text to Pig Latin, the function must find the first vowel index. Finding the first vowel index would be easier if there was a function that returned whether a single character is a vowel. So write the function
is_vowel(character: str) -> bool
. 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 first vowel index 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: str) -> int
that returns the index of first vowel in the specified text.With the above two functions, writing the translation function is much more straightforward. If the first vowel index is 0, then apply the first Pig Latin rule using concatenation. If the first vowel index is not 0, then apply the second Pig Latin rule using a substring and concatenation.
Challenge
Ofyay ourse'cay, ityay akes'may ittle'lay ense'say o'tay ite'wray ayay ogram'pray o'tay anslate'tray ayay ord'way o'tay ig'pay atin'lay ifyay ityay annot'cay anslate'tray ack'bay e'thay otheryay ay'way! Ite'wray ayay unction'fay om_pig_latin'fray
at'thay akes'tay ayay ord'way alreadyyay inyay ig'pay atin'lay, andyay eturns'ray e'thay Englishyay equivalentyay.