In: Computer Science
Python
Write a program that will analyse the string input and print “accept” or “reject” based on the pattern given
Accept if it fulfils the following conditions
-String length 9
-3 small alphabet (3 lowercase letter)
-3 digits
-3 big alphabet (3 uppercase letters)
-1st alphabet should be a capital
-Last alphabet should be a number
-Two consecutive alphabets can't be small
Reject if any of the conditions is absent
So i want it to accept or reject my input, so if i input ABiG1h2f3 it will say accept and if I put something like ABiG1h it will say reject (because length is not 9)
Could you also explain each line of code, thank you
python code:
def string_check(stringpattern):
#setting initial value of val as True to change it later in
code
val = True
#below IF condition checks if String length is < 9
#if it is less then 9, false is returned
if len(stringpattern) < 9:
#print('length should be at least 9')
val = False
return val
#assigns all the lowercaseletters in stringpattern to varialble
lowercase_letters
lowercase_letters = [c for c in stringpattern if c.islower()]
#below IF condition to checks if variable lowercase_letters < 3
and retuns false
if len(lowercase_letters) < 3:
#print('String should have at least 3 lowercase letters')
val = False
return val
#assigns all the numbers in stringpattern to varialble digits
digits = [c for c in stringpattern if c.isdigit()]
#below IF condition to check if variable digits < 3 and retuns
false
if len(digits) < 3:
#print('String should have at least 3 digits')
val = False
return val
#assigns all the Uppercaseletters in stringpattern to varialble
Uppercase_letters
Uppercase_letters = [c for c in stringpattern if c.isupper()]
#below IF condition to check if variable Uppercase_letters < 3
and retuns false
if len(Uppercase_letters) < 3:
#print('String should have at least 3 uppercase letters')
val = False
return val
#condition checks if firstletter i.e.,[0] of stringpattern is
uppercase or not
first_letter = stringpattern[0].isupper()
if first_letter == False:
#print('First character should be Uppercase')
val = False
return val
#condition checks if lastletter i.e.,[-1] of stringpattern is digit
or not
Last_letter = stringpattern[-1].isdigit()
if Last_letter == False:
#print('Last character should be digit')
val = False
return val
#Traverseing through the characters in the string
for i in range(0, len(stringpattern)) :
# If the current and the previous
# characters are in the lowercase
# then sets val as false
if (stringpattern[i].islower() and stringpattern[i + 1].islower())
:
#print(stringpattern[i])
#print(stringpattern[i+1])
val = False
return val
return val
#Main method to accept string named stringpattern and pass to
function named string_check
#string_check function returns value either True or False
#If true is returned by string_check then accept is printed
#If false is returned by string_check then reject is printed
def main():
stringpattern = 'GiTrNStRiNg123'
if (string_check(stringpattern)):
print("accept")
else:
print("reject")
# Driver Code
if __name__ == "__main__" :
main()