logo CodeStepByStep logo

valid_passwords2

Write a function named valid_passwords2 (similar to the previous exercise valid_passwords) that examines a list of passwords and password policies and returns how many of the passwords are acceptable by those policies. Your function accepts a string parameter representing a file name as a parameter. Each line of the file contains a policy followed by a password. A policy is of the form INDEX1-INDEX2 LETTER indicating that the given letter must occur at exactly one of the two given 1-based indexes in the password. For example, if the file named passwords.txt contains the following values:

1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc

The first line, abcde is valid: position 1 contains a and position 3 does not. The second line, cdefg is invalid: neither position 1 nor position 3 contains b. The third line, ccccccccc is invalid: both position 2 and position 9 contain c. So the call of valid_passwords2("passwords.txt") should return 1. Note again that the indexes given are 1-based, not 0-based.

You may assume that the file exists and is readable and that it follows the format described above.

Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.