最常见的Tweet路径

2024-04-25 20:03:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试解决一个问题,从日志中读取所有tweet视图并查找频繁出现的tweet路径 我能够用python编写代码,通过12个测试用例中的7个。测试用例对我来说是隐藏的,所以我无法真正检查我的不足之处。附件是我的一些解决方案可以提供我的意见,为地区改善?我非常感谢您的时间和帮助。你知道吗

Problem explained in detail:
A log where each line represents a tweet viewed by a user. A “tweet view” is a user’s handle, followed by a comma, followed by the tweet id. Three tweets views in a row by the same user is “tweet View path”
Example 
@Biz,T1
@Jack,T7
@Biz,T2
@Biz,T5

Here we can see Biz had a tweet view path of T1,T2,T5. Jack did not view enough tweets to create a tweet view path.

Constraints
1)  Input should be read from stdin and the result should be written to stdout.
2)  If there is a single most common tweet path, out it in the following format:
T1,T2,T3

3)  If there is more than one most common tweet path, print each one on a new line in lexicographical order.. for example if T1,T3,T4 and T1,T2,T3 and T1,T3,T2 are each the most viewed path then output would be :
T1,T2,T3
T1,T3,T2
T1,T3,T4

4)  If there is no longest path then only output “No Tweet path found!”
5)  If the input is malformed, including empty, then only output “log malformed!”

import collections
import operator
import sys

class FindMostCommonTweetViewPath:
    # TODO implement your solution to find the most common tweet view path
    def __init__(self):
        self.tweet_log = {}
        self.tweet_view_path = {}

    def logTweet(self, userid, tweetid):
        if userid not in self.tweet_log:
            self.tweet_log[userid] = [tweetid]
        else:
            self.tweet_log[userid].append(tweetid)

    def printlog(self):
        print(self.tweet_log)
        print(self.tweet_view_path)

    def capture_view_path(self, path):
        if path != '':
            if path not in self.tweet_view_path:
                self.tweet_view_path[path] = 1
            else:
                self.tweet_view_path[path] = self.tweet_view_path.get(path) + 1

    def store_tweet_view_path(self):
        if self.tweet_log:
            for tweets in self.tweet_log.values():
                view_path = ''
                tweetlen = len(tweets)
                if tweetlen == 3:
                    view_path = ','.join(str(e) for e in tweets[:tweetlen])
                    self.capture_view_path(view_path)
                elif tweetlen > 3:
                    for i in range(0, len(tweets)):
                        cnt = i + 3
                        view_path = ','.join(str(e) for e in tweets[i:cnt])
                        self.capture_view_path(view_path)
        else:
            print('No tweet path found!', end='')

    def get_frequent_path(self):
        if self.tweet_view_path:
            maxval = max(self.tweet_view_path.items(), key=lambda x: x[1])
            listof_fequentpath = list()

            for keys,val in self.tweet_view_path.items():
                if val == maxval[1]:
                    listof_fequentpath.append(keys)

            listof_fequentpath.sort()

            for freqpath in listof_fequentpath:
                if freqpath != '':
                    print(freqpath)

        else:
            print('No tweet path found!', end='')


if __name__ == '__main__':
    # TODO Read input from stdin
    # TODO Call the FindMostCommonTweetViewPath to find results
    # TODO Print results
    tw = FindMostCommonTweetViewPath()
    proceed = True

    while True:
        try:
            tweet_input = str(input().strip())
        except EOFError:
            break

        if tweet_input != '' and ',' in tweet_input:

            userid , tweetid = tweet_input.split(',')
            tw.logTweet(userid.strip(), tweetid.strip())
            if userid == '' or tweetid == '':
                print('Log malformed!', end='')
                proceed = False
                break

        elif tw.tweet_log is None and tweet_input == '':
            print('Log malformed!', end='')
            proceed = False
            break

        elif ',' not in tweet_input and tw.tweet_log is not None:
            print('Log malformed!', end='')
            proceed = False
            break
        else:
            print('Log malformed!', end='')
            break

    if tw.tweet_log and proceed:
        tw.store_tweet_view_path()
        tw.get_frequent_path()
        #tw.printlog()

Tags: thepathinselflogviewinputif