Python Exercise: Print the string article. It contains the first few paragraphs of an article . It already looks quite n

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Python Exercise: Print the string article. It contains the first few paragraphs of an article . It already looks quite n

Post by answerhappygod »

Python Exercise:
Print the string article. It contains the first few
paragraphs of an article . It already looks quite nice, however,
since it was just copy & pasted from the internet, there are a
few things that you should improve upon.
It is your task to write a function make nice that
takes an article text like this as argument and returns a copy with
the following improvements:
First generate a list of all the lines. Since there are a lot of
double linebreaks, this results in some empty lines. Find a way to
get a list of only those lines that contain text.
Drop the third and the sixth line. The third only contains
duplicate information and the sixth (the update) was only added
afterwards.
Arrange the meta-data about author and date in a sensible
way.
Add line numbers for the body of the text and lines marking the
start and end of the text
Execute article nice = make nice(article) after your
function definition. From now on we will work with this version of
the article.
The output of print(article nice) should look like
this (with complete lines, no ”...”):
Now we want to learn something about our article. Write a script
that answers the following questions on the contents
of article nice:
What is the most frequent word in the text?
What is the most frequent character in the text?
How many non-alphanumeric characters are there in the text?
Subtract the 118 ”#” and 8 ”:” that we artificially
inserted.
Hint 1: To answer the first two questions, you can first
define a function print frequencies that takes a list of
strings as argument and for each element in the list prints how
often it is contained in the list. You can also just print those
elements that occur more than once.
Hint 2: count can also be applied to lists with the
following syntax: my list.count(my
element). split may also come in handy.
Text code:
article = """High-Tech Degrees and the Price of an Avocado: The
Data New York Gave to Amazon The city and state sent loads of data
to Amazon during its search for a new headquarters, offering a peek
into the valuable information the company collected during the
process. Karen Weise By Karen Weise Dec. 12, 2018 [Updated Feb. 14:
Amazon said it was canceling plans to build a corporate campus in
New York City, after the deal had run into fierce opposition from
local lawmakers who criticized providing subsidies to one of the
world’s most valuable companies.] An avocado at Whole Foods costs
1.25. Columbia University handed out 724 graduate degrees in
computer science over the past three years. And 10 potential land
parcels in Long Island City are zoned M1-4, for light
manufacturing. New York provided all of these data points, and
thousands more, to Amazon as part of its successful bid to woo the
tech giant to town. On Monday, New York City posted online the
253-page proposal it submitted, along with New York State, to
Amazon in March. The city quickly took the file down, saying it
should have checked with its partners before posting it, because
the document included proprietary information. But The New York
Times downloaded the document before it was taken off the public
website. The proposal shows the types of data, some rarely
available publicly, that the company amassed from cities across the
country as part of its search for a second headquarters.
"""
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply