## -----------------------------------------------------------------------------------------------------------------------------¶

Approach to identify a sequence of one upper case letter followed by lower case letters

To check if the sequence of one upper case letter followed by lower case letters we use regular expression [A-Z]+[a-z]+$ Approach to find the patterns of 1(0+)1 in a given string A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0's. First compile a pattern which follows 1(0+)1 using re.compile(regex) method. Search a first sub-string in original string which follows 1(0+)1 pattern using pattern.search(string) method. substr = pattern.search(string) returns None if it doesn't find the given regex as sub-string in original string otherwise it returns first matched sub-string which follows 1(0+)1 pattern. substr.start() gives us starting index of matched regex and substr.end() gives us ending index of matched regex. Whenever we find regex as sub-string then increase count by 1 and again search for given regex starting from ending index of previous sub-string. The patterns are allowed to overlap. Approach to match a word consisting of z followed by one or more o's Compile a pattern which follows zo+\w*, that matches a word which contains 'z' followed by one or more o's. Then pass a string to the findall() method. This method returns the list of the matched strings. If the length of this list is equal to zero then it doesn't contain a matched string. \w -> represents any letter, numeric digit, or the underscore character. * -> means zero or more occurrence of the character. + -> means one or more occurrence of the character. ## -----------------------------------------------------------------------------------------------------------------------------¶ In [19]: import re # Function to Find all the patterns of "1(0+)1" in a given string def check_uc_lc_pattern(user_input): # regex pattern = re.compile("[A-Z]+[a-z]+$")

# searching pattern
if pattern.search(user_input):
print("String pattern match success \n")
else:
print("String fails the pattern \n")

def count_pattern(user_input):
# search regex '10+1' in original string search() function return first occurrence
# of regex '10+1' otherwise None '10+1' means sub-string starting and ending with 1
# and at least 1 or more zeros in between
count = 0
pattern = re.compile("10+1")
substr = pattern.search(user_input)

# search for regex in original string until we are done with complete string
while substr != None:
# if we find any occurrence then increase count by 1
count = count + 1

# find next occurrence just after previous sub-string for first occurrence of the pattern
user_input = user_input[(substr.end() - 1) :]
substr = pattern.search(user_input)
print(f"The number of times the pattern appears in the string is {count} \n")

def z_followed_by_o(user_input):
# Regex \w * zo+\w * will match text that contains 'z', followed by one or more 'o'
pattern = re.compile("zo+\w*")

# The findall() method returns all matching strings of the regex pattern
match_object = pattern.findall(user_input)

# If length of match_object is not equal to zero then it contains matched string
if len(match_object) != 0:
print("String pattern match success \n")
else:
print("No match \n")

while True:
print(
"1 --> Identify a word with a sequence of one upper case letter followed by lower case letters"
)
print("2 --> Find all the patterns of '1(0+)1' in a given string")
print("3 --> Match a word containing 'z' followed by one or more o's")
print("4 --> Exit the program")
choice = int(input("Enter a number to perform any of the operation: "))
print("\n")
if choice == 1:
user_input = input(
"Enter a string with a sequence of Upper and Lower case letters: "
)
print("\n")
check_uc_lc_pattern(user_input)
elif choice == 2:
user_input = input("Enter a string in the form of 1(0+)1 pattern: ")
print("\n")
count_pattern(user_input)
elif choice == 3:
user_input = input("Enter a string: ")
print("\n")
z_followed_by_o(user_input)
else:
break

# Main
if __name__ == "__main__":

1 --> Identify a word with a sequence of one upper case letter followed by lower case letters
2 --> Find all the patterns of '1(0+)1' in a given string
3 --> Match a word containing 'z' followed by one or more o's
4 --> Exit the program
Enter a number to perform any of the operation: 1

Enter a string with a sequence of Upper and Lower case letters: Arunachalpradesh

String pattern match success

1 --> Identify a word with a sequence of one upper case letter followed by lower case letters
2 --> Find all the patterns of '1(0+)1' in a given string
3 --> Match a word containing 'z' followed by one or more o's
4 --> Exit the program
Enter a number to perform any of the operation: 1

Enter a string with a sequence of Upper and Lower case letters: madhyaPradesh

String pattern match success

1 --> Identify a word with a sequence of one upper case letter followed by lower case letters
2 --> Find all the patterns of '1(0+)1' in a given string
3 --> Match a word containing 'z' followed by one or more o's
4 --> Exit the program
Enter a number to perform any of the operation: 1

Enter a string with a sequence of Upper and Lower case letters: MadhyaPradesh

String pattern match success

1 --> Identify a word with a sequence of one upper case letter followed by lower case letters
2 --> Find all the patterns of '1(0+)1' in a given string
3 --> Match a word containing 'z' followed by one or more o's
4 --> Exit the program
Enter a number to perform any of the operation: 1

Enter a string with a sequence of Upper and Lower case letters: andhrapradesh

String fails the pattern

1 --> Identify a word with a sequence of one upper case letter followed by lower case letters
2 --> Find all the patterns of '1(0+)1' in a given string
3 --> Match a word containing 'z' followed by one or more o's
4 --> Exit the program
Enter a number to perform any of the operation: 2

Enter a string in the form of 1(0+)1 pattern: 10101010001111

The number of times the pattern appears in the string is 4

1 --> Identify a word with a sequence of one upper case letter followed by lower case letters
2 --> Find all the patterns of '1(0+)1' in a given string
3 --> Match a word containing 'z' followed by one or more o's
4 --> Exit the program
Enter a number to perform any of the operation: 2

Enter a string in the form of 1(0+)1 pattern: 110

The number of times the pattern appears in the string is 0

1 --> Identify a word with a sequence of one upper case letter followed by lower case letters
2 --> Find all the patterns of '1(0+)1' in a given string
3 --> Match a word containing 'z' followed by one or more o's
4 --> Exit the program
Enter a number to perform any of the operation: 3

Enter a string: zoo

String pattern match success

1 --> Identify a word with a sequence of one upper case letter followed by lower case letters
2 --> Find all the patterns of '1(0+)1' in a given string
3 --> Match a word containing 'z' followed by one or more o's
4 --> Exit the program
Enter a number to perform any of the operation: 3

Enter a string: zip

No match

1 --> Identify a word with a sequence of one upper case letter followed by lower case letters
2 --> Find all the patterns of '1(0+)1' in a given string
3 --> Match a word containing 'z' followed by one or more o's
4 --> Exit the program
Enter a number to perform any of the operation: 4