Uploaded by Coco M

COMP1117-1A-2019-Quiz2-question

advertisement
2019 Fall COMP1117A Quiz 2
1. (30%)
Write a function insert_in_middle(val, lst), which returns a list obtained by inserting value val
in the middle of list lst.
Example run
Expected output
[11, 17, 0, 20, 19]
[10, 7, 8, 9]
2. (30%)
An Armstrong number is a number that is the sum of its own digits each raised to the power of
the number of digits.
For example:
 9 is an Armstrong number, because 9^1 = 9
 10 is not an Armstrong number, because 1^2 + 0^2 = 1 != 10
 153 is an Armstrong number, because: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
 154 is not an Armstrong number, because: 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190 != 154
Write a function is_armstrong(n), which takes an integer argument n, returns True if n is an
Armstrong number, and False otherwise.
Example run
Expected output
print(is_armstrong(9))
True
print(is_armstrong(10))
False
print(is_armstrong(153))
True
print(is_armstrong(154))
False
P. 1 of 2
3. (40%)
Write a program to ask user to input a password and check if the password meet the following
criteria. The program should output the corresponding error message if a criterion is not met and
output “Good password.” if the password meets all criteria.
1.
2.
3.
4.
5.
6.
Criteria
At least 1 lower case letter [a-z].
At least 1 upper case letter [A-Z].
At least 1 digit [0-9].
At least 1 symbol from [$#@].
At least 6 characters long.
Error message
"At least 1 lower case letter [a-z] is needed."
"At least 1 upper case letter [A-Z] is needed."
"At least 1 digit [0-9] is needed."
"At least 1 symbol from [$#@] is needed."
"Password too short. At least 6 characters are
needed."
No more than 12 characters long. "Password too long. No more than 12 characters."
Example input
Expected output
ABC
At least 1 lower case letter [a-z] is needed.
At least 1 digit [0-9] is needed.
At least 1 symbol from [$#@] is needed.
Password too short. At least 6 characters are needed.
abc123@
At least 1 upper case letter [A-Z] is needed.
abcABC012345$
Password too long. No more than 12 characters.
a1B2#34
Good password.
P. 2 of 2
Download