In: Computer Science
# Let me define the function I use for testing. Don't change this cell.
def check_equal(x, y, msg=None):
if x == y:
if msg is None:
print("Success")
else:
print(msg, ": Success")
else:
if msg is None:
print("Error:")
else:
print("Error in", msg, ":")
print(" Your answer was:", x)
print(" Correct answer: ", y)
assert x == y, "%r and %r are different" % (x, y)
## Problem 1: common word pairs
The goal is to find the set of consecutive word pairs in common between two sentences.
We will get there in steps, guiding you to the solution.
# First, notice that .split() splits a sentence according to white space, returning a list.
print("I love eating bananas".split())
# Second, recall that in Python, a[i:k] gives you the sub-list of a consisting
# of a[i], a[i+1], ..., a[k-1].
a = "I love eating bananas".split()
print(a[2:4])
### Problem 1 part 1: generating the list of consecutive words in a sentence.
You are given a sentence, with words separated by white space. You need to write a function `word_pairs` that outputs the list of consecutive words. For example, if the input is:
"I love eating bananas"
Then the function `word_pairs` should output:
[("I", "love"), ("love", "eating"), ("eating", "bananas")]
If the input sentence consists of fewer than two words, then
`word_pairs` should output the empty list.
**Be careful:** the result should be a list of tuples, not a list
of lists.
def word_pairs(sentence):
# YOUR CODE HERE
# Base cases. 5 points.
check_equal(word_pairs(" "), [])
check_equal(word_pairs("woohoo"), [])
check_equal(word_pairs("I am"), [("I", "am")])
def common_word_pairs(sentence1, sentence2):
"""Returns the set of common consecutive word pairs in two sentences."""
# YOUR CODE HERE
s1 = "I love bananas"
s2 = "I love to eat bananas"
s3 = "Nobody truly dislikes to eat bananas"
s4 = "I love to eat anything but bananas"
s5 = "I like mangos more than bananas"
check_equal(common_word_pairs(s1, s2), {("I", "love")})
check_equal(common_word_pairs(s2, s3), {("to", "eat"), ("eat", "bananas")})
check_equal(common_word_pairs(s3, s4), {("to", "eat")})
check_equal(common_word_pairs(s1, s5), set())
Solution:
The functions are written in python 3
Code for word_pairs()
def word_pairs(sentence):
words=sentence.split()
wordpairs=[]
for i in range(0,len(words)-1):
x=[]
x.append(words[i])
x.append(words[i+1])
y=tuple(x)
wordpairs.append(y)
return wordpairs
Code for common_word_pairs()
def common_word_pairs(sentence1, sentence2):
"""Returns the set of common consecutive word pairs in two sentences."""
# YOUR CODE HERE
word_pairs1=word_pairs(sentence1)
word_pairs2=word_pairs(sentence2)
common = set([value for value in word_pairs1 if value in word_pairs2])
return common
Complete code:
def check_equal(x, y, msg=None):
if x == y:
if msg is None:
print("Success")
else:
print(msg, ": Success")
else:
if msg is None:
print("Error:")
else:
print("Error in", msg, ":")
print(" Your answer was:", x)
print(" Correct answer: ", y)
assert x == y, "%r and %r are different" % (x, y)
def word_pairs(sentence):
words=sentence.split()
wordpairs=[]
for i in range(0,len(words)-1):
x=[]
x.append(words[i])
x.append(words[i+1])
y=tuple(x)
wordpairs.append(y)
return wordpairs
check_equal(word_pairs(" "), [])
check_equal(word_pairs("woohoo"), [])
check_equal(word_pairs("I am"), [("I", "am")])
def common_word_pairs(sentence1, sentence2):
"""Returns the set of common consecutive word pairs in two sentences."""
# YOUR CODE HERE
word_pairs1=word_pairs(sentence1)
word_pairs2=word_pairs(sentence2)
common = set([value for value in word_pairs1 if value in word_pairs2])
return common
s1 = "I love bananas"
s2 = "I love to eat bananas"
s3 = "Nobody truly dislikes to eat bananas"
s4 = "I love to eat anything but bananas"
s5 = "I like mangos more than bananas"
check_equal(common_word_pairs(s1, s2), {("I", "love")})
check_equal(common_word_pairs(s2, s3), {("to", "eat"), ("eat", "bananas")})
check_equal(common_word_pairs(s3, s4), {("to", "eat")})
check_equal(common_word_pairs(s1, s5), set())
Screenshot of output: