-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisLegalVar.py
More file actions
69 lines (64 loc) · 1.93 KB
/
isLegalVar.py
File metadata and controls
69 lines (64 loc) · 1.93 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#isLegalVar.py
#check the var in programming language is legal or not
#Legal Var has 3 property:
#1:Only consist of alphabet , digit or '_'.
#2:It contains at least 1 alphabet.
#3:Before the first alphabet occurs , digit can not be occur.
#illegal var:
#'123','a@1','a b' ,'','1c' or '___','_12b' is not accepted.
#legal var:
#a1b , _a , _a2 , a____,well_3
#way 1:
def isLegalVar(s):
if s=='':
return False
if s[0].isupper() or s[0].islower() or s[0]=='_':
idx=0
while s[idx]=='_':
idx+=1
if idx>=len(s):
return False
if s[idx].isupper() or s[idx].islower():
for i in range(idx,len(s)):
if(s[i].isalnum() or s[i]=='_'):
pass
else:
return False
return True
else:
return False
else:
return False
#driver code
print("way1:")
example_list=['123','ab1','___','a#$2','','1abc','a1_1','_ee','__ee','__1e','__e1']
for l in example_list:
print(l,"is",isLegalVar(l))
#way 2:
def isLegalVar(s):
s1=s.replace('_','')
if s1=='':
return False
elif s1[0].isdigit()==True:
return False
else:
for i in range(1,len(s1)):
if(s1[i].isalpha()==False and s1[i].isdigit()==False):
return False
return True
#driver code
print("way2:")
example_list=['123','ab1','___','a#$2','','1abc','a1_1','_ee','__ee','__1e','__e1']
for l in example_list:
print(l,"is",isLegalVar(l))
#way 3:
def isLegalVar(s):
s1=(s.replace('_',''))
if s1=='' or s1[0].isdigit():
return False
return [x for x in list(s1) if x.isdigit()==True or x.isalpha()==True]==list(s1)
#driver code
print("way3:")
example_list=['123','ab1','___','a#$2','','1abc','a1_1','_ee','__ee','__1e','__e1']
for l in example_list:
print(l,"is",isLegalVar(l))