CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 21 - Strings and Objects


As usual, create two directories for today's class. Create a directory called lecture21 under activities, and a directory called lab21 under labs.


Strings

You have been using Strings in your program since your first program. You have mostly been using them to label your print statements. Strings have a lot of built in functionality that we havve ignored up to this point. Today, we will explore this additional functionality, to see how we can build more interesting programs.


In-Class Activity 1

Is Alphabetic

is_alpha.py in your lecture21 directory. Create a function is_alphabetic that takes as a parameter a single character, and returns True if and only if the character parameter is an alphabetic character (a - z or A - Z). It should return false otherwise. Use the built-in ord function to determine if a character is alphabetic. Do not use any string methods.

Don't forget to define your test cases. Your program should execute your test cases automatically.

Challenge If you look at an ASCII table, you can see that you can write the code for is_alpha using one boolean statement. This same process can be used to determine if a character is numeric. Write a function is_numeric that returns True if and only if the character parameter is a numeric digit. Using this function, and the function you wrote above, write a function is_alphanum that returns True if and only if the character is an alphabetic character or a numeric digit.

In-Class Activity 2

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 the system somewhere.

For our purposes, we will consider any non-alphabetic character to be "offending." Write a function strip_non_alpha in a file called input_sanitation.py that takes as a parameter a string, and returns a copy of the input string with all non-alphabetic characters removed.

Challenge In the real world, the true offending characters are double and single quotes. Write a new function escape_strings 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 \').

Lab Assignment 21

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. Today, you will write a program that can generate passwords for individuals.

Create a file called password_generation.py in your lab21 directory. Your program should take as input from the user the length of the password they want generated. Your program should then print a random string of lower case characters of the specified length.

Recall that you can use the random module to generate random numbers. Use the ord and chr functions generate random numbers in the ASCII range of the lowercase characters, and convert them into a string.

Make sure you test your program well. How many test cases do you need? Include your test cases in your program file. Comment them appropriately.

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 program that it sometimes will include uppercase letters in the output as well. Make sure the parameter used to determine how often an uppercase character is used is well documented, and easily altered.

Challenge

An even better program will occasionally throw in a special, non-alphabetic character. Alter your program so that it will occasionally output one of the following characters: !#$%()*+-.=_

Make sure you can efficiently chose from one of these special characters. Having 12 conditional statements will not earn you credit for this challenge.


Submission

When you have finished, create a tar file of your lab21 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czvf lab21.tgz lab21/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 21. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your files.

Do not forget to email your partner today's files!


Last modified: Fri Oct 11 13:31:51 EDT 2013