Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I code this in Python? Will you be able to show me the answer along with with step by step instructions? I am

How do I code this in Python? Will you be able to show me the answer along with with step by step instructions? I am completely lost and do not know where to start.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Part 2: Open-ended Tasks Background: Useful Functions for Reading Files A frequent programming task is to read in data from a file and process it. Python has functions and methods for doing this in a few lines of code. open(filename) file.read() Takes the name of a file and produces a readable file value. Returns the content of the file as a string. This means that, for example, if you have a file called words5.txt in your workspace, you can use the definition below, and a string of the text in the file will be stored in the contents variable: contents = open("words5.txt").read() Note that if you want to open a file named something other than words5.txt, you would need to replace the filename in the use of open(). We provide three files you can use for this assignment, words.txt, words100+.txt, and words5.txt. You should be able to see these files when you open the assignment for the first time. words.txt is a file that contains some standard English words for spell checking and autocorrect (Ed cannot display a file this large, but feel free to download the file and see what's in it). words100+.txt is a random sample of those words, and words5.txt is a sample of just 5 words. This assignment will have you read in a file that has text on many different lines. You can use the string split method to get a list of the lines in the file. The way text is stored in files, the special character " " indicates a new line (n is for newline) you see it as the text going onto the next line, but Python sees it as this special character. So you can split the text from the file on newlines and get a list of strings with the code below (a more detailed example is shown at the end of this document). contents = open("words5.txt").read() lines = contents.strip().split(" ") # strip() removes any extra blank lines Part 2.1: Implementation You will choose an interesting textual dataset - you can use the provided words files or you can find one on your own. Then, in pa4_open_ended.py, you'll write an interesting transformation of the lines of text (using map), and an interesting search on the text (using filter). You must do both. You can pick any search criteria you want. For example, you might choose one of the following for filter: . Find all the lines that have at least three "a" characters Find all the lines that contain a particular string . Find all the lines that start with a particular string Find all the lines that contain numeric data 0 You can choose any transformation of the string data you want. You might choose one of the following for map: . Make a list of all the strings with all the letters capitalized Make a list of all the strings with all the a's replaced with b's Make a list of all the numeric strings converted to numbers . Get creative! You can browse the Python string methods to see what some options are. You must write (at least) four function definitions: . A function you will pass to filter, which can have any name you choose (in the class handout, is_pos was an example of such a function). search_lines, which takes a list of strings and returns a list of strings. It must call filter. A function you will pass to map, which can have any name you choose. transform_lines, which takes a list of strings and returns a list of a datatype of your choice. It must call map. Part 2.2: Write up You must report on how you tested your code. In particular, you must provide: A. Report Bugs and Issues Include in your report any known bugs or issues with your program. If you think you have none, also let us know. B. Demo at Interactions: For all your functions that take and produce lists, first tell us what they do. Then, demonstrate their behavior at interactions. An example of the interactions is attached below. For search_lines, call it on a large list (over 100, like using the full words.txt file or the words 100+.txt file). Compare the length of the filtered list to the length of the original list using len on both. For transform_lines, call it on a small number of lines (for example, read in just the lines from words5.txt for this) and copy/paste the output. Make sure that these lines show any interesting behavior of your transform function. Example Here's an example run where the search is whether the string is longer than 10 characters, and the transform uses shout from class. You can use the code provided for contents and lines if it helps you to get started. In the Python file: # load the entire words.txt file into lines as a list contents = open("words.txt").read() lines = contents.strip().split(" ") # load the smaller words5.txt file into lines5 as a list contents5 = open("words5.txt").read() lines5 = contents5.strip().split(" ") # ... definitions of four functions would go here # search_lines is defined to keep lines longer than 10 characters # transform_lines is defined to make all the strings shout Demonstration at interactions: >>> lines5 ['aquation', 'defend', 'indolence', 'interviewable', 'revolt'] >>> transform_lines (lines5) ['AQUATION', DEFEND', 'INDOLENCE', 'INTERVIEWABLE', 'REVOLT'] >>> len(lines) 235886 >>> search_lines (lines5) ['interviewable'] >>> long_lines = search_lines(lines) >>> len(long_lines) 83907 >>> len(lines) 235886 >>> # 83907 lines are longer than 10 characters! i aquation 2 defend 3 indolence 4 interviewable 5 revolt Part 2: Open-ended Tasks Background: Useful Functions for Reading Files A frequent programming task is to read in data from a file and process it. Python has functions and methods for doing this in a few lines of code. open(filename) file.read() Takes the name of a file and produces a readable file value. Returns the content of the file as a string. This means that, for example, if you have a file called words5.txt in your workspace, you can use the definition below, and a string of the text in the file will be stored in the contents variable: contents = open("words5.txt").read() Note that if you want to open a file named something other than words5.txt, you would need to replace the filename in the use of open(). We provide three files you can use for this assignment, words.txt, words100+.txt, and words5.txt. You should be able to see these files when you open the assignment for the first time. words.txt is a file that contains some standard English words for spell checking and autocorrect (Ed cannot display a file this large, but feel free to download the file and see what's in it). words100+.txt is a random sample of those words, and words5.txt is a sample of just 5 words. This assignment will have you read in a file that has text on many different lines. You can use the string split method to get a list of the lines in the file. The way text is stored in files, the special character " " indicates a new line (n is for newline) you see it as the text going onto the next line, but Python sees it as this special character. So you can split the text from the file on newlines and get a list of strings with the code below (a more detailed example is shown at the end of this document). contents = open("words5.txt").read() lines = contents.strip().split(" ") # strip() removes any extra blank lines Part 2.1: Implementation You will choose an interesting textual dataset - you can use the provided words files or you can find one on your own. Then, in pa4_open_ended.py, you'll write an interesting transformation of the lines of text (using map), and an interesting search on the text (using filter). You must do both. You can pick any search criteria you want. For example, you might choose one of the following for filter: . Find all the lines that have at least three "a" characters Find all the lines that contain a particular string . Find all the lines that start with a particular string Find all the lines that contain numeric data 0 You can choose any transformation of the string data you want. You might choose one of the following for map: . Make a list of all the strings with all the letters capitalized Make a list of all the strings with all the a's replaced with b's Make a list of all the numeric strings converted to numbers . Get creative! You can browse the Python string methods to see what some options are. You must write (at least) four function definitions: . A function you will pass to filter, which can have any name you choose (in the class handout, is_pos was an example of such a function). search_lines, which takes a list of strings and returns a list of strings. It must call filter. A function you will pass to map, which can have any name you choose. transform_lines, which takes a list of strings and returns a list of a datatype of your choice. It must call map. Part 2.2: Write up You must report on how you tested your code. In particular, you must provide: A. Report Bugs and Issues Include in your report any known bugs or issues with your program. If you think you have none, also let us know. B. Demo at Interactions: For all your functions that take and produce lists, first tell us what they do. Then, demonstrate their behavior at interactions. An example of the interactions is attached below. For search_lines, call it on a large list (over 100, like using the full words.txt file or the words 100+.txt file). Compare the length of the filtered list to the length of the original list using len on both. For transform_lines, call it on a small number of lines (for example, read in just the lines from words5.txt for this) and copy/paste the output. Make sure that these lines show any interesting behavior of your transform function. Example Here's an example run where the search is whether the string is longer than 10 characters, and the transform uses shout from class. You can use the code provided for contents and lines if it helps you to get started. In the Python file: # load the entire words.txt file into lines as a list contents = open("words.txt").read() lines = contents.strip().split(" ") # load the smaller words5.txt file into lines5 as a list contents5 = open("words5.txt").read() lines5 = contents5.strip().split(" ") # ... definitions of four functions would go here # search_lines is defined to keep lines longer than 10 characters # transform_lines is defined to make all the strings shout Demonstration at interactions: >>> lines5 ['aquation', 'defend', 'indolence', 'interviewable', 'revolt'] >>> transform_lines (lines5) ['AQUATION', DEFEND', 'INDOLENCE', 'INTERVIEWABLE', 'REVOLT'] >>> len(lines) 235886 >>> search_lines (lines5) ['interviewable'] >>> long_lines = search_lines(lines) >>> len(long_lines) 83907 >>> len(lines) 235886 >>> # 83907 lines are longer than 10 characters! i aquation 2 defend 3 indolence 4 interviewable 5 revolt

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions

Question

Describe the steps in developing a flexible budget.

Answered: 1 week ago

Question

Explain exothermic and endothermic reactions with examples

Answered: 1 week ago

Question

Write a short note on rancidity and corrosiveness.

Answered: 1 week ago