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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import uuid
import datetime
import math


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


class User:

    posts = []

    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        number_of_posts = len(self.posts)
        if number_of_posts == 50:
            for ind in range(49):
                self.posts[ind] = self.posts[ind + 1]
            self.posts[49] = post
        else:
            self.posts.append(post)

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


class SocialGraph:

    def __init__(self):
        self.users = []
        self.user_followers = dict()
        self.user_followees = dict()

    def check_if_user_in_graph(self, uuid):
        for ind in range(len(self.users)):
            if self.users[ind].uuid == uuid:
                return True
        return False

    def add_user(self, user):
        if self.check_if_user_in_graph(user.uuid):
            user_already_exists()
        self.users.append(user)
        self.user_followers[user.uuid] = set()
        self.user_followees[user.uuid] = set()

    def get_user(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        for ind in range(len(self.users)):
            if self.users[ind].uuid == user_uuid:
                return self.users[ind]

    def delete_user(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        for ind in range(len(self.users)):
            if self.users[ind].uuid == user_uuid:
                del self.users[ind]
                # delete his ->(edges) too #
                return

    def follow(self, follower, followee):
        if not (self.check_if_user_in_graph(follower) and
                self.check_if_user_in_graph(followee)):
            return
        self.user_followees[follower].add(followee)
        self.user_followers[followee].add(follower)

    def unfollow(self, follower, followee):
        if not (self.check_if_user_in_graph(follower) and
                self.check_if_user_in_graph(followee)):
            return
        self.user_followees[follower].discard(followee)
        self.user_followers[followee].discard(follower)

    def is_following(self, follower, followee):
        if not (self.check_if_user_in_graph(follower) and
                self.check_if_user_in_graph(followee)):
            user_does_not_exist()
        if follower in self.user_followers[followee]:
            return True
        return False

    def followers(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        return self.user_followers[user_uuid]

    def following(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        return self.user_followees[user_uuid]

    def friends(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        return {
            x for x in self.user_followees[user_uuid]
            if x in self.user_followees[x]
        }

    def max_distance(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        if len(self.user_followees[user_uuid]) == 0:
            return math.inf
        reached = self.user_followees[user_uuid]
        max_path_dist = 1
        new_vertices = reached
        while len(new_vertices) != 0:
            new_vertices = {
                y for y in self.user_followees[z]
                for z in new_vertices
                if y not in reached
            }
            max_path_dist = max_path_dist + 1
            reached = reached.union(new_vertices)
        return max_path_dist - 1

    def min_distance(self, from_user_uuid, to_user_uuid):
        if not (self.check_if_user_in_graph(from_user_uuid) and
                self.check_if_user_in_graph(to_user_uuid)):
            user_does_not_exist()
        if from_user_uuid == to_user_uuid:
            return 0
        elif len(self.user_followees[from_user_uuid]) == 0:
            users_not_connected()
        reached = self.user_followees[from_user_uuid]
        min_dist = 1
        new_vertices = reached
        while to_user_uuid not in reached and len(new_vertices) != 0:
            new_vertices = {
                y for y in self.user_followees[z]
                for z in new_vertices
                if y not in reached
            }
            min_dist = min_dist + 1
            reached = reached.union(new_vertices)
        if to_user_uuid not in reached:
            users_not_connected()
        return min_dist

    def nth_layer_followings(self, user_uuid, n):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        if n == 0:
            return [user_uuid]
        new_vertices = self.user_followees[user_uuid]
        for _ in range(n):
            new_vertices = {
                y for y in self.user_followees[z]
                for z in new_vertices
            }
        return new_vertices

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        pass


class UserDoesNotExistError(Exception):
    def __init__(self):
        self.message = 'User does not exist!!'


def user_does_not_exist():
    raise UserDoesNotExistError


class UserAlreadyExistsError(Exception):
    def __init__(self):
        self.message = 'User already exists!!'


def user_already_exists():
    raise UserAlreadyExistsError


class UsersNotConnectedError(Exception):
    def __init__(self):
        self.message = 'Users are not connected!!'


def users_not_connected():
    raise UsersNotConnectedError

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

.EE.FE..
======================================================================
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
NameError: name 'z' is not defined

======================================================================
ERROR: 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
TypeError: 'NoneType' object is not iterable

======================================================================
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
NameError: name 'z' is not defined

======================================================================
FAIL: test_friends (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: UUID('7cbfac58-3ed3-4351-b98d-75cd8e9c8660') not found in set()

----------------------------------------------------------------------
Ran 8 tests in 0.066s

FAILED (failures=1, errors=3)

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

Иван обнови решението на 18.04.2016 12:54 (преди над 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
import uuid
import datetime
import math


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


class User:

    posts = []

    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        number_of_posts = len(self.posts)
        if number_of_posts == 50:
            for ind in range(49):
                self.posts[ind] = self.posts[ind + 1]
            self.posts[49] = post
        else:
            self.posts.append(post)

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


class SocialGraph:

    def __init__(self):
        self.users = []
        self.user_followers = dict()
        self.user_followees = dict()

    def check_if_user_in_graph(self, uuid):
        for ind in range(len(self.users)):
            if self.users[ind].uuid == uuid:
                return True
        return False

    def add_user(self, user):
        if self.check_if_user_in_graph(user.uuid):
            user_already_exists()
        self.users.append(user)
        self.user_followers[user.uuid] = set()
        self.user_followees[user.uuid] = set()

    def get_user(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        for ind in range(len(self.users)):
            if self.users[ind].uuid == user_uuid:
                return self.users[ind]

    def delete_user(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        for ind in range(len(self.users)):
            if self.users[ind].uuid == user_uuid:
                del self.users[ind]
                # delete his ->(edges) too #
                return

    def follow(self, follower, followee):
        if not (self.check_if_user_in_graph(follower) and
        self.check_if_user_in_graph(followee)):
            return
        self.user_followees[follower].add(followee)
        self.user_followers[followee].add(follower)

    def unfollow(self, follower, followee):
        if not (self.check_if_user_in_graph(follower) and
        self.check_if_user_in_graph(followee)):
            return
        self.user_followees[follower].discard(followee)
        self.user_followers[followee].discard(follower)

    def is_following(self, follower, followee):
        if not (self.check_if_user_in_graph(follower) and
        self.check_if_user_in_graph(followee)):
            user_does_not_exist()
        if follower in self.user_followers[followee]:
            return True
        return False

    def followers(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        return self.user_followers[user_uuid]

    def following(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        return self.user_followees[user_uuid]

    def friends(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        return {
            x for x in self.user_followees[user_uuid]
            if x in self.user_followees[x]
        }

    def max_distance(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        if len(self.user_followees[user_uuid]) == 0:
            return math.inf
        reached = self.user_followees[user_uuid]
        max_path_dist = 1
        new_vertices = reached
        while len(new_vertices) != 0:
            new_vertices = {
                y for y in self.user_followees[z]
                for z in new_vertices
                if y not in reached
            }
            max_path_dist = max_path_dist + 1
            reached = reached.union(new_vertices)
        return max_path_dist - 1

    def min_distance(self, from_user_uuid, to_user_uuid):
        if not (self.check_if_user_in_graph(from_user_uuid) and
        self.check_if_user_in_graph(to_user_uuid)):
            user_does_not_exist()
        if from_user_uuid == to_user_uuid:
            return 0
        elif len(self.user_followees[from_user_uuid]) == 0:
            users_not_connected()
        reached = self.user_followees[from_user_uuid]
        min_dist = 1
        new_vertices = reached
        while to_user_uuid not in reached and len(new_vertices) != 0:
            new_vertices = {
                y for y in self.user_followees[z]
                for z in new_vertices
                if y not in reached
            }
            min_dist = min_dist + 1
            reached = reached.union(new_vertices)
        if to_user_uuid not in reached:
            users_not_connected()
        return min_dist

    def nth_layer_followings(self, user_uuid, n):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        if n == 0:
            return [user_uuid]
        new_vertices = self.user_followees[user_uuid]
        for _ in range(n):
            new_vertices = {
                y for y in self.user_followees[z]
                for z in new_vertices
            }
        return new_vertices

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        pass


class UserDoesNotExistError(Exception):
    def __init__(self):
        self.message = 'User does not exist!!'


def user_does_not_exist():
    raise UserDoesNotExistError


class UserAlreadyExistsError(Exception):
    def __init__(self):
        self.message = 'User already exists!!'


def user_already_exists():
    raise UserAlreadyExistsError


class UsersNotConnectedError(Exception):
    def __init__(self):
        self.message = 'Users are not connected!!'


def users_not_connected():
    raise UsersNotConnectedError

Иван обнови решението на 18.04.2016 13:37 (преди над 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
import uuid
import datetime
import math


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


class User:

    posts = []

    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        number_of_posts = len(self.posts)
        if number_of_posts == 50:
            for ind in range(49):
                self.posts[ind] = self.posts[ind + 1]
            self.posts[49] = post
        else:
            self.posts.append(post)

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


class SocialGraph:

    def __init__(self):
        self.users = []
        self.user_followers = dict()
        self.user_followees = dict()

    def check_if_user_in_graph(self, uuid):
        for ind in range(len(self.users)):
            if self.users[ind].uuid == uuid:
                return True
        return False

    def add_user(self, user):
        if self.check_if_user_in_graph(user.uuid):
            user_already_exists()
        self.users.append(user)
        self.user_followers[user.uuid] = set()
        self.user_followees[user.uuid] = set()

    def get_user(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        for ind in range(len(self.users)):
            if self.users[ind].uuid == user_uuid:
                return self.users[ind]

    def delete_user(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        for ind in range(len(self.users)):
            if self.users[ind].uuid == user_uuid:
                del self.users[ind]
                # delete his ->(edges) too #
                return

    def follow(self, follower, followee):
        if not (self.check_if_user_in_graph(follower) and
                self.check_if_user_in_graph(followee)):
            return
        self.user_followees[follower].add(followee)
        self.user_followers[followee].add(follower)

    def unfollow(self, follower, followee):
        if not (self.check_if_user_in_graph(follower) and
                self.check_if_user_in_graph(followee)):
            return
        self.user_followees[follower].discard(followee)
        self.user_followers[followee].discard(follower)

    def is_following(self, follower, followee):
        if not (self.check_if_user_in_graph(follower) and
                self.check_if_user_in_graph(followee)):
            user_does_not_exist()
        if follower in self.user_followers[followee]:
            return True
        return False

    def followers(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        return self.user_followers[user_uuid]

    def following(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        return self.user_followees[user_uuid]

    def friends(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        return {
            x for x in self.user_followees[user_uuid]
            if x in self.user_followees[x]
        }

    def max_distance(self, user_uuid):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        if len(self.user_followees[user_uuid]) == 0:
            return math.inf
        reached = self.user_followees[user_uuid]
        max_path_dist = 1
        new_vertices = reached
        while len(new_vertices) != 0:
            new_vertices = {
                y for y in self.user_followees[z]
                for z in new_vertices
                if y not in reached
            }
            max_path_dist = max_path_dist + 1
            reached = reached.union(new_vertices)
        return max_path_dist - 1

    def min_distance(self, from_user_uuid, to_user_uuid):
        if not (self.check_if_user_in_graph(from_user_uuid) and
                self.check_if_user_in_graph(to_user_uuid)):
            user_does_not_exist()
        if from_user_uuid == to_user_uuid:
            return 0
        elif len(self.user_followees[from_user_uuid]) == 0:
            users_not_connected()
        reached = self.user_followees[from_user_uuid]
        min_dist = 1
        new_vertices = reached
        while to_user_uuid not in reached and len(new_vertices) != 0:
            new_vertices = {
                y for y in self.user_followees[z]
                for z in new_vertices
                if y not in reached
            }
            min_dist = min_dist + 1
            reached = reached.union(new_vertices)
        if to_user_uuid not in reached:
            users_not_connected()
        return min_dist

    def nth_layer_followings(self, user_uuid, n):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        if n == 0:
            return [user_uuid]
        new_vertices = self.user_followees[user_uuid]
        for _ in range(n):
            new_vertices = {
                y for y in self.user_followees[z]
                for z in new_vertices
            }
        return new_vertices

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if not self.check_if_user_in_graph(user_uuid):
            user_does_not_exist()
        pass


class UserDoesNotExistError(Exception):
    def __init__(self):
        self.message = 'User does not exist!!'


def user_does_not_exist():
    raise UserDoesNotExistError


class UserAlreadyExistsError(Exception):
    def __init__(self):
        self.message = 'User already exists!!'


def user_already_exists():
    raise UserAlreadyExistsError


class UsersNotConnectedError(Exception):
    def __init__(self):
        self.message = 'Users are not connected!!'


def users_not_connected():
    raise UsersNotConnectedError