timeit

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

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

Решение на Социална мрежа от Александър Чешмеджиев

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

Към профила на Александър Чешмеджиев

Резултати

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

Код

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
from uuid import uuid4
import datetime

MAXIMUM_POSTS_PER_USER = 50


class User:
        def __init__(self, full_name):
                self.full_name = full_name
                self.uuid = uuid4()
                self._posts = []

        def add_post(self, post_content):
                self._posts.append(Post(self.uuid, datetime.datetime.now(), post_content))
                if len(self._posts) > MAXIMUM_POSTS_PER_USER:
                        del self._posts[0]

        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


class SocialGraph:
        __social_graph = {}
        __users = [];

        def add_user(self, user):
                if user.uuid in SocialGraph.__social_graph:
                        raise UserAlreadyExistsError(
                                "User with uuid {0} already exists".format(user.uuid)
                        )

                SocialGraph.__social_graph[user.uuid] = []
                SocialGraph.__users.append(user)

        def get_user(self, user_uuid):
                filtered_users = list(filter(lambda u: u.uuid == user_uuid, 
                        SocialGraph.__users))

                if len(filtered_users) > 0:
                        return filtered_users[0]
                raise UserDoesNotExistsError(
                        "User with uuid {0} does not exist".format(user_uuid)
                )

        def delete_user(self, user_uuid):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                        "User with uuid {0} does not exist".format(user_uuid)
                        )
                user = self.get_user(user_uuid)
                SocialGraph.__users.remove(user)
                del SocialGraph.__social_graph[user_uuid]

        def follow(self, follower, followee):
                if followee not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Followee with uuid {0} does not exist".format(followee)
                        )

                if follower not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Follower with uuid {0} does not exist".format(follower)
                        )

                if follower not in SocialGraph.__social_graph[followee]:
                        SocialGraph.__social_graph[followee].append(follower)

        def unfollow(self, follower, followee):
                if followee not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Followee with uuid {0} does not exist".format(followee)
                        )

                if follower not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Follower with uuid {0} does not exist".format(follower)
                        )

                if follower in SocialGraph.__social_graph[followee]:
                        SocialGraph.__social_graph[followee].remove(follower)

        def is_following(self, follower, followee):
                if followee not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Followee with uuid {0} does not exist".format(followee)
                        )

                if follower not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Follower with uuid {0} does not exist".format(follower)
                        )

                return follower in SocialGraph.__social_graph[followee]

        def followers(self, user_uuid):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "User with uuid {0} does not exist".format(user_uuid)
                        )

                return set(SocialGraph.__social_graph[user_uuid])

        def following(self, user_uuid):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "User with uuid {0} does not exist".format(user_uuid)
                        )

                return set(
                        [followee_uuid
                        for followee_uuid, follower_uuids
                        in SocialGraph.__social_graph.items()
                        if user_uuid in follower_uuids])

        def friends(self, user_uuid):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "User with uuid {0} does not exist".format(user_uuid)
                        )

                return set(
                        [friend_uuid
                        for friend_uuid
                        in SocialGraph.__social_graph.keys()
                        if self.is_following(friend_uuid, user_uuid)
                                        and self.is_following(user_uuid, friend_uuid)])

        def max_distance(self, user_uuid, result=0):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "User with uuid {0} does not exist".format(user_uuid)
                        )

                user_followers = self.followers(user_uuid)
                distances = [self.__get_distance(follower_uuid)
                        for follower_uuid
                        in user_followers]
                distances.sort()
                return distances[-1]

        def generate_feed(self, user_uuid, offset=0, limit=10):
                sorted_posts = sorted([post
                        for followed_user_uuid
                        in self.following(user_uuid)
                                for post
                                in self.get_user(followed_user_uuid)._posts],
                                        key=lambda post: post.published_at)
                if len(sorted_posts) <= (limit + offset):
                        return sorted_posts[offset:]
                return sorted_posts[offset:limit + offset]

        def __get_distance(self, user_uuid, result=1):
                user_followers = self.followers(user_uuid)
                for follower_uuid in user_followers:
                        if len(SocialGraph.__social_graph[follower_uuid]) == 0:
                                continue
                        return self.__get_distance(follower_uuid, result + 1)
                return result


class UserDoesNotExistsError(Exception):
        def __init__(self, message):
                self.message = message


class UserAlreadyExistsError(Exception):
        def __init__(self, message):
                self.message = message

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

