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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import uuid
import datetime
import math


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.__posts = []
        self.follows = set()
        self.followers = set()

    def add_post(self, post_content):
        post = Post(self, post_content)
        self.__posts.append(post)
        if len(self.__posts) == 51:
            del self.__posts[0]

    def get_post(self):
        return iter(self.__posts)

    def follow(self, user):
        self.follows.add(user)

    def unfollow(self, user):
        self.follows.remove(user)

    def add_follower(self, user):
        self.followers.add(user)

    def remove_follower(self, user):
        self.followers.remove(user)


class Post:
    def __init__(self, user, post_content):
        self.author = user.uuid
        self.published_at = datetime.datetime.now()
        self.content = post_content


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class SocialGraph:
    def __init__(self):
        self.__users = {}

    def __check_user_existence(self, user_uuid):
        if user_uuid not in self.__users.keys():
            raise UserDoesNotExistError

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

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

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

    def follow(self, follower, followee):
        self.__check_user_existence(follower)
        self.__check_user_existence(followee)
        self.__users[follower].follow(followee)
        self.__users[followee].add_follower(follower)

    def unfollow(self, follower, followee):
        self.__check_user_existence(follower)
        self.__check_user_existence(followee)
        self.__users[follower].unfollow(followee)
        self.__users[followee].remove_follower(follower)

    def is_following(self, follower, followee):
        self.__check_user_existence(follower)
        self.__check_user_existence(followee)
        return follower in self.__users[followee].followers

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

    def following(self, user_uuid):
        self.__check_user_existence(user_uuid)
        return self.__users[user_uuid].follows

    def friends(self, user_uuid):
        self.__check_user_existence(user_uuid)
        user = self.__users[user_uuid]
        return user.followers.intersection(user.follows)

    def get_next_layer(self, visited, layer):
        layer = {next for uuid in layer for next in self.__users[uuid].follows}
        return layer.difference(visited)

    def max_distance(self, user_uuid):
        self.__check_user_existence(user_uuid)
        visited = set()
        visited.add(user_uuid)
        layer = set()
        layer.add(user_uuid)
        count = -1
        while layer:
            count += 1
            layer = self.get_next_layer(visited, layer)
            visited = visited.union(layer)
        return count

    def min_distance(self, from_user_uuid, to_user_uuid):
        self.__check_user_existence(from_user_uuid)
        self.__check_user_existence(to_user_uuid)
        visited = set()
        visited.add(from_user_uuid)
        layer = set()
        layer.add(from_user_uuid)
        count = 0
        while layer:
            count += 1
            if to_user_uuid in layer:
                return count - 1
            layer = self.get_next_layer(visited, layer)
            visited = visited.union(layer)
        return math.inf

    def nth_layer_followings(self, user_uuid, n):
        self.__check_user_existence(user_uuid)
        visited = set()
        visited.add(user_uuid)
        layer = set()
        layer.add(user_uuid)
        count = 0
        while layer:
            if count == n:
                return layer
            count += 1
            layer = self.get_next_layer(visited, layer)
            visited = visited.union(layer)
        return layer

    def generate_feed(self, user_uuid, offset=0, limit=10):
        self.__check_user_existence(user_uuid)
        follows = self.__users[user_uuid].follows

        def posts_per_user(user_uuid):
            return self.__users[user_uuid].get_post()

        posts = [post for user in follows for post in posts_per_user(user)]
        posts = reversed(sorted(posts, key=lambda post: post.published_at))
        end = offset + limit
        return list(posts)[offset:end]

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.064s

OK

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

