NOTE: THIS LAB CANNOT BE TURNED IN LATE. It must be turned in by Sunday night, Dec 10 so we can grade it.
There is code to snarf for lab 11, that code is also here.
NOTE: This file RegexDemo.py is slightly different than the one you snarfed for lecture.
To get credit for this lab, you will need to do the following by Sunday night.
First discuss the following mystery function (where lines are numbered) and calls to it. How many recursive calls are there for each call (the original call is not a recursive call)? What is the return value in each case?
1 def mystery(x, y): 2 if x <= y: 3 return 1 4 return 2 + mystery(x-1, y+1) print mystery(5,5) print mystery(7,4) print mystery(10,3)
Answer these questions on the online form.
Now you will rework one older APT by rewriting it to solve it with simpler recursion - Acronym
If you have already done it, you could comment out the code you have and put in new code that uses recursion.
Just use the Test link to test it out since we will not submit this as an APT.
Answer these questions:
Consider the problem of extracting all the numbers out of a list to create a list of numbers, where the original list might have lists inside it. This problem is similar to the problem of listing all the big files in a folder we looked at in lecture.
For example, consider the following list:
[5, [6, 7], 8, [[1], [2, [6]], 7]]
You will write a recursive function named getNums that has one parameter that is a list like this one (that might have lists inside of it). This function returns returns a simple list of the elements from this list in their same order. For example, for the list of lists above, getNums would return the list:
[5, 6, 7, 8, 1, 2, 6, 7]
This code has been started for you in the file GettingAllNumbers.py
Answer these questions:
getNums
using recursion and copy your final code here.
Snarf the code for this part of the lab.
In creating regular expressions you'll need to understand these aspects of regular expressions. You can see more about regular expressions in Python via Python documentation, e.g., https://docs.python.org/2/library/re.html
Answer each question on the online form. You can run module RegexDemo.py which accepts regex expressions and prints those words that match the expression. It reads data from one of two files, one that has a lot of words, with one word per line, and one that is a book.
For example using the word file, entering the regex
^\w\w\wz$
matches 12 words as shown below. There have to be four characters, the first three are any letter and the fourth letter must be z.
benz buzz cruz fuzz jazz katz lutz quiz ritz salz suez whiz
Whereas entering
\w\w\wz$
matches 65 words, the last four of which are shown here. In this case there are restrictions on the last four characters but none at the beginning of the string. There must be at least four characters and the last character must be z.
vasquez velasquez waltz whiz
You'll use expressions provided to you and develop expressions to answer questions about understanding regular expressions.
regex | purpose |
---|---|
. | any character |
* | zero or more of previous regex |
\w | any alphanumeric character (and _) |
+ | one or more of previous regex |
\s | any whitespace character |
*? or +? | non-greedy version of either * or + |
\d | any digit character |
() | tag/group a regular expression |
[] | character class, e.g., [A-Z] or [aeiou] |
\1, \2, .. | match numbered tagged/grouped regex |
{n} | n occurrences of preceding regex |
^ | beginning of line/string |
[^...] | not the characters in the class, e.g., [^aeiou] |
$ | end of line/string |
Part 2a) Answer these questions on the online form.
For this part, use the words.txt file. When you run RegexDemo, select "w".
Answer: there are 898 words that end with 'a', the regex used is a$ and the last word printed is zomba
^(\w)(\w)\w\2\1$shows there are 9 five-letter palindromes. How many seven letter palindromes are there and what are they?
amalgamate amalgamated amalgamates amalgamatating amalgamatation assesses dereference monotonous monotonously monotonousness possesses sensitivities
Part 2b)
For this part, use the book file, littlebrother.txt. When you run RegexDemo, select "b".
After submitting the lab form, then submit the code you wrote with ambient/websubmit. Use lab11 as the submission folder.
This is the last lab of the semester. Celebrate!