]: ]: ]: This one definitely falls in the category of pointless exercises. You're going to...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
]: ]: ]: This one definitely falls in the category of "pointless exercises". You're going to make a program that prompts for a filename. If the file exists, quit the program! Don't print anything out, don't do anything with the file, don't do anything. Just quit. If the file doesn't exist, complain, and prompt the user for a filename again. Do this until the user gives you a valid filename or pours water on their computer in frustration. (Just kidding. Don't pour water on your computer.) You're going to want to put the filename prompt inside a loop, so you can ask the user for a filename as many times as you want. But what kind of loop? And how will you get out of the loop once you've got a valid file? Exercise 4 sections to reference: searching through a file This exercise is a continuation of the previous one. You've got a new text file to process, called spam2.txt. It has a bunch of spam confidence values, but it's also got some other information, too, like the sender, message subject, and the date and time that it's been sent. A sample record from the file looks like this: From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008 Return-Path: <postmaster@collab.sakaiproject.org> X-Sieve: CMU Sieve 2.3 Message-ID: Mime-Version: 1.0 <200801042308.m04N8v60008125@nakamura.uits.iupui.edu> Content-Transfer-Encoding: 7bit Date: Fri 4 Jan 2008 18:08:57 -0500 To: source@collab.sakaiproject.org Subject: [sakai] svn commit: r39771 in bspace/site-manage/sakai_2-4-x/site-manage-tool/tool/src: bundle java/org/sakaiproject/site/tool X-Content-Type-Outer-Envelope: X-Content-Type-Message-Body: text/plain; charset=UTF-8 text/plain; charset=UTF-8 Content-Type: text/plain; charset=UTF-8 X-DSPAM-Result: Innocent X-DSPAM-Processed: Fri Jan 4 18:10:48 2008 X-DSPAM-Confidence: 0.6178 X-DSPAM-Probability: 0.0000 There is a lot of information here, and this, if you can believe it, is a stripped-down version of what the email server records look like. spam2.txt contains a bunch of email records. You're going to write a program, similar to Exercise 3, wherein you grab the spam confidence from each email. Like before, you're going to take the average of these values. When you encounter an email with a spam confidence greater than 95%, though, you're going to print out the email address that sent the email, the email address that received it, the date and time that it was sent, and the spam confidence value. Note: Don't simply print out the entire lines of the file that have that information; get the specific items that you want and format it nicely. [ ]: Exercise 6 sections to reference: lists So, now that you're an expert in sorting lists of food, let's get a little bit more literary. richard3.txt contains the text of Richard's opening soliloquy from Shakespeare's play Richard III. You can watch a performance of it here, if you're interested. You're going to write a program that reads in the soliloquy from the file line by line. Then, using the split function, split each line into a list of words. Create a list to contain all of your words. Going word by word, check if the word is already in the list. If not, add it. Once you've gone through the whole file, sort the list of words in alphabetical order, and print it out. fh = open ("richard3.txt") 1st = list() for line in fh: words= line.split() for word in words: if word in 1st: continue 1st.append(word) print (sorted (lst)) Exercise 7 sections to reference: dictionaries and files In the previous exercise, you just discarded words that were duplicates, but what if you wanted to count them? You're going to read in richard3.txt again, but this time use a dictionary. Iterate through the lines in the file and, for each word, if this is its first occurence, add the word as a key to the dictionary and the number "1" as a value. If the word exists in the dictionary, add one to its value. Then, print out your word-frequency dictionary. ]: ]: ]: This one definitely falls in the category of "pointless exercises". You're going to make a program that prompts for a filename. If the file exists, quit the program! Don't print anything out, don't do anything with the file, don't do anything. Just quit. If the file doesn't exist, complain, and prompt the user for a filename again. Do this until the user gives you a valid filename or pours water on their computer in frustration. (Just kidding. Don't pour water on your computer.) You're going to want to put the filename prompt inside a loop, so you can ask the user for a filename as many times as you want. But what kind of loop? And how will you get out of the loop once you've got a valid file? Exercise 4 sections to reference: searching through a file This exercise is a continuation of the previous one. You've got a new text file to process, called spam2.txt. It has a bunch of spam confidence values, but it's also got some other information, too, like the sender, message subject, and the date and time that it's been sent. A sample record from the file looks like this: From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008 Return-Path: <postmaster@collab.sakaiproject.org> X-Sieve: CMU Sieve 2.3 Message-ID: Mime-Version: 1.0 <200801042308.m04N8v60008125@nakamura.uits.iupui.edu> Content-Transfer-Encoding: 7bit Date: Fri 4 Jan 2008 18:08:57 -0500 To: source@collab.sakaiproject.org Subject: [sakai] svn commit: r39771 in bspace/site-manage/sakai_2-4-x/site-manage-tool/tool/src: bundle java/org/sakaiproject/site/tool X-Content-Type-Outer-Envelope: X-Content-Type-Message-Body: text/plain; charset=UTF-8 text/plain; charset=UTF-8 Content-Type: text/plain; charset=UTF-8 X-DSPAM-Result: Innocent X-DSPAM-Processed: Fri Jan 4 18:10:48 2008 X-DSPAM-Confidence: 0.6178 X-DSPAM-Probability: 0.0000 There is a lot of information here, and this, if you can believe it, is a stripped-down version of what the email server records look like. spam2.txt contains a bunch of email records. You're going to write a program, similar to Exercise 3, wherein you grab the spam confidence from each email. Like before, you're going to take the average of these values. When you encounter an email with a spam confidence greater than 95%, though, you're going to print out the email address that sent the email, the email address that received it, the date and time that it was sent, and the spam confidence value. Note: Don't simply print out the entire lines of the file that have that information; get the specific items that you want and format it nicely. [ ]: Exercise 6 sections to reference: lists So, now that you're an expert in sorting lists of food, let's get a little bit more literary. richard3.txt contains the text of Richard's opening soliloquy from Shakespeare's play Richard III. You can watch a performance of it here, if you're interested. You're going to write a program that reads in the soliloquy from the file line by line. Then, using the split function, split each line into a list of words. Create a list to contain all of your words. Going word by word, check if the word is already in the list. If not, add it. Once you've gone through the whole file, sort the list of words in alphabetical order, and print it out. fh = open ("richard3.txt") 1st = list() for line in fh: words= line.split() for word in words: if word in 1st: continue 1st.append(word) print (sorted (lst)) Exercise 7 sections to reference: dictionaries and files In the previous exercise, you just discarded words that were duplicates, but what if you wanted to count them? You're going to read in richard3.txt again, but this time use a dictionary. Iterate through the lines in the file and, for each word, if this is its first occurence, add the word as a key to the dictionary and the number "1" as a value. If the word exists in the dictionary, add one to its value. Then, print out your word-frequency dictionary.
Expert Answer:
Answer rating: 100% (QA)
Exercise 4 Prompting for a Filename Heres a Python program that prompts the user for a filename until they provide a valid one or decide to quit pytho... View the full answer
Related Book For
A Concise Introduction to Logic
ISBN: 978-1305958098
13th edition
Authors: Patrick J. Hurley, Lori Watson
Posted Date:
Students also viewed these programming questions
-
Many groups have treasury policies that use hedging of net positions . solve They would look at their group-wide exposure in, e . solveg . solve foreign currency and then take forward contracts to...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Managing Scope Changes Case Study Scope changes on a project can occur regardless of how well the project is planned or executed. Scope changes can be the result of something that was omitted during...
-
Five Smithtown High School students are saving up to buy their first cars. They all have after-school jobs, and their weekly salaries are listed in the table. Emily ..........................$110 Sam...
-
For each of the agent types listed in Exercise 2.5, characterize the environment according to the properties given in Section 2.3, and select a suitable agent design. The following exercises all...
-
Refer to the consolidate statements of earnings in the Campbell Soup Company annual report in the appendix Required: a. Calculate the gross profit ratio for each of the past three years. b. Assume...
-
In Example 8.1, the mosquito has an inertia of \(0.1 \mathrm{~g}\) and is initially at rest, while the bus, with an inertia of \(10,000 \mathrm{~kg}\), has an initial speed of \(25 \mathrm{~m} /...
-
The management of Russel Inc. is trying to decide whether it can increase its dividend. During the current year, it reported net income of $875,000. It had cash provided by operating activities of...
-
Mr. Mutwiri opened a business in Meru town at the beginning of the year. During the first month of operation the following transactions were completed. 2014. January 1 Deposited sh 20,000 in the bank...
-
Use a dot plot to display the data. The data represent the systolic blood pressures (in millimeters of mercury) of 30 patients at a doctor's office. Organize the data using the indicated type of...
-
Pretend you are an event planner for a major Technology Association called TechHeads Inc. and you are recommending that your event will for the first time use networking and appointment scheduling...
-
The chief financial officer (CFO) of New Age Fashion Ltd. has just received a request from a project manager to authorize an expenditure in the amount of 45,000. The manager states that this...
-
A 2-year Treasury bond currently offers a 6% rate of return. A 3-year Treasury bond offers a 7% rate of return. Under the expectations theory, what rate of return do investors expect a 2-year...
-
Suppose you purchase a 30-year Treasury bond with a 6% annual coupon, initially trading at par. In 10 years time, the bonds yield to maturity has risen to 7% (EAR). a. If you sell the bond now, what...
-
Transaction costs Assume that you use a TD Ameritrade brokerage account and place your stock trades through the Interactive Voice Response (IVR) Phone System, which charges a $34.99 commission per...
-
Jonathan Harper currently has 1,000 that he can spend today on novelty yarn costing 20 a bundle. Alternatively, he could invest the 1,000 in a Bank of England bond that pays 6% nominal rate of...
-
(20 points) "San Luis Valley syndrome", also known as "Recombinant 8" is caused by a chromosome inversion that includes the centromere (pericentric) of chromosome 8 in humans (let us say region BCD...
-
Write a paper detailing a geographic information system (GIS) of your own design that would utilize data in an original manner.
-
Some ATM locations are places criminals lurk. All places criminals lurk are places to avoid at night. Therefore, some places to avoid at night are ATM locations. Use Venn diagrams to determine...
-
[X (R F)] [(X R) F ] Use truth tables to determine whether the following symbolized statements are tautologous, self-contradictory, or contingent.
-
Nothing that is brittle is ductile. Superconductors are all ceramics. Only ductile things can be pulled into wires. Ceramics are brittle. Superconductors cannot be pulled into wires. The following...
-
The mean height of American males is 69.5 inches. The heights of the 43 male U.S. presidents* (Washington through Obama) have a mean 70.78 inches and a standard deviation of 2.77 inches. Treating the...
-
The "fun size" of a Snickers bar is supposed to weigh 20 grams. Because the penalty for selling candy bars under their advertised weight is severe, the manufacturer calibrates the machine so the mean...
-
According to the American Community Survey, the mean travel time to work in Collin County, Texas, in 2013 was 27.5 minutes. The Department of Transportation reprogrammed all the traffic lights in...
Study smarter with the SolutionInn App