Notice: Write a Python function by using abstract list functions! Note: You must
ID: 3595001 • Letter: N
Question
Notice: Write a Python function by using abstract list functions!
Note: You must not use explicit recursion for this question, nor loops. You may use abstract list functions though. In Twitter, it is possible to search through tweets for specific tweeter. For the purpose of this question, we will represent a Twitter tweet (containing the tweet number, the tweeter name, and the body) as a single string with the following format. "#N.-@name:-Body". (You may assume that" appears only twice in a tweet) For example, "#1:-@DanClark:-The party was amazing" corresponds to tweet #1, the tweeter name is "DanClark", and the body is "The party was amazing" You may assume that N>0, the tweeter and the body are non-empty. Write a Python function search_tweets, that consumes tweets, a list of strings in the format described above, and tweeter, a nonempty string, and returns a list of Nat, containing the tweet numbers of every string in tweets which the sender name is tweeter.Explanation / Answer
Code:
import re
def search_tweets(tweets, name):
result = []
for i in tweets:
numMatch = re.match(r"^.*#(.*):-@.*$",i).group(1)
nameMatch = re.match(r"^.*@(.*):.*$",i).group(1)
if(nameMatch == name):
result.append(int(numMatch))
return result
tweets = ["#1:-@DanClark:-The party was amazing", "#19:-@NatalyS:-Avoid 901 Toronto area at this time", "#50:-@CBCNews:-How Canadian captain gave her team a speech", "#14:-@DanClark:-The food was good", "#15:-@DaveLin:-Lucky you DanClark"]
name = "DanClark"
print search_tweets(tweets, name)
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.