Everything you need to know to complete today's activities is covered in appendix A of our textbook. There are also several relevant sections from the official Python tutorial:
You may also want to take a look at the complete list of string methods.
removeLetter(string, letter)
. This function
should take a string and letter as arguments, and return a copy of that
string with every instance of the indicated letter removed. For example,
removeLetter("Hello there!", "e")
, should
return the string "Hllo thr!"
.
greeting = "" greeting = greeting + "H" greeting = greeting + "i"Or, equivalently:
greeting = "" greeting += "H" greeting += "i"
main
function to your program, and use it to
test removeLetter
.
replaceWord(sentence, original,
new)
. This function will take three strings as
arguments. The first will contain a sentence, the
second will contain a word to replace, and the third
will contain the replacement word. The return value
should be a copy of sentence
with every
instance of original
replaced with an
instance of new
. For example,
replaceWord("I am happy to meet you!", "happy", "angry")should return the string
"I am angry to meet you!
"
. Don't worry about handling punctuation
correctly. Punctuation marks may be considered part of
the word that they follow. (Note that Python strings have
a built-in replace
method that can accomplish
this in a single line of code. Don't use it! Solve the
problem by iterating over the words in the string.)
split
method that splits
the string into a list of words. For example, after the following code segment:
greeting = "hello there" words = greeting.split()
words
will contain the list ['hello', 'there']
.
main
. I
suggest testing this function in conjunction
with removeLetter
as follows:
angry = replaceWord("I am happy to meet you!", "happy", "angry") noY = removeLetter(angry, "y") print(noY)The result should be
"I am angr to meet ou!"
.
reverseSentence(string)
. This function
should take a string as an argument, and return a new string in
which the order of the individual words has been reversed.
range
function or by using negative list indexing.
removeFrequent(string,
maxCount)
. This function should take a string as
an argument, and should return a new string in which all
words that occur more than maxCount
times
have been removed.
For example,
removeInfrequent("This is great really really really great", 2)should return the string
"This is great great "
.
dict
to
create a count for all the words. The second time
through, you can build the new string from all of the
words that have few enough occurrences.
There is nothing to hand in for this assignment. Make sure that you save a copy of your code. If you worked with a partner, make sure both of you get a copy.