COMP 1012 Assignment

Question 1—Generate and Count Passwords [10 marks]


Using arrays, write a program that generates all possible 5 letter passwords containing only lowercase letters, uppercase letters, and/or digits. For example, “rot13” would be such a password, but “32RAx” would not because it has a hyphen. There are 62 letters to use (26 + 26 + 10). There are 625 = 916132832 (almost a billion) possible passwords.

Test each password you generate to determine if it satisfies the following rules:

  • At least one lowercase letter;
  • At least one uppercase letter;
  • At least one digit;
  • No character can appear twice in a row (e.g. “Haax5” is not valid, but “Ha5ax” is).


A list comprehension can have nested for loops. For example, the following list comprehension generates all possible passwords with letters that come from the string LETTERS. It has been formatted to emphasize the similarity to nested for loops, but notice there are no colons.

passwords(=[ "".join([c0,(c1,(c2,(c3,(c4])
for c0 in LETTERS
for c1 in LETTERS
for c2 in LETTERS
for c3 in LETTERS
for c4 in LETTERS]

If LETTERS is the string “0Aa”, the 243-entry result is:


If we wanted the locations of each letter in LETTERS instead, we could write

passwords = [c0,c1, c2, c3, c4)
for c0 in range (len LETTERS))
for c1 in range (len LETTERS))
for c2 in range (len LETTERS))
for c3 in range (len LETTERS))
for c4 in range (len LETTERS))

which yields

{`[(0, 0, 0, 0, 0), (0, 0, 0, 0, 1), (0, 0, 0, 0, 2), (0, 0, 0, 1, 0), (0, 0, 0, 1, 1), (0, 0, 0, 1, 2), (0, 0, 0, 2, 0), (0, 0, 0, 2, 1), (0, 0, 0, 2, 2), (0, 2, 0, 0, 0),
(2, 2, 2, 0, 1), (2, 2, 2, 0, 2), (2, 2, 2, 1, 0), (2, 2, 2, 1, 1), (2, 2, 2, 1, 2), (2, 2, 2, 2, 0), (2, 2, 2, 2, 1), (2, 2, 2, 2, 2), 

This code is a good example of the flexibility of Python, but it is not good for tackling really big problems, because the storage scheme wastes space, and the interpreted code is relatively slow (although this code is faster than most because it is so short). If we generate the last list of tuples as a 2Rdimensional numpy array, however, we can shrink the storage, and increase the speed of operations on this data structure by an order of magnitude, so we can tackle larger problems.1

COMP 1012 Assignment Image 2

In fact, since all the numbers in this data structure are small, we can pick a data type that reduces the size even more. We will be considering LETTERS containing all digits, all lowercase letters and all uppercase letters, with a total of 10 + 26 + 26 = 62 characters, so the numpy.int8 data type, which is an 8Rbit (1 byte) integer representing numbers from R128 to 127, will work fine and save us lots of space. Storing one million of these numbers requires only 1 MB of memory, in comparison with the 8 MB of memory needed to store the same numbers using the default 64Rbit ints.

The list of tuples shown above looks like the text box to the right as an array. To generate this array, first make an array of the right size and type with np.zeros((numRows, numCols), dtype=np.int8). Then fill each column. Each column consists of the digits from 0 to nR1 in order, where n is the length of LETTERS, in this case 3. Each number is repeated 1 time in the last column, n times in the second last column, n2 times in the third last column, etc. Then the whole sequence from 0 to nR1 is repeated as many times as necessary to fill the column length of n5. You can use np.arange to generate the sequence of numbers, np.repeat to create repetitions of each entry, and np.resize to cause an entire array to repeat as many times as necessary to fill a designated size.

Once you have an array, do all your operations on the entire array, or on entire columns of the array. For example, to find if there are entries in the 0 column that are equal to entries in the1 column, use array[:,0] == array[:,1], which yields a boolean array of the same length as a column. What would np.sum[:,0] == array[:,1]) represent?

What to do

Write a complete script file that prompts the user to specify how many letters of the alphabet, from 1 to 26, and how many digits, from 1 to 10, are to be used. It should then generate all 5Rcharacter passwords using just those letters (both upper and lower case) and digits. It should test each candidate against the rules stated in the Description section, and count the number of passwords that pass the test.


COMP 1012 Assignment Image 1 COMP 1012 Assignment Image 3

Sample Output

COMP 1012 Assignment Image 4 COMP 1012 Assignment Image 5