Денис обнови решението на 18.04.2016 02:32 (преди над 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import uuid
import datetime
import math


class User:

    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.__posts = []
        self.follows = set()
        self.followers = set()

    def add_post(self, post_content):
        post = Post(self, post_content)
        self.__posts.append(post)
        if len(self.__posts) == 51:
            del self.__posts[0]

    def get_post(self):
        return iter(self.__posts)

    def follow(self, user):
        self.follows.add(user)

    def unfollow(self, user):
        self.follows.remove(user)

    def add_follower(self, user):
        self.followers.add(user)

    def remove_follower(self, user):
        self.followers.remove(user)


class Post:
    def __init__(self, user, post_content):
        self.author = user.uuid
        self.published_at = datetime.datetime.now()
        self.content = post_content


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class SocialGraph:
    def __init__(self):
        self.__users = {}

    def __check_user_existence(self, user_uuid):
        if user_uuid not in self.__users.keys():
            raise UserDoesNotExistError

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

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

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

    def follow(self, follower, followee):
        self.__check_user_existence(follower)
        self.__check_user_existence(followee)
        self.__users[follower].follow(followee)
        self.__users[followee].add_follower(follower)

    def unfollow(self, follower, followee):
        self.__check_user_existence(follower)
        self.__check_user_existence(followee)
        self.__users[follower].unfollow(followee)
        self.__users[followee].remove_follower(follower)

    def is_following(self, follower, followee):
        self.__check_user_existence(follower)
        self.__check_user_existence(followee)
        return follower in self.__users[followee].followers

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

    def following(self, user_uuid):
        self.__check_user_existence(user_uuid)
        return self.__users[user_uuid].follows

    def friends(self, user_uuid):
        self.__check_user_existence(user_uuid)
        user = self.__users[user_uuid]
        return user.followers.intersection(user.follows)

    def get_next_layer(self, visited, layer):
        layer = {next for uuid in layer for next in self.__users[uuid].follows}
        return layer.difference(visited)

    def max_distance(self, user_uuid):
        self.__check_user_existence(user_uuid)
        visited = set()
        visited.add(user_uuid)
        layer = set()
        layer.add(user_uuid)
        count = -1
        while layer:
            count += 1
            layer = self.get_next_layer(visited, layer)
            visited = visited.union(layer)
        return count

    def min_distance(self, from_user_uuid, to_user_uuid):
        self.__check_user_existence(from_user_uuid)
        self.__check_user_existence(to_user_uuid)
        visited = set()
        visited.add(from_user_uuid)
        layer = set()
        layer.add(from_user_uuid)
        count = 0
        while layer:
            count += 1
            if to_user_uuid in layer:
                return count - 1
            layer = self.get_next_layer(visited, layer)
            visited = visited.union(layer)
        return math.inf

    def nth_layer_followings(self, user_uuid, n):
        self.__check_user_existence(user_uuid)
        visited = set()
        visited.add(user_uuid)
        layer = set()
        layer.add(user_uuid)
        count = 0
        while layer:
            if count == n:
                return layer
            count += 1
            layer = self.get_next_layer(visited, layer)
            visited = visited.union(layer)
        return layer

    def generate_feed(self, user_uuid, offset=0, limit=10):
        self.__check_user_existence(user_uuid)
        follows = self.__users[user_uuid].follows

        def posts_per_user(user_uuid):
            return self.__users[user_uuid].get_post()

        posts = [post for user in follows for post in posts_per_user(user)]
        posts = reversed(sorted(posts, key=lambda post: post.published_at))
        end = offset + limit
        return list(posts)[offset:end]

Денис обнови решението на 18.04.2016 02:33 (преди над 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import uuid
import datetime
import math


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.__posts = []
        self.follows = set()
        self.followers = set()

    def add_post(self, post_content):
        post = Post(self, post_content)
        self.__posts.append(post)
        if len(self.__posts) == 51:
            del self.__posts[0]

    def get_post(self):
        return iter(self.__posts)

    def follow(self, user):
        self.follows.add(user)

    def unfollow(self, user):
        self.follows.remove(user)

    def add_follower(self, user):
        self.followers.add(user)

    def remove_follower(self, user):
        self.followers.remove(user)


class Post:
    def __init__(self, user, post_content):
        self.author = user.uuid
        self.published_at = datetime.datetime.now()
        self.content = post_content


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class SocialGraph:
    def __init__(self):
        self.__users = {}

    def __check_user_existence(self, user_uuid):
        if user_uuid not in self.__users.keys():
            raise UserDoesNotExistError

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

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

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

    def follow(self, follower, followee):
        self.__check_user_existence(follower)
        self.__check_user_existence(followee)
        self.__users[follower].follow(followee)
        self.__users[followee].add_follower(follower)

    def unfollow(self, follower, followee):
        self.__check_user_existence(follower)
        self.__check_user_existence(followee)
        self.__users[follower].unfollow(followee)
        self.__users[followee].remove_follower(follower)

    def is_following(self, follower, followee):
        self.__check_user_existence(follower)
        self.__check_user_existence(followee)
        return follower in self.__users[followee].followers

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

    def following(self, user_uuid):
        self.__check_user_existence(user_uuid)
        return self.__users[user_uuid].follows

    def friends(self, user_uuid):
        self.__check_user_existence(user_uuid)
        user = self.__users[user_uuid]
        return user.followers.intersection(user.follows)

    def get_next_layer(self, visited, layer):
        layer = {next for uuid in layer for next in self.__users[uuid].follows}
        return layer.difference(visited)

    def max_distance(self, user_uuid):
        self.__check_user_existence(user_uuid)
        visited = set()
        visited.add(user_uuid)
        layer = set()
        layer.add(user_uuid)
        count = -1
        while layer:
            count += 1
            layer = self.get_next_layer(visited, layer)
            visited = visited.union(layer)
        return count

    def min_distance(self, from_user_uuid, to_user_uuid):
        self.__check_user_existence(from_user_uuid)
        self.__check_user_existence(to_user_uuid)
        visited = set()
        visited.add(from_user_uuid)
        layer = set()
        layer.add(from_user_uuid)
        count = 0
        while layer:
            count += 1
            if to_user_uuid in layer:
                return count - 1
            layer = self.get_next_layer(visited, layer)
            visited = visited.union(layer)
        return math.inf

    def nth_layer_followings(self, user_uuid, n):
        self.__check_user_existence(user_uuid)
        visited = set()
        visited.add(user_uuid)
        layer = set()
        layer.add(user_uuid)
        count = 0
        while layer:
            if count == n:
                return layer
            count += 1
            layer = self.get_next_layer(visited, layer)
            visited = visited.union(layer)
        return layer

    def generate_feed(self, user_uuid, offset=0, limit=10):
        self.__check_user_existence(user_uuid)
        follows = self.__users[user_uuid].follows

        def posts_per_user(user_uuid):
            return self.__users[user_uuid].get_post()

        posts = [post for user in follows for post in posts_per_user(user)]
        posts = reversed(sorted(posts, key=lambda post: post.published_at))
        end = offset + limit
        return list(posts)[offset:end]