.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, November 16, 2021.
This homework assignment focuses on regular expressions, software licensing, and testing with pytest.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials and other resources you are allowed to refer to for this homework are:
(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)
This homework assignment has 2 problems, numbered 2 and 3. Thus the grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
Ask your instructor or TA a question by email, in office hours, or on discord.
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
In the 1970s and 1980s, one of the most popular series of components in digitial electronic circuits were the 7400 series of integrated circuits. They were used extensively in early personal computers, for example. They are still used in some electronics applications. Here's a picture of a few of them:
There are many parts in this series, with numbers like 7404
and SN74LS02N
and 74F08
.
More precisely, the format of the part number is
Write a program hwk12prob2.py
that takes a filename as its command line argument. It should read the lines of the specified text file and look for names of 7400-series ICs matching the specs above. It should print each name it finds.
If test.txt
contains
I was thinking about the clock module we discussed, and I think by adding a couple of JK-type
flip-flops (maybe 74LS107 or SN74LS72N? not sure...) we could convert the momentary pushbutton
input to a toggle, without using the more mechanically complicated sustained switch your BOM
originally called for. But I agree we should keep using dedicated inverters on a 7404 rather
than repurposing an unused NAND from the 74HC00, since it will be a pain to interface the TTL
and CMOS parts to make that work.
then the output of
python3 hwk12prob2.py test.txt
would be
74LS107
SN74LS72N
7404
74HC00
import sys
import re
# Regular expression to match the format of part numbers
chip_regex = r"[A-Z]*74[A-Z]*\d\d(\d)?[A-Z]*"
filename = sys.argv[1]
infile = open(filename,"r",encoding="UTF-8")
for line in infile:
# Use the regular expression we defined at the start of the code.
for match in re.finditer(chip_regex,line):
print(match.group())
infile.close()
Create test_dice.py
, a module containing pytest-compatible functions to test the module dice.py we created in lecture 23.
The tests should do the following:
"heads"
or "tails"
For full credit, your module must have three passing tests that can be discovered and run by pytest
if test_dice.py
and dice.py
are in the same directory. Also, if test_dice.py
is run as a script, it must do nothing at all.
test_dice.py
is dice
import dice
def test_twelve_sided_die():
'''Roll a 12-sided die and test whether result is as expected'''
result = dice.roll_die(sides=12)
assert result in [1,2,3,4,5,6,7,8,9,10,11,12]
def test_roll_ten_dice():
'''Roll ten 6-sided dice and test whether results are as expected'''
result_list = dice.roll_dice(10)
assert len(result_list) == 10
for result in result_list:
assert result in [1,2,3,4,5,6]
# Alternative approach instead of the for loop:
# assert all([result in [1,2,3,4,5,6] for result in result_list])
def test_flip_coin():
'''Flip a coin and test whether the result is either one of "heads" or "tails"'''
result = dice.flip_coin()
assert result == "heads" or result == "tails"
# Alternative approach:
# assert result in ["heads","tails"]