timeit

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

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

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

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

Към профила на Алекс Николов

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 7 успешни тест(а)
  • 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
import uuid
import datetime


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

    def add_post(self, post_content):
        new_post = Post(self.uuid, post_content)

        if len(self.posts) is 50:
            self.posts.pop(0)
        self.posts.append(new_post)

    def get_post(self):
        return (post for post in self.posts)


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

    def __lt__(self, other):
        return self.published_at < other.published_at


class SocialGraph:
    def __init__(self):
        self.graph = {}
        self.users = set()

    def add_user(self, user):
        self.user_existance_check(user.uuid)

        self.graph[user.uuid] = set()
        self.users.add(user)

    def get_user(self, user_uuid):
        self.user_non_existance_check(user_uuid)

        for user in self.users:
            if user.uuid == user_uuid:
                return user

    def delete_user(self, user_uuid):
        self.user_non_existance_check(user_uuid)

        del self.graph[user_uuid]
        found_user = 1
        for user in self.users:
            if user.uuid == user_uuid:
                found_user = user
                break
        self.users.remove(found_user)

    def user_existance_check(self, user_uuid):
        if user_uuid in self.graph.keys():
            raise UserAlreadyExistsError()

    def user_non_existance_check(self, *users):
        for user in users:
            if user not in self.graph.keys():
                raise UserDoesNotExistError

    def follow(self, follower, followee):
        self.user_non_existance_check(follower, followee)
        self.graph[follower].add(followee)

    def unfollow(self, follower, followee):
        self.user_non_existance_check(follower, followee)
        self.graph[follower].remove(followee)

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

    def followers(self, user_uuid):
        self.user_non_existance_check(user_uuid)
        return {follower for follower in self.graph.keys()
                if user_uuid in self.graph[follower]}

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

    def friends(self, user_uuid):
        self.user_non_existance_check(user_uuid)
        return {friend for friend in self.graph.keys()
                if self.is_following(friend, user_uuid) and
                self.is_following(user_uuid, friend)}

    def max_distance(self, user_uuid):
        distances_to_all_users = []
        for target_user in self.users:
            try:
                distance_to_target = self.min_distance(user_uuid,
                                                       target_user.uuid)
            except UserNotConnectedError:
                distance_to_target = 0
            finally:
                distances_to_all_users.append(distance_to_target)
        return max(distances_to_all_users)

    def min_distance(self, from_user_uuid, to_user_uuid):
        return self.bfs(from_user_uuid, to_user_uuid)

    def bfs(self, from_user_uuid, to_user_uuid):
        distances = {uuid: -1 for uuid in self.graph.keys()}
        distances[from_user_uuid] = 0
        visited_queue = [from_user_uuid]

        while visited_queue:
            current_vertex = visited_queue[0]
            visited_queue.pop(0)

            for vertex in self.graph[current_vertex]:

                if distances[vertex] == -1:
                    distances[vertex] = distances[current_vertex] + 1
                    visited_queue.append(vertex)

                if vertex == to_user_uuid:
                    return distances[vertex]
        raise UserNotConnectedError

    def generate_feed(self, user_uuid, offset=0, limit=10):
        feed_from_all_users = []
        for followee in self.graph[user_uuid]:
            feed_from_all_users += self.get_user_from_uuid(followee).posts

        feed_from_all_users.sort(reverse=True)
        del feed_from_all_users[:offset]
        return feed_from_all_users[:limit]

    def get_user_from_uuid(self, user_uuid):
        for user in self.users:
            if user_uuid == user.uuid:
                return user


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UserNotConnectedError(Exception):
    pass

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

.....E..
======================================================================
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'

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

FAILED (errors=1)

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

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


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

    def add_post(self, post_content):
        new_post = Post(self.uuid, post_content)

        if len(self.posts) is 50:
            self.posts.pop(0)
        self.posts.append(new_post)

    def get_post(self):
        return (post for post in self.posts)


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

    def __lt__(self, other):
        return self.published_at < other.published_at


class SocialGraph:
    def __init__(self):
        self.graph = {}
        self.users = set()

    def add_user(self, user):
        self.user_existance_check(user.uuid)

        self.graph[user.uuid] = set()
        self.users.add(user)

    def get_user(self, user_uuid):
        self.user_non_existance_check(user_uuid)

        for user in self.users:
            if user.uuid == user_uuid:
                return user

    def delete_user(self, user_uuid):
        self.user_non_existance_check(user_uuid)

        del self.graph[user_uuid]
        found_user = 1
        for user in self.users:
            if user.uuid == user_uuid:
                found_user = user
                break
        self.users.remove(found_user)

    def user_existance_check(self, user_uuid):
        if user_uuid in self.graph.keys():
            raise UserAlreadyExistsError()

    def user_non_existance_check(self, *users):
        for user in users:
            if user not in self.graph.keys():
                raise UserDoesNotExistError

    def follow(self, follower, followee):
        self.user_non_existance_check(follower, followee)
        self.graph[follower].add(followee)

    def unfollow(self, follower, followee):
        self.user_non_existance_check(follower, followee)
        self.graph[follower].remove(followee)

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

    def followers(self, user_uuid):
        self.user_non_existance_check(user_uuid)
        return {follower for follower in self.graph.keys()
                if user_uuid in self.graph[follower]}

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

    def friends(self, user_uuid):
        self.user_non_existance_check(user_uuid)
        return {friend for friend in self.graph.keys()
                if self.is_following(friend, user_uuid) and
                self.is_following(user_uuid, friend)}

    def max_distance(self, user_uuid):
        distances_to_all_users = []
        for target_user in self.users:
            try:
                distance_to_target = self.min_distance(user_uuid,
                                                       target_user.uuid)
            except UserNotConnectedError:
                distance_to_target = 0
            finally:
                distances_to_all_users.append(distance_to_target)
        return max(distances_to_all_users)

    def min_distance(self, from_user_uuid, to_user_uuid):
        return self.bfs(from_user_uuid, to_user_uuid)

    def bfs(self, from_user_uuid, to_user_uuid):
        distances = {uuid: -1 for uuid in self.graph.keys()}
        distances[from_user_uuid] = 0
        visited_queue = [from_user_uuid]

        while visited_queue:
            current_vertex = visited_queue[0]
            visited_queue.pop(0)

            for vertex in self.graph[current_vertex]:

                if distances[vertex] == -1:
                    distances[vertex] = distances[current_vertex] + 1
                    visited_queue.append(vertex)

                if vertex == to_user_uuid:
                    return distances[vertex]
        raise UserNotConnectedError

    def generate_feed(self, user_uuid, offset=0, limit=10):
        feed_from_all_users = []
        for followee in self.graph[user_uuid]:
            feed_from_all_users += self.get_user_from_uuid(followee).posts

        feed_from_all_users.sort(reverse=True)
        del feed_from_all_users[:offset]
        return feed_from_all_users[:limit]

    def get_user_from_uuid(self, user_uuid):
        for user in self.users:
            if user_uuid == user.uuid:
                return user


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UserNotConnectedError(Exception):
    pass