EEF..E..
======================================================================
ERROR: test_add_get_and_delete_user (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AttributeError: module 'solution' has no attribute 'UserDoesNotExistError'

======================================================================
ERROR: test_distnaces (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
IndexError: list index out of range

======================================================================
ERROR: test_layer_followings (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AttributeError: 'SocialGraph' object has no attribute 'nth_layer_followings'

======================================================================
FAIL: test_feed (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Lists differ: ['0', '10', '20', '30', '1', '11', '21', '31', '2', '12'] != ['39', '29', '19', '9', '38', '28', '18', '8', '37', '27']

First differing element 0:
0
39

- ['0', '10', '20', '30', '1', '11', '21', '31', '2', '12']
+ ['39', '29', '19', '9', '38', '28', '18', '8', '37', '27']

----------------------------------------------------------------------
Ran 8 tests in 0.067s

FAILED (failures=1, errors=3)

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

Александър обнови решението на 18.04.2016 16:35 (преди над 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
from uuid import uuid4
import datetime

MAXIMUM_POSTS_PER_USER = 50


class User:
        def __init__(self, full_name):
                self.full_name = full_name
                self.uuid = uuid4()
                self._posts = []

        def add_post(self, post_content):
                self._posts.append(Post(self.uuid, datetime.datetime.now(), post_content))
                if len(self._posts) > MAXIMUM_POSTS_PER_USER:
                        del self._posts[0]

        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


class SocialGraph:
        __social_graph = {}
        __users = [];

        def add_user(self, user):
                if user.uuid in SocialGraph.__social_graph:
                        raise UserAlreadyExistsError(
                                "User with uuid {0} already exists".format(user.uuid)
                        )

                SocialGraph.__social_graph[user.uuid] = []
                SocialGraph.__users.append(user)

        def get_user(self, user_uuid):
                filtered_users = list(filter(lambda u: u.uuid == user_uuid, 
                        SocialGraph.__users))

                if len(filtered_users) > 0:
                        return filtered_users[0]
                raise UserDoesNotExistsError(
                        "User with uuid {0} does not exist".format(user_uuid)
                )

        def delete_user(self, user_uuid):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                        "User with uuid {0} does not exist".format(user_uuid)
                        )
                user = self.get_user(user_uuid)
                SocialGraph.__users.remove(user)
                del SocialGraph.__social_graph[user_uuid]

        def follow(self, follower, followee):
                if followee not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Followee with uuid {0} does not exist".format(followee)
                        )

                if follower not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Follower with uuid {0} does not exist".format(follower)
                        )

                if follower not in SocialGraph.__social_graph[followee]:
                        SocialGraph.__social_graph[followee].append(follower)

        def unfollow(self, follower, followee):
                if followee not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Followee with uuid {0} does not exist".format(followee)
                        )

                if follower not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Follower with uuid {0} does not exist".format(follower)
                        )

                if follower in SocialGraph.__social_graph[followee]:
                        SocialGraph.__social_graph[followee].remove(follower)

        def is_following(self, follower, followee):
                if followee not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Followee with uuid {0} does not exist".format(followee)
                        )

                if follower not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "Follower with uuid {0} does not exist".format(follower)
                        )

                return follower in SocialGraph.__social_graph[followee]

        def followers(self, user_uuid):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "User with uuid {0} does not exist".format(user_uuid)
                        )

                return set(SocialGraph.__social_graph[user_uuid])

        def following(self, user_uuid):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "User with uuid {0} does not exist".format(user_uuid)
                        )

                return set(
                        [followee_uuid
                        for followee_uuid, follower_uuids
                        in SocialGraph.__social_graph.items()
                        if user_uuid in follower_uuids])

        def friends(self, user_uuid):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "User with uuid {0} does not exist".format(user_uuid)
                        )

                return set(
                        [friend_uuid
                        for friend_uuid
                        in SocialGraph.__social_graph.keys()
                        if self.is_following(friend_uuid, user_uuid)
                                        and self.is_following(user_uuid, friend_uuid)])

        def max_distance(self, user_uuid, result=0):
                if user_uuid not in SocialGraph.__social_graph:
                        raise UserDoesNotExistsError(
                                "User with uuid {0} does not exist".format(user_uuid)
                        )

                user_followers = self.followers(user_uuid)
                distances = [self.__get_distance(follower_uuid)
                        for follower_uuid
                        in user_followers]
                distances.sort()
                return distances[-1]

        def generate_feed(self, user_uuid, offset=0, limit=10):
                sorted_posts = sorted([post
                        for followed_user_uuid
                        in self.following(user_uuid)
                                for post
                                in self.get_user(followed_user_uuid)._posts],
                                        key=lambda post: post.published_at)
                if len(sorted_posts) <= (limit + offset):
                        return sorted_posts[offset:]
                return sorted_posts[offset:limit + offset]

        def __get_distance(self, user_uuid, result=1):
                user_followers = self.followers(user_uuid)
                for follower_uuid in user_followers:
                        if len(SocialGraph.__social_graph[follower_uuid]) == 0:
                                continue
                        return self.__get_distance(follower_uuid, result + 1)
                return result


class UserDoesNotExistsError(Exception):
        def __init__(self, message):
                self.message = message


class UserAlreadyExistsError(Exception):
        def __init__(self, message):
                self.message = message