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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import math
from uuid import uuid4
from datetime import datetime
from collections import deque, defaultdict

POST_LIMIT = 50


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class UserCannotFollowSelf(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass


class User:

    def __init__(self, name):
        self.full_name = name
        self.uuid = uuid4()
        self.posts = deque([], POST_LIMIT)
        self.follows = []

    def add_post(self, post_content):
        if len(self.posts) == POST_LIMIT:
            self.posts.popleft()
        self.posts.append(Post(self.uuid, post_content))

    def get_post(self):
        index = 0
        while index < len(self.posts):
            yield self.posts[index]
            index += 1


class Post:

    def __init__(self, author_uuid, post_content):
        self.author = author_uuid
        self.published_at = datetime.now()
        self.content = post_content


class SocialGraph:

    def __init__(self):
        self.users = defaultdict(User)
        self.graph = defaultdict(list)

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

        self.users[user.uuid] = user

    def get_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError

        return self.users[user_uuid]

    def delete_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        for user in self.graph:
            if user_uuid in self.graph[user]:
                self.graph[user].remove(user_uuid)
        del self.users[user_uuid]

    def follow(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError
        if follower == followee:
            raise UserCannotFollowSelf
        if followee in self.graph[follower]:
            return
        self.graph[follower].append(followee)

    def unfollow(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError
        if followee not in self.graph[follower]:
            return
        self.graph[follower].remove(followee)

    def is_following(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError
        if followee in self.graph[follower]:
            return True
        return False

    def following(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        return set(self.graph[user_uuid])

    def followers(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        followers = []
        for user in self.graph:
            if user_uuid in self.graph[user]:
                followers.append(user)
        return set(followers)

    def friends(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        friends = []
        for user in list(self.graph.keys()):
            if user_uuid in self.graph[user] and user in self.graph[user_uuid]:
                friends.append(user)
        return set(friends)

    def min_distance(self, from_user_uuid, to_user_uuid):
        if from_user_uuid not in self.users or to_user_uuid not in self.users:
            raise UserDoesNotExistError
        min_dist = self._find_shortest_path(from_user_uuid, to_user_uuid)
        if min_dist == math.inf:
            raise UsersNotConnectedError
        return min_dist

    def max_distance(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        if not self.graph[user_uuid]:
            return math.inf
        max_distance = -1
        for user in list(self.users.keys()):
            if user == user_uuid:
                continue
            current_distance = self._find_shortest_path(user_uuid, user)
            if current_distance != math.inf and current_distance > max_distance:
                max_distance = current_distance
        if max_distance == -1:
            return math.inf
        return max_distance

    def _find_shortest_path(self, from_user_uuid, to_user_uuid, path=[]):
        if from_user_uuid == to_user_uuid:
            return 0

        queue = deque([])
        visited = []
        queue.append([from_user_uuid])
        while queue:
            path = queue.popleft()
            node = path[-1]
            if node == to_user_uuid:
                return len(path) - 1
            for neighbour in self.graph[node]:
                if neighbour not in visited:
                    visited.append(node)
                    new_path = list(path)
                    new_path.append(neighbour)
                    queue.append(new_path)
            visited.append(node)
        return math.inf

    def nth_layer_followings(self, user_uuid, n, visited=[]):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        visited = visited + [user_uuid]
        if n == 0:
            return []
        if n == 1:
            return set([user for user in self.graph[user_uuid] if user not in visited])
        if self.max_distance(user_uuid) < n:
            return set([])
        layer = []
        visited.extend(self.graph[user_uuid])
        for followee in self.graph[user_uuid]:
            new_layer = self.nth_layer_followings(followee, n - 1, visited)
            layer.extend(new_layer)
        return set(layer)

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        posts = [post for followee in self.graph[user_uuid]
                 for post in self.users[followee].posts]
        posts.sort(key=lambda post: post.published_at, reverse=True)
        return posts[offset:offset + limit]

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.077s

OK

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

Кристина обнови решението на 18.04.2016 01:11 (преди над 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import math
from uuid import uuid4
from datetime import datetime
from collections import deque, defaultdict

POST_LIMIT = 50


class UserAlreadyExistsError(Exception):

    def __init__(self, message):
        self.message = message

    def __str__(self):
        return repr(self.message)


class UserDoesNotExistError(Exception):

    def __init__(self, message):
        self.message = message

    def __str__(self):
        return repr(self.message)


class UserCannotFollowSelf(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass


class User:

    def __init__(self, name):
        self.full_name = name
        self.uuid = uuid4()
        self.posts = deque([], POST_LIMIT)
        self.follows = []

    def add_post(self, post_content):
        if len(self.posts) == POST_LIMIT:
            self.posts.popleft()
        self.posts.append(Post(self.uuid, post_content))

    def get_post(self):
        index = 0
        while index < len(self.posts):
            yield self.posts[index]
            index += 1

    def __str__(self):
        return "Full name: {}, UUID: {}".format(self.full_name, self.uuid)


class Post:

    def __init__(self, author_uuid, post_content):
        self.author = author_uuid
        self.published_at = datetime.now()
        self.content = post_content

    def __str__(self):
        return "Published by {0} at {1}: \n {2} ".format(self.author, self.published_at, self.content)


class SocialGraph:

    def __init__(self):
        self.users = defaultdict(User)
        self.graph = defaultdict(list)

    def add_user(self, user):
        if user.uuid in self.users:
            raise UserAlreadyExistsError("This guy is already here!")

        self.users[user.uuid] = user

    def get_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")

        return self.users[user_uuid]

    def delete_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        for user in self.graph:
            if user_uuid in self.graph[user]:
                self.graph[user].remove(user_uuid)
        del self.users[user_uuid]

    def follow(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        if follower == followee:
            raise UserCannotFollowSelf(
                "Sorry, a person cannot follow themselves! Maybe you mean someone else.")
        self.graph[follower].append(followee)

    def unfollow(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        self.graph[follower].remove(followee)

    def is_following(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        if followee in self.graph[follower]:
            return True
        return False

    def following(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        return set(self.graph[user_uuid])

    def followers(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        followers = []
        for user in self.graph:
            if user_uuid in self.graph[user]:
                followers.append(user)
        return set(followers)

    def friends(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        friends = []
        for user in list(self.graph.keys()):
            if user_uuid in self.graph[user] and user in self.graph[user_uuid]:
                friends.append(user)
        return set(friends)

    def min_distance(self, from_user_uuid, to_user_uuid):
        if from_user_uuid not in self.users or to_user_uuid not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        min_dist = self._find_shortest_path(from_user_uuid, to_user_uuid)
        if min_dist == math.inf:
            raise UsersNotConnectedError("Sorry, these guys aren't connected")
        return min_dist

    def max_distance(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        if not self.graph[user_uuid]:
            return math.inf
        max_distance = -1
        for user in list(self.users.keys()):
            current_distance = self._find_shortest_path(user_uuid, user)
            if current_distance != math.inf and current_distance > max_distance:
                max_distance = current_distance
        return max_distance

    def _find_shortest_path(self, from_user_uuid, to_user_uuid, path=[]):
        if from_user_uuid == to_user_uuid:
            return 0

        queue = deque([])
        visited = []
        queue.append([from_user_uuid])
        while queue:
            path = queue.popleft()
            node = path[-1]
            if node == to_user_uuid:
                return len(path) - 1
            for neighbour in self.graph[node]:
                if neighbour not in visited:
                    visited.append(node)
                    new_path = list(path)
                    new_path.append(neighbour)
                    queue.append(new_path)
            visited.append(node)
        return math.inf

    def nth_layer_followings(self, user_uuid, n, visited=[]):
        if user_uuid not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        visited = visited + [user_uuid]
        if n == 0:
            return []
        if n == 1:
            return [user for user in self.graph[user_uuid] if user not in visited]
        layer = []
        for followee in self.graph[user_uuid]:
            if followee not in visited:
                visited.append(followee)
                new_layer = self.nth_layer_followings(followee, n - 1, visited)
                layer.extend(new_layer)
            else:
                break
        return layer

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if user_uuid not in self.users:
            raise UserDoesNotExistError("Ops, maybe you meant someone else..")
        posts = [post for followee in self.graph[user_uuid]
                 for post in self.users[followee].posts]
        posts.sort(key=lambda post: post.published_at, reverse=True)
        return posts[offset:offset + limit]

Кристина обнови решението на 18.04.2016 16:24 (преди над 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import math
from uuid import uuid4
from datetime import datetime
from collections import deque, defaultdict

POST_LIMIT = 50


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class UserCannotFollowSelf(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass


class User:

    def __init__(self, name):
        self.full_name = name
        self.uuid = uuid4()
        self.posts = deque([], POST_LIMIT)
        self.follows = []

    def add_post(self, post_content):
        if len(self.posts) == POST_LIMIT:
            self.posts.popleft()
        self.posts.append(Post(self.uuid, post_content))

    def get_post(self):
        index = 0
        while index < len(self.posts):
            yield self.posts[index]
            index += 1


class Post:

    def __init__(self, author_uuid, post_content):
        self.author = author_uuid
        self.published_at = datetime.now()
        self.content = post_content


class SocialGraph:

    def __init__(self):
        self.users = defaultdict(User)
        self.graph = defaultdict(list)

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

        self.users[user.uuid] = user

    def get_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError

        return self.users[user_uuid]

    def delete_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        for user in self.graph:
            if user_uuid in self.graph[user]:
                self.graph[user].remove(user_uuid)
        del self.users[user_uuid]

    def follow(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError
        if follower == followee:
            raise UserCannotFollowSelf
        self.graph[follower].append(followee)

    def unfollow(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError
        self.graph[follower].remove(followee)

    def is_following(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError
        if followee in self.graph[follower]:
            return True
        return False

    def following(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        return set(self.graph[user_uuid])

    def followers(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        followers = []
        for user in self.graph:
            if user_uuid in self.graph[user]:
                followers.append(user)
        return set(followers)

    def friends(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        friends = []
        for user in list(self.graph.keys()):
            if user_uuid in self.graph[user] and user in self.graph[user_uuid]:
                friends.append(user)
        return set(friends)

    def min_distance(self, from_user_uuid, to_user_uuid):
        if from_user_uuid not in self.users or to_user_uuid not in self.users:
            raise UserDoesNotExistError
        min_dist = self._find_shortest_path(from_user_uuid, to_user_uuid)
        if min_dist == math.inf:
            raise UsersNotConnectedError
        return min_dist

    def max_distance(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        if not self.graph[user_uuid]:
            return math.inf
        max_distance = -1
        for user in list(self.users.keys()):
            if user == user_uuid:
                continue
            current_distance = self._find_shortest_path(user_uuid, user)
            if current_distance != math.inf and current_distance > max_distance:
                max_distance = current_distance
        if max_distance == -1:
            return math.inf
        return max_distance

    def _find_shortest_path(self, from_user_uuid, to_user_uuid, path=[]):
        if from_user_uuid == to_user_uuid:
            return 0

        queue = deque([])
        visited = []
        queue.append([from_user_uuid])
        while queue:
            path = queue.popleft()
            node = path[-1]
            if node == to_user_uuid:
                return len(path) - 1
            for neighbour in self.graph[node]:
                if neighbour not in visited:
                    visited.append(node)
                    new_path = list(path)
                    new_path.append(neighbour)
                    queue.append(new_path)
            visited.append(node)
        return math.inf

    def nth_layer_followings(self, user_uuid, n, visited=[]):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        visited = visited + [user_uuid]
        if n == 0:
            return []
        if n == 1:
            return set([user for user in self.graph[user_uuid] if user not in visited])
        if self.max_distance(user_uuid) < n:
            return set([])
        layer = []
        visited.extend(self.graph[user_uuid])
        for followee in self.graph[user_uuid]:
            new_layer = self.nth_layer_followings(followee, n - 1, visited)
            layer.extend(new_layer)
        return set(layer)

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        posts = [post for followee in self.graph[user_uuid]
                 for post in self.users[followee].posts]
        posts.sort(key=lambda post: post.published_at, reverse=True)
        return posts[offset:offset + limit]

Кристина обнови решението на 18.04.2016 16:40 (преди над 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import math
from uuid import uuid4
from datetime import datetime
from collections import deque, defaultdict

POST_LIMIT = 50


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class UserCannotFollowSelf(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass


class User:

    def __init__(self, name):
        self.full_name = name
        self.uuid = uuid4()
        self.posts = deque([], POST_LIMIT)
        self.follows = []

    def add_post(self, post_content):
        if len(self.posts) == POST_LIMIT:
            self.posts.popleft()
        self.posts.append(Post(self.uuid, post_content))

    def get_post(self):
        index = 0
        while index < len(self.posts):
            yield self.posts[index]
            index += 1


class Post:

    def __init__(self, author_uuid, post_content):
        self.author = author_uuid
        self.published_at = datetime.now()
        self.content = post_content


class SocialGraph:

    def __init__(self):
        self.users = defaultdict(User)
        self.graph = defaultdict(list)

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

        self.users[user.uuid] = user

    def get_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError

        return self.users[user_uuid]

    def delete_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        for user in self.graph:
            if user_uuid in self.graph[user]:
                self.graph[user].remove(user_uuid)
        del self.users[user_uuid]

    def follow(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError
        if follower == followee:
            raise UserCannotFollowSelf
        if followee in self.graph[follower]:
            return
        self.graph[follower].append(followee)

    def unfollow(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError
        if followee not in self.graph[follower]:
            return
        self.graph[follower].remove(followee)

    def is_following(self, follower, followee):
        if follower not in self.users or followee not in self.users:
            raise UserDoesNotExistError
        if followee in self.graph[follower]:
            return True
        return False

    def following(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        return set(self.graph[user_uuid])

    def followers(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        followers = []
        for user in self.graph:
            if user_uuid in self.graph[user]:
                followers.append(user)
        return set(followers)

    def friends(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        friends = []
        for user in list(self.graph.keys()):
            if user_uuid in self.graph[user] and user in self.graph[user_uuid]:
                friends.append(user)
        return set(friends)

    def min_distance(self, from_user_uuid, to_user_uuid):
        if from_user_uuid not in self.users or to_user_uuid not in self.users:
            raise UserDoesNotExistError
        min_dist = self._find_shortest_path(from_user_uuid, to_user_uuid)
        if min_dist == math.inf:
            raise UsersNotConnectedError
        return min_dist

    def max_distance(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        if not self.graph[user_uuid]:
            return math.inf
        max_distance = -1
        for user in list(self.users.keys()):
            if user == user_uuid:
                continue
            current_distance = self._find_shortest_path(user_uuid, user)
            if current_distance != math.inf and current_distance > max_distance:
                max_distance = current_distance
        if max_distance == -1:
            return math.inf
        return max_distance

    def _find_shortest_path(self, from_user_uuid, to_user_uuid, path=[]):
        if from_user_uuid == to_user_uuid:
            return 0

        queue = deque([])
        visited = []
        queue.append([from_user_uuid])
        while queue:
            path = queue.popleft()
            node = path[-1]
            if node == to_user_uuid:
                return len(path) - 1
            for neighbour in self.graph[node]:
                if neighbour not in visited:
                    visited.append(node)
                    new_path = list(path)
                    new_path.append(neighbour)
                    queue.append(new_path)
            visited.append(node)
        return math.inf

    def nth_layer_followings(self, user_uuid, n, visited=[]):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        visited = visited + [user_uuid]
        if n == 0:
            return []
        if n == 1:
            return set([user for user in self.graph[user_uuid] if user not in visited])
        if self.max_distance(user_uuid) < n:
            return set([])
        layer = []
        visited.extend(self.graph[user_uuid])
        for followee in self.graph[user_uuid]:
            new_layer = self.nth_layer_followings(followee, n - 1, visited)
            layer.extend(new_layer)
        return set(layer)

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if user_uuid not in self.users:
            raise UserDoesNotExistError
        posts = [post for followee in self.graph[user_uuid]
                 for post in self.users[followee].posts]
        posts.sort(key=lambda post: post.published_at, reverse=True)
        return posts[offset:offset + limit]