2024 April 1,

CS 254: Day 04

Carleton College, Joshua R. Davis

Today's homework is entirely Python programming. You solve three problems about DFAs and NFAs and three problems about regular expressions.

DFAs and NFAs

Download the file dfaNFA.py. Read it in detail. It consists of four sections: Sets, DFAs, NFAs, and Main. There are three methods with broken implementations.

A. Implement dfa.accepts. Once you are done, run the program. The first two tests in main should work.

B. Implement dfa.intersection. Then the third test in main should work.

C. Implement nfa.dfa. Then the fourth and fifth tests should work.

Regular Expressions

Read the Python regular expression tutorial on our course web site. Also download regExp.py. It contains three broken pieces.

E. Define the regular expression miscapitalized based on the specification just before it. Then the first test in main should work.

F. Define the regular expression email based on the specification. (For clarity, my solution defines sub-expressions for the local part and the hostname.) Then the second and third tests should work.

G. Implement the function fixDates based on the specification. Then the fourth test should work.

Submit Your Work Electronically

In both files, feel free to add your own tests, if they help you debug. Like the tests already in main, they should run if a user runs the file, but they should not run if a user imports the file.

When you are finished, please hand in your two Python files through the Day 04 Homework link on our Moodle site.

Extra Practice

Here is an optional problem. You are not expected to hand it in, and it does not earn any extra credit, but it might help you become a better programmer. It's about HTML tables, such as this example:

AaronBabatopeCalvin
AbbyBetsyCathy
AishaBeyonceChinua
AloysiusBobChristina
ArikoBridgetChuck

Inspect the HTML source code for this page, to see how such a table is constructed. When editing HTML, moving table rows is not difficult; you can just cut and paste. Moving table columns is harder. For example, switching the first two columns of the table above requires an edit to each row, which is tedious and error-prone. So, when I need to move table columns in real life, I use regular expressions.

So here's your problem: Using a regular expression and re.sub, write Python code to swap the first and second cells in every table row in a given HTML string. For example, when run on the HTML for this web page, your Python code should swap the A and B columns of the table above. For simplicity, assume that the cells contain only alphanumeric characters. There may be zero or more whitespace characters outside the table cells inside the table rows.