timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Социална мрежа от Марио Даскалов

Обратно към всички решения

Към профила на Марио Даскалов

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 8 успешни тест(а)
  • 0 неуспешни тест(а)

Код

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import uuid
import datetime
import math
from collections import deque, defaultdict


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.__posts = deque(maxlen=50)

    def add_post(self, post_content):
        post = Post(self.uuid, datetime.datetime.now(), post_content)
        self.__posts.append(post)

    def get_post(self):
        for post in self.__posts:
            yield post


class Post:
    def __init__(self, author, published_at, content):
        self.author = author
        self.published_at = published_at
        self.content = content

    def published_at(self):
        return self.published_at


class SocialGraph:
    def __check_user_existence(func):
        def checked_func(self, user_uuid):
            if user_uuid not in self.__users:
                raise UserDoesNotExistError

            return func(self, user_uuid)

        return checked_func

    def __init__(self):
        self.__users = {}
        self.__user_followers = defaultdict(set)
        self.__user_following = defaultdict(set)

    def add_user(self, user):
        if user.uuid in self.__users:
            raise UserAlreadyExistsError

        self.__users[user.uuid] = user

    @__check_user_existence
    def get_user(self, user_uuid):
        return self.__users[user_uuid]

    @__check_user_existence
    def delete_user(self, user_uuid):
        del self.__users[user_uuid]

    def follow(self, follower, followee):
        self.__user_following[follower].add(followee)
        self.__user_followers[followee].add(follower)

    def unfollow(self, follower, followee):
        self.__user_following[follower].discard(followee)
        self.__user_followers[followee].discard(follower)

    def is_following(self, follower, followee):
        return followee in self.__user_following[follower]

    def followers(self, user_uuid):
        return self.__user_followers[user_uuid]

    def following(self, user_uuid):
        return self.__user_following[user_uuid]

    def friends(self, user_uuid):
        return self.followers(user_uuid) & self.following(user_uuid)

    def max_distance(self, user_uuid):
        bfs_result = self.__bfs(user_uuid)
        return bfs_result[-1][0]

    def min_distance(self, from_user_uuid, to_user_uuid):
        distance = math.inf
        for level, user_uuid in self.__bfs(from_user_uuid):
            if user_uuid == to_user_uuid:
                distance = level
                break

        return distance

    def nth_layer_followings(self, user_uuid, n):
        return [
            current_user_uuid
            for level, current_user_uuid in self.__bfs(user_uuid)
            if level == n
        ]

    def generate_feed(self, user_uuid, offset=0, limit=10):
        users = [
            self.__users[followee] for followee in self.following(user_uuid)
        ]

        return sorted(
            [post for user in users for post in user.get_post()],
            key=Post.published_at,
            reverse=True
        )[offset:][0:limit]

    def __bfs(self, start_vertex):
        result = []
        vertices = deque([(0, start_vertex)])
        visited = set()
        while len(vertices) > 0:
            level, current_vertex = vertices.popleft()
            if current_vertex not in visited:
                result.append((level, current_vertex))
                visited.add(current_vertex)
                neighbours = [
                    (level + 1, followee)
                    for followee in self.following(current_vertex)
                    if followee not in visited
                ]
                vertices.extend(neighbours)

        return result


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UserNotConnectedError(Exception):
    pass

Лог от изпълнението

........
----------------------------------------------------------------------
Ran 8 tests in 0.065s

OK

История (1 версия и 0 коментара)

Марио обнови решението на 18.04.2016 16:48 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import uuid
import datetime
import math
from collections import deque, defaultdict


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.__posts = deque(maxlen=50)

    def add_post(self, post_content):
        post = Post(self.uuid, datetime.datetime.now(), post_content)
        self.__posts.append(post)

    def get_post(self):
        for post in self.__posts:
            yield post


class Post:
    def __init__(self, author, published_at, content):
        self.author = author
        self.published_at = published_at
        self.content = content

    def published_at(self):
        return self.published_at


class SocialGraph:
    def __check_user_existence(func):
        def checked_func(self, user_uuid):
            if user_uuid not in self.__users:
                raise UserDoesNotExistError

            return func(self, user_uuid)

        return checked_func

    def __init__(self):
        self.__users = {}
        self.__user_followers = defaultdict(set)
        self.__user_following = defaultdict(set)

    def add_user(self, user):
        if user.uuid in self.__users:
            raise UserAlreadyExistsError

        self.__users[user.uuid] = user

    @__check_user_existence
    def get_user(self, user_uuid):
        return self.__users[user_uuid]

    @__check_user_existence
    def delete_user(self, user_uuid):
        del self.__users[user_uuid]

    def follow(self, follower, followee):
        self.__user_following[follower].add(followee)
        self.__user_followers[followee].add(follower)

    def unfollow(self, follower, followee):
        self.__user_following[follower].discard(followee)
        self.__user_followers[followee].discard(follower)

    def is_following(self, follower, followee):
        return followee in self.__user_following[follower]

    def followers(self, user_uuid):
        return self.__user_followers[user_uuid]

    def following(self, user_uuid):
        return self.__user_following[user_uuid]

    def friends(self, user_uuid):
        return self.followers(user_uuid) & self.following(user_uuid)

    def max_distance(self, user_uuid):
        bfs_result = self.__bfs(user_uuid)
        return bfs_result[-1][0]

    def min_distance(self, from_user_uuid, to_user_uuid):
        distance = math.inf
        for level, user_uuid in self.__bfs(from_user_uuid):
            if user_uuid == to_user_uuid:
                distance = level
                break

        return distance

    def nth_layer_followings(self, user_uuid, n):
        return [
            current_user_uuid
            for level, current_user_uuid in self.__bfs(user_uuid)
            if level == n
        ]

    def generate_feed(self, user_uuid, offset=0, limit=10):
        users = [
            self.__users[followee] for followee in self.following(user_uuid)
        ]

        return sorted(
            [post for user in users for post in user.get_post()],
            key=Post.published_at,
            reverse=True
        )[offset:][0:limit]

    def __bfs(self, start_vertex):
        result = []
        vertices = deque([(0, start_vertex)])
        visited = set()
        while len(vertices) > 0:
            level, current_vertex = vertices.popleft()
            if current_vertex not in visited:
                result.append((level, current_vertex))
                visited.add(current_vertex)
                neighbours = [
                    (level + 1, followee)
                    for followee in self.following(current_vertex)
                    if followee not in visited
                ]
                vertices.extend(neighbours)

        return result


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UserNotConnectedError(Exception):
    pass