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
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...
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)...
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...
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.
Write a program in python that corrects misspelled words. The input for the program is either...
Write a program in python that corrects misspelled words. The input for the program is either a string or a list of strings. The output should be a string or a list depending on the input and should have the spellings corrected. The speller should be able to correct at least the words listed below. You are free to use any data structures we have covered so far including lists and dictionaries. Your program should be in autocorrect.py. Here is...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
Write a program that determines whether an input string is a palindrome; that is, whether it...
Write a program that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. In Pseudocode please
Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT