How would I fix this code? The goal was to create a program that uses a regular expression to find all the words in waroftheworlds.txt that begin with th (or Th) and end with an e and report a list if all the unique words found, the total number of unique occurrences and the total number of all occurrences. My code: import re try: fn = "waroftheworlds.txt" with open(fn, 'r') as file: contents = file.read() pattern = r"\b[Tt]h\S*e\b" regex = re.compile(pattern) matches = regex.findall(contents) unique_words = set(map(str.lower, matches)) print("Unique words found:") print(", ".join(unique_words)) print("Total number of unique occurrences:", len(unique_words)) print("Total number of occurrences:", len(matches))
How would I fix this code? The goal was to create a program that uses a regular expression to find all the words in waroftheworlds.txt that begin with th (or Th) and end with an e and report a list if all the unique words found, the total number of unique occurrences and the total number of all occurrences. My code: import re try: fn = "waroftheworlds.txt" with open(fn, 'r') as file: contents = file.read() pattern = r"\b[Tt]h\S*e\b" regex = re.compile(pattern) matches = regex.findall(contents) unique_words = set(map(str.lower, matches)) print("Unique words found:") print(", ".join(unique_words)) print("Total number of unique occurrences:", len(unique_words)) print("Total number of occurrences:", len(matches))
Oh no! Our experts couldn't answer your question.
Don't worry! We won't leave you hanging. Plus, we're giving you back one question for the inconvenience.
Submit your question and receive a step-by-step explanation from our experts in as fast as 30 minutes.
You have no more questions left.
Message from our expert:
Sorry, our expert can’t provide a solution for this question. Your question has been credited back for next time.
Your Question:
How would I fix this code? The goal was to create a program that uses a regular expression to find all the words in waroftheworlds.txt that begin with th (or Th) and end with an e and report a list if all the unique words found, the total number of unique occurrences and the total number of all occurrences.
My code:
import re
try:
fn = "waroftheworlds.txt"
with open(fn, 'r') as file:
contents = file.read()
pattern = r"\b[Tt]h\S*e\b"
regex = re.compile(pattern)
matches = regex.findall(contents)
unique_words = set(map(str.lower, matches))
print("Unique words found:")
print(", ".join(unique_words))
print("Total number of unique occurrences:", len(unique_words))
print("Total number of occurrences:", len(matches))
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education