This lab was designed to be completed during lab-time for cs250. This lab is not graded for correctness. However, you should work through all of the problems, as it will help you on assignments and exams. If you cannot complete the lab in the alloted time, finish at home.
This particular lab was designed to be completed on the Mac lab machines in GS 930
Below is a “skeleton” of a program that uses regular expressions to search for matches in a string.
I say “skeleton” because a few key components are missing (labeled with
import re text = ??? regex_str = ??? pattern = re.compile(regex_str) matches = pattern.findall(sentence) print('All matches listed below:') for m in matches: print(' ' + m)
(You’ll use this skeleton in a second…)
Develop a regular expression that matches either word “I” or “it” followed by any word, followed by “you”.
You can use regexr to help you figure out the right regex.
Once you’ve figured it out, write a python script named
problem1.py, put the skeleton code into it, and put the regular expression in the right place.
Now, try running the program several times, with
text set to each of the below strings:
''' You blame me, and I blame you. But would you like it if I treated you to a free lunch? '''
''' I don't really like you, I like you, I love you. But He cannot stand you because He stopped you. '''
''' I would like to eat a meal with you! You would like to eat dinner with I? I cannot afford to eat a meal with you :( '''
Write a python program named
This program should repeatedly read lines of input from the user on the command line in a while loop.
Each time a user types in a line, your program should do the following:
Here’s an example of what running this should look like:
$ python3 sum-the-nums.py Write some lines, and I'll sum the 5-digit numbers! 3 5 8 11 55 832 The sum of 5-digit numbers is: 0 1234 123 12 1 The sum of 5-digit numbers is: 0 12345 10000 1 1 1 The sum of 5-digit numbers is: 22345 10000 20000 30000 45 40000 The sum of 5-digit numbers is: 100000 10002 10002 13 The sum of 5-digit numbers is: 20004 ...
Write a program named
This program will do the following:
Below are a few examples of running this program with
bodybild.txt being used as input.
bodybild.txt is available for download here.
$ python3 word-lengths.py Enter a file to search through: ./bodybild.txt Enter a word length to look for 3 There are 1232 words of length 3 $ $
$ python3 word-lengths.py Enter a file to search through: ./bodybild.txt Enter a word length to look for 9 There are 94 words of length 9
$ python3 word-lengths.py Enter a file to search through: ./bodybild.txt Enter a word length to look for 14 There are 2 words of length 14
$ python3 word-lengths.py Enter a file to search through: ./bodybild.txt Enter a word length to look for 17 There are 0 words of length 17 $
Write a program named
format-numbers.py that will read in a text file, similar to how
Once read in, find all phone numbers of the form
X-XXX-XXX-XXXX in the contents of the file (where the
Xs are digits, obviously).
Remove the dashes from all of the phone numbers, and then print out each phone number found, one per line.
You should use regular expressions to find the numbers.
Once you think you have your program working, try it out with these downloadable text files: