forked from devAmoghS/Python-Interview-Problems-for-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusername_validation.py
38 lines (27 loc) · 1.12 KB
/
username_validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Have the function UsernameValidation(`str`) take the `str` parameter being passed and determine if the string is a valid username according to the following rules:
# 1. The username is between 4 and 25 characters.
# 2. It must start with a letter.
# 3. It can only contain letters, numbers, and the underscore character.
# 4. It cannot end with an underscore character.
#If the username is valid then your program should return the string `true`, otherwise return the string `false`.
def UsernameValidation(strParam):
# username is between 4 and 25 characters
if len(strParam) < 4 or len(strParam) > 25 :
return False
# start with a letter
if not str(strParam[0]).isalpha():
return False;
# can't end with an underscore
if str(strParam[-1] ) == '_':
return False;
# contains only letters, numbers and underscore
valid_grammar = set('abcdefghijklmnopqrstuvwxyz0123456789_')
for ch in strParam:
if ch.lower() not in valid_grammar:
return False;
return True
# keep this function call here
TC1 = "aa_"
TC2 = "uaa__hello_worldW"
print(TC1, UsernameValidation(TC1))
print(TC2, UsernameValidation(TC2))