Lab: Random Sequences
In this lab, you will write code to generate random character sequences.
Instructions
You should complete this lab individually.
Background
In class, we used the product rule to count the number of possible sequences:
The Generalized Product Rule: Let S be a set of sequences of length k. Suppose that there are n1 choices for the first element of a sequence, n2 for the second, …, and nk for the kth element. Then |S|=n1⋅n2⋅…⋅nk
Your code will use the product rule to count the number of possible sequences for a user-specified format.
Implementation
Write a Python program sequences.py
which accepts a user-specified sequence format. The sequence format is a string consisting of three special characters:
d
represents a digita
represents a lowercase alphabetic character (a-z)A
represents an uppercase alphabetic character (A-Z)
These special characters should be replaced by randomly chosen characters. All other characters should remain unchanged.
First, your program should calculate the total number of possible sequences. Next, your program should print three example sequences. For example:
> python sequences.py d
10 possibilities. For example:
2
2
8
> python sequences.py aaAAdd
45697600 possibilities. For example:
nkVF03
maYO52
acMZ35
> python sequences.py "(ddd) ddd-dddd"
10000000000 possibilities. For example:
(132) 000-3056
(852) 711-1016
(623) 187-7081
You may find these Python modules helpful:
Submit
Upload sequences.py
to Gradescope.
Learning Goals
- Count using the product rule
- Use randomness in Python