In: Computer Science
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output.
Here is the starter outline for the homework:
a.
def count_character(text, char):
""" Count the number of times a character occurs in some text.
Do not use the count() method. """
return 0
b.
def count_sentences(text):
""" Count the number of sentences occurring in some text
We assume a sentence corresponds to the number of periods. """
return 0
c.
def text_to_words(text, punctuation=".,?;"):
"""remove punctuation, convert all words to lower case and
return a list of words """
return []
gettysburg_address = """
Four score and seven years ago our fathers brought forthon this continent, a new nation, conceived in Liberty,and dedicated to the proposition that all men are createdequal. Now we are engaged in a great civil war, testingwhether that nation, or any nation so conceived and sodedicated, can long endure. We are met on a greatbattlefield of that war. We have come to dedicate aportion of that field, as a final resting place forthose who here gave their lives that that nation mightlive. It is altogether fitting and proper that we shoulddo this. But, in a larger sense, we can not dedicate, wecan not consecrate, we can not hallow this ground.The brave men, living and dead, who struggled here,have consecrated it, far above our poor power to addor detract. The world will little note, nor longremember what we say here, but it can never forget whatthey did here. It is for us the living, rather, to bededicated here to the unfinished work which they whofought here have thus far so nobly advanced. It israther for us to be here dedicated to the great taskremaining before us—that from these honored dead wetake increased devotion to that cause for which theygave the last full measure of devotion that we herehighly resolve that these dead shall not have died invain that this nation, under God, shall have a newbirth of freedom and that government of the people, bythe people, for the people, shall not perish from theearth.
"""
Program Code Screenshot:
Sample output:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Program to copy:
""" Count the number of times a character occurs in some text.
Do not use the count() method. """
def count_character(text, char):
c=0
for i in text:
if i==char:
c+=1
return c
""" Count the number of sentences occurring in some text
We assume a sentence corresponds to the number of periods. """
def count_sentences(text):
c=0
for i in text:
if i=='.':
c+=1
else:
continue
return c
def text_to_words(text, punctuation=".,?;"):
l=[]
text_list=text.split()
for i in text_list:
f=0
for j in punctuation:
if j in i:
temp=i.replace(j,'')
temp=temp.lower()
l.append(temp)
f=1
break
if f!=1:
l.append(i)
return l
gettysburg_address = """
Four score and seven years ago our fathers brought forthon this
continent, a new nation, conceived in Liberty,and dedicated to the
proposition that all men are createdequal. Now we are engaged in a
great civil war, testingwhether that nation, or any nation so
conceived and sodedicated, can long endure. We are met on a
greatbattlefield of that war. We have come to dedicate aportion of
that field, as a final resting place forthose who here gave their
lives that that nation mightlive. It is altogether fitting and
proper that we shoulddo this. But, in a larger sense, we can not
dedicate, wecan not consecrate, we can not hallow this ground.The
brave men, living and dead, who struggled here,have consecrated it,
far above our poor power to addor detract. The world will little
note, nor longremember what we say here, but it can never forget
whatthey did here. It is for us the living, rather, to bededicated
here to the unfinished work which they whofought here have thus far
so nobly advanced. It israther for us to be here dedicated to the
great taskremaining before us—that from these honored dead wetake
increased devotion to that cause for which theygave the last full
measure of devotion that we herehighly resolve that these dead
shall not have died invain that this nation, under God, shall have
a newbirth of freedom and that government of the people, bythe
people, for the people, shall not perish from theearth.
"""
print(text_to_words(gettysburg_address))
print("The number of
sentences:",count_sentences(gettysburg_address))