Code Coach Solutions
Password Validation
Difficulty: Hard
Problem Statement:
You're interviewing to join a security team. They want to see you build a password evaluator for your technical interview to validate the input.
Task:
Write a program that takes in a string as input and evaluates it as a valid password. The password is valid if it has at a minimum 2 numbers, 2 of the following special characters ('!', '@', '#', '$', '%', '&', '*'), and a length of at least 7 characters.
If the password passes the check, output 'Strong', else output 'Weak'.
Input Format:
A string representing the password to evaluate.
Output Format:
A string that says 'Strong' if the input meets the requirements, or 'Weak', if not.
Sample Input:
Hello@$World19
Sample Output:
Strong
Explanation:
The password has 2 numbers, 2 of the defined special characters, and its length is 14, making it valid.
Solution:
s=input()
a,n,c=0,0,0
for i in s:
if i.isalpha():
a+=1
elif i.isnumeric():
n+=1
else:
c+=1
if n>=2 and c>=2 and a+n+c>=7:
print("Strong")
else:
print("Weak")
Explanation:
Apply loop whether there exist at least 2 numbers(n), 2 special symbols(c), and at least 3 alphabets(a). If the above three conditions are satisfied print strong otherwise weak. isaplha() method is used to check whether the character is an alphabet or not. isnumeric() method is used to check whether the character is a number or not. Make sure the variables are initialized with zero.
Result:
Hope You understand the solution. The more you practice more you Learn. If there are any queries, let us in the comment section.
Tips to solve a problem in less time :
1. Don't read the whole problem statement initially. First, go through the input and output.
2. Mostly you will understand the problem while reading input, output, and Output explanation.
3. Now, Divide your Logic into 3 steps:
step1: Input & output
check whether you have written the correct syntax for input. To check you have to print the input.
step2: Build your own logic with proper indentation.
step3: Print the expected output.
Thank you!


0 Comments