Question

In: Computer Science

Python Write a program that will analyse the string input and print “accept” or “reject” based...

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

Solutions

Expert Solution

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()


Related Solutions

Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
The assignment is to build a program in Python that can take a string as input...
The assignment is to build a program in Python that can take a string as input and produce a “frequency list” of all of the wordsin the string (see the definition of a word below.)  For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words.  In the output, each line contains of a...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
In Python Write a function to read a Sudoku board from an input string. The input...
In Python Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5 385...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Python programmingWrite a program whose input is a string which contains acharacter and a...
Python programmingWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT