A document from MCS 260 Fall 2021, instructor David Dumas. You can also get the notebook file.

MCS 260 Fall 2021 Homework 12 Solutions

  • Course instructor: David Dumas
  • Solutions prepared by: Johnny Joyce

Instructions:

  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)

Deadline

This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, November 16, 2021.

Topic

This homework assignment focuses on regular expressions, software licensing, and testing with pytest.

Collaboration

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

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.)

Point distribution

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

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

( 1. There's no problem 1 )

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

2. Mentions of 7400-series ICs

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

  • Some capital letters (possibly 0 of them)
  • The digits 74
  • Some more capital letters (possibly 0 of them)
  • Two or three digits (e.g. 00, 01, 04, 26, 138, 193)
  • Some more capital letters (possibly 0 of them)

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.

Restricted methods note

  • You must use regular expressions to do the searching/matching
  • You can only use Python syntax and techniques we've covered in MCS 260

Example

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

Solution

In [5]:
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()
74LS107
SN74LS72N
7404
74HC00

3. Test dice

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:

  • Check that rolling a 12-sided die returns an integer between 1 and 12
  • Check that rolling 10 dice returns a list of 10 integers, all between 1 and 6
  • Check that the return value of the coin flip function is either "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.

Restricted methods note

  • The only module you are allowed to import in test_dice.py is dice
  • You can only use Python syntax and techniques we've covered in MCS 260
In [ ]:
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"]

Revision history

  • 2021-11-18 Initial release of solutions