Having a little trouble with this assignment.
"Please add comments within Script, for the guidance of the code and what each function is doing, so i can understand more."
Project 4 Draft: Pattern Search PYTHON
Introduction
Your task for this
project is to use regular expressions to discover and modify
information from the provided test.
The project is broken into two sections:
Determine the number of non alpha numeric characters in the lorem_ipsum string.
Replace all instances of the words ‘sit’ and ‘amet’ separated by a dash (-) or a colon (:) with a space.
original_text='''Lorem ipsum dolor sit-amet, consectetur adipiscing elit. Phasellus iaculis velit ac nunc interdum tempor.
Ut volutpat elit metus, vel auctor enim commodo at. Nunc quis quam non ligula ultricies luctus porta id justo.
Quisque dapibus est ut sagittis bibendum. Mauris ullamcorper pellentesque porttitor. Ut sit:amet ex nec dolor gravida
porttitor. Proin at justo finibus justo vestibulum congue. Suspendisse et ipsum et ipsum eleifend dapibus a fermentum
lacus. Vivamus porta nunc eu nisl sagittis, quis vulputate metus dignissim. Integer non fermentum nisl, id vestibulum
elit. Sed euismod vestibulum purus ut porttitor. Integer sit-amet mollis neque. Donec sed lacinia diam, ac finibus
lectus. Mauris tempor ipsum nisl, vitae posuere est lacinia nec. Nam eget euismod odio.'''
lorem_ipsum = '''Lorem ipsum dolor sit-amet, consectetur adipiscing elit. Phasellus iaculis velit ac nunc interdum tempor.
Ut volutpat elit metus, vel auctor enim commodo at. Nunc quis quam non ligula ultricies luctus porta id justo.
Quisque dapibus est ut sagittis bibendum. Mauris ullamcorper pellentesque porttitor. Ut sit:amet ex nec dolor gravida
porttitor. Proin at justo finibus justo vestibulum congue. Suspendisse et ipsum et ipsum eleifend dapibus a fermentum
lacus. Vivamus porta nunc eu nisl sagittis, quis vulputate metus dignissim. Integer non fermentum nisl, id vestibulum
elit. Sed euismod vestibulum purus ut porttitor. Integer sit-amet mollis neque. Donec sed lacinia diam, ac finibus
lectus. Mauris tempor ipsum nisl, vitae posuere est lacinia nec. Nam eget euismod odio.'''
Find and print the number of non-alphanumeric characters
Find and print the number of non-alphanumeric characters.
The first thing to do when doing a pattern search is to import the module re which provides full support for regular expressions in Python. This has already been provided in the code to the left.
Also provided for you on the left is the original string,original_text and the string,lorem_ipsum. lorem_ipsum is the string you will be using to change as you go through the programming activities in this course unit.
Find all of the instances of non alphanumeric characters in the string assigned to lorem_ipsum.
Use the ^ and [] regular expression operator along with the findall() regular expression function.
Assign the outcome to a variable named results.
Output to the console, the number of non-alphanumeric characters. Hint: use the len function.
Find and print the number of 'sit' and 'amet' separated with punctuation marks
Using the re.findall() function, get all of the instances of 'sit-amet' or 'sit:amet' characters in the string assigned to lorem_ipsum.
Assign the outcome to a variable named occurrences_sit_amet
Output to the console, the number of sit-amet or sit:amet occurrences.
Replace 'sit' and 'amet' words separated with punctuation marks with 'sit amet'
Replace sit:amet and sit-amet with sit amet using the re.sub() function.
Assign the outcome to a variable named replace_results
Find and print all the instances of 'sit amet'
Using the re.findall() function, get all of the instances of 'sit amet'in the string assigned to replace_results.
Assign the outcome to a variable named occurrence_sit_amet.
Output to the console, the number of sit amet occurrences.
"Please add comments within Script, for the guidance of the code and what each function is doing, so i can understand more."
Editable Code:
import re
def check_non_alphanum():
pattern_to_check_non_alphanum =
r'[^a-zA-Z0-9\_]' # Excluding Lower case and Upper case
alphabets and 0-9 numbers
results = re.findall(pattern_to_check_non_alphanum,
(str(lorem_ipsum))) # Finds and returns a list of all
non-alphanumeric characters in lorem_ipsum string
print("Number of occurences of non-alphanumeric
characters in lorem_ipsum = %d" %(len(results))) #
Prints the number of non-alphanumeric characters in lorem_ipsum
string
def check_occurrences_sit_amet():
pattern_to_check_sit_amet_with_punctuation =
r'sit-amet|sit:amet' # Pattern to check 'sit-amet' or
'sit:amet'
occurrences_sit_amet =
re.findall(pattern_to_check_sit_amet_with_punctuation,
(str(lorem_ipsum))) # Finds and returns a list of
occurences of 'sit-amet' or 'sit:amet'
print ("Number of occurences of 'sit-amet' and
'sit:amet' in lorem_ipsum = %d"
%(len(occurrences_sit_amet))) # Prints the number of
occurences of 'sit-amet' or 'sit:amet'
def replace_lorem_ipsum():
pattern_to_check_sit_amet_with_punctuation =
r'sit-amet|sit:amet' # Pattern to check 'sit-amet' or
'sit:amet'
to_change = r'sit amet' # String with
removed punctuation marks '-' and ':'
replace_results =
re.sub(pattern_to_check_sit_amet_with_punctuation, to_change,
lorem_ipsum, flags=re.IGNORECASE) # Substitute
punctuated string to non-punctuated string (string =
lorem_ipsum)
occurrences_sit_amet = re.findall(to_change,
(str(replace_results))) # Finds and returns a list of
occurences of 'sit amet' in replace_results
print ("Number of occurences of 'sit amet in
replace_results = %d" % (len(occurrences_sit_amet))) #
Print the number of occurences of 'sit amet' in replace_results
using len
if __name__ == "__main__":
original_text = '''Lorem ipsum dolor sit-amet,
consectetur adipiscing elit. Phasellus iaculis velit ac nunc
interdum tempor.
Ut volutpat elit metus, vel auctor enim commodo at.
Nunc quis quam non ligula ultricies luctus porta id justo.
Quisque dapibus est ut sagittis bibendum. Mauris
ullamcorper pellentesque porttitor. Ut sit:amet ex nec dolor
gravida
porttitor. Proin at justo finibus justo vestibulum
congue. Suspendisse et ipsum et ipsum eleifend dapibus a
fermentum
lacus. Vivamus porta nunc eu nisl sagittis, quis
vulputate metus dignissim. Integer non fermentum nisl, id
vestibulum
elit. Sed euismod vestibulum purus ut porttitor.
Integer sit-amet mollis neque. Donec sed lacinia diam, ac
finibus
lectus. Mauris tempor ipsum nisl, vitae posuere est
lacinia nec. Nam eget euismod odio.'''
lorem_ipsum = '''Lorem ipsum dolor sit-amet,
consectetur adipiscing elit. Phasellus iaculis velit ac nunc
interdum tempor.
Ut volutpat elit metus, vel auctor enim commodo at.
Nunc quis quam non ligula ultricies luctus porta id justo.
Quisque dapibus est ut sagittis bibendum. Mauris
ullamcorper pellentesque porttitor. Ut sit:amet ex nec dolor
gravida
porttitor. Proin at justo finibus justo vestibulum
congue. Suspendisse et ipsum et ipsum eleifend dapibus a
fermentum
lacus. Vivamus porta nunc eu nisl sagittis, quis
vulputate metus dignissim. Integer non fermentum nisl, id
vestibulum
elit. Sed euismod vestibulum purus ut porttitor.
Integer sit-amet mollis neque. Donec sed lacinia diam, ac
finibus
lectus. Mauris tempor ipsum nisl, vitae posuere est
lacinia nec. Nam eget euismod odio.'''
# Function calls
check_non_alphanum()
check_occurrences_sit_amet()
replace_lorem_ipsum()
Images of code (for indentation reference):
Output of the corresponding code:
Having a little trouble with this assignment. "Please add comments within Script, for the guidance of...
I need to explain the importance of knowing how and when to use: A search using literal characters For sequence and ranges For special characters and wild cards I need to support this answer with examples from my script. Please help. ( I need a written answer and explanation my script is complete and running) 140 lorem ipsumLorem ipsum dolor sit-amet consectetur adipiscing elit- Phasellus iaculis velit ac ounc interdum tempor Ut volutpat elit metus, vel auctor enim commodo at....
Problems: Develop a web page that allows a user to try out different text and background color combinations by clicking buttons. Use Javascript to implement the functionalities. Below are two screen shots of the working application. Clicking the buttons at the bottom changes either the foreground color or the background color. The following descriptions are about the details. Specifically, you have to implement: An HTML file should include the following features (3 points) Some texts (Anything you like) Put the...
Text: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem...
CaesarCipher Introduction Hiding the meaning of messages by putting them in some kind of code is something we have all done, from the “coding rings” and secret symbols of childhood to the top secrecy of military and commercial establishments’ secret data. In fact, some of the earliest uses of computers were for coding messages and for breaking the enemy’s coded messages. Newer coding methods are among the most interesting research areas in computer science today. The Caesar Cipher One of...
python program do not use dictionary, list only Complete the program found in assignment4.py. You may not change any provided code. You may only complete the sections labeled: #YOUR CODE HERE Write a program that does the following. Reads the contents of Text from the include file, input.txt Create a dictionary of key-value pairs called index.txt Key: This represents the individual word Value: This is a list of the line number from Text where Key appeared Example: If the word...