CPSC150A
Scientific Computing

Activity 23

String Accumulator

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 paramters start and end are non-negative integers. The function should return a copy of the characters from text between start and end. Note, it 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:
    test.equal(substring("hello", 1, 4), "ell")
    # 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 to get their hands on. This is simply because most Internet users choose incredibly weak passwords. The easiest way to get around this issue to to use a randomly generated password.

Details

Write the Python function generate_password(size: int) -> str that returns a string of random lower-case letters. It should be possible for the generated password to contain any letter. The parameter size size is a positive integer and is the number of characters the returned password string should contain.

Example

The code:

print(generate_password(5))
print(generate_password(10))

Might print:

crkkb
bgchwxqybn
  • 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 upper-case letters and digits. Alter your function so that it is garenteed to include at least 1 lower-case letter, 1 upper-case letter and 1 digit. Note, the location of the required characters should be still be completely random.

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

import test

def sanitize(text: str) -> str:
    # Put your code here

def main() -> None:
    test.equal(sanitize("Hello, World!"), "HelloWorld")
    # Put more test cases here
    return None

main()
  • 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 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: 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:
    test.equal(to_pig_latin("scram"), "am'scray")
    # Put more test cases here return None
    return None

main()
  • 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: str) -> bool. 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.
  • 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: str) -> int 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 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.