CPSC120A
Fundamentals of Computer Science I

Lab 16

Strings

Use the command line to create a new directory called lab16 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

String Strip

One of the biggest security vulnerabilities that web developers are constantly fighting is known as an 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 a function called strip_non_alpha(offending_string) in a file called input_sanitation.py. This function takes a string as a parameter, and returns a copy of the input string with all non-alphabetic characters removed.

Make sure you test your program well. How many test cases do you need? Make sure you follow all of the course's coding conventions.

Sample Test Cases

Note: Quotation marks included in output for clairly only. Your program should not output the quotation marks.

Function Parameters Expected Output
"Hello, World" "HelloWorld"
"Scotty " "Scotty"
"!@#$" ""

Hint

  • You are going to need to use the accumulator pattern for this activity. This is because Strings are immutable datatypes, so you need to make a copy of the input string. The accumulator for strings simply appends characters to the end of the string using the + operator.

  • Recall that everything in the computer can be represented as an integer. The way that characters are represented as a number follows the ASCII standard. You can get the ASCII value of a character by using the ord function.

  • An alphabetic characters ASCII value is between the ASCII values for 'a' and 'z' or 'A' and 'Z'.

Challenge

In the real world, the true offending characters are double and single quotes. Write a new function called escape_strings(offending_string) that takes as input a single string, and returns a copy of the string 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 \').

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 a function called generate_password(size), which takes a positive integers as a parameter. Your program should return a string of lower case letters of the specified size. This function should randomly choose the lower case letters in the returned string.

Your program should ask the user for a password length, and print to the terminal the password you generate.

Make sure you test your program well. How many test cases do you need? Make sure you follow all of the course's coding conventions.

Example Execution

$ python3 password_generation.py
How long do you want your password? 5
neolp
$ python3 password_generation.py
How long do you want your password? 10
wigmnhgqwn

Hint

  • You can use the random module to generate random numbers. Since we know we are generating lower-case characters, you need to generate a random number in the range of the ASCII value for 'a' to the ASCII value for 'z'.

  • Use the chr function to convert an integer for an ASCII character into the desired character.

  • Since you know how many characters you want, you should use a for loop for this activity. You are also using the accumulator pattern again, like in the previous activity.

Challenge

Even random strings of lowercase characters are pretty easy to break. A better program would randomly decide to include some uppercase letters as well. Alter your function so that it will sometimes include uppercase letters in the generated passwords. Make sure the parameter used to determine how often an uppercase character is used is well documented and easily altered.

Submission

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