In: Computer Science
def check(s): #Conditions applied 1,5,6 if len(s)=9 and s[0].isupper() and s[-1].isdigit(): upper_count = sum(1 for c in s if c.isupper()) lower_count = sum(1 for c in s if c.islower()) number_count = sum(1 for c in s if c.isdigit()) #Conditions 2,3,4 if upper_count=3 and lower_count==3 and number_count==3: #Condition 7 { Two consecutive alphabets can’t be small } for i in range(1,len(s)): if s[i-1].islower() and s[i].islower() : return 'reject' else: return 'reject' #All conditions satisfies here, so accept return 'accept' else: return 'reject' ### TEST CASES ### print(check('ABiG1h2f3')) #ALL correct # BELOW ALL SHOULD BE REJECTED print(check('AbiG1h2f3')) # bi consecutive two small letters print(check('aBiG1h2f3')) # Starts with small letter print(check('ABiG1h2fe')) # doesn't end with number print(check('ABiG1h')) #Length is not 9 print(check('ABiG1h334')) # has 4 numbers. Also only 2 small letters
i need a definition of this python code?
Definition of each part of the provide code is explained in comments. Please go through and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Also fixed some small bugs (i.e. found two usages of = instead of == for value comparisons, fixed it)
'''
The below method accepts a string and returns either 'accept' or
'reject' based on the
value of the string. The method returns 'accept' only if the
following conditions are met
> The length of string must be 9
> There should be three upper case, 3 lower case and 3 digits
characters
> There should not be any consecutive small letters like 'ac' or
'xp' etc
> First character must be a capital letter
> Last character must be a digit
The method returns 'reject' if any of these case(s) is/are
violated.
'''
def check(s):
# checking if length is 9 and first
character is upper case and last
#character is a digit
if len(s)==9
and s[0].isupper() and
s[-1].isdigit():
#counting the number
of upper, lower and numeric characters
#(1 for c in s if
c.isupper()) will return a set/list of 1's where
#number of 1s equal to
the number of upper case characters, sum() will
#sum all these 1s, which
will return the total number of upper case chars
upper_count = sum(1
for c in s if
c.isupper())
lower_count = sum(1
for c in s if
c.islower())
number_count = sum(1
for c in s if
c.isdigit())
# checking if number
of upper, lower, numeric characters are exactly 3 each
if
upper_count==3 and lower_count == 3
and number_count == 3:
# checking if there exists a two consecutive occurrences of
small letters
for i in range(1, len(s)):
if s[i - 1].islower() and
s[i].islower():
#characters at i-1 and i are lower case
return 'reject'
else:
#upper or lower or numeric count not equals 3
return 'reject'
# All
conditions satisfies here, so accept
return 'accept'
else:
#length is not 9 or
first character is not upper case or last character
#is not numeric
return 'reject'
### TEST CASES ###
print(check('ABiG1h2f3')) # ALL correct
# BELOW ALL SHOULD BE REJECTED
print(check('AbiG1h2f3')) # bi consecutive two
small letters
print(check('aBiG1h2f3')) # Starts with small
letter
print(check('ABiG1h2fe')) # doesn't end with
number
print(check('ABiG1h')) # Length is not
9
print(check('ABiG1h334')) # has 4 numbers.
Also only 2 small letters
#OUTPUT
accept
reject
reject
reject
reject
reject