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
import math
import datetime
import uuid


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass


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

    def add_post(self, post_content):
        self.count_posts += 1
        if self.count_posts > 50:
            self.posts.pop(0)
        self.posts.append(
            Post(self.uuid, datetime.datetime.now(), post_content))

    def get_post(self):
        for post in self.posts:
            yield post
        raise StopIteration


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


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

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

    def get_user(self, user_uuid):
        if user_uuid in self.graph:
                return self.graph[user_uuid][0]
        raise UserDoesNotExistError

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

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

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

    def is_following(self, follower, followee):
        if follower not in self.graph or followee not in self.graph:
            raise UserDoesNotExistError
        return followee in self.following(follower)

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

    def followers(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        return {user for user in self.graph.keys()
                if self.is_following(user, user_uuid)}

    def friends(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        return set(filter(lambda x: self.is_following(x, user_uuid),
                                    self.following(user_uuid)))

    def max_distance(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        if(not self.following(user_uuid)):
            return math.inf
        level = {user_uuid}
        history = {user_uuid}
        dist = 0
        while level:
            level2 = set()
            for user in level:
                level2 = level2.union(self.following(user)) - history

            history = history.union(level2)
            dist += 1
            level = level2
        return dist-1

    def min_distance(self, from_user_uuid, to_user_uuid):
        if from_user_uuid not in self.graph or to_user_uuid not in self.graph:
            raise UserDoesNotExistError
        level = {from_user_uuid}
        history = {from_user_uuid}
        dist = 0
        while level:
            level2 = set()
            for followee in level:
                if followee == to_user_uuid:
                    return dist
                level2 = level2.union(self.following(followee)) - history
            history = history.union(level2)
            dist += 1
            level = level2
        raise UsersNotConnectedError

    def nth_layer_followings(self, user_uuid, n):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        level = {user_uuid}
        history = {user_uuid}
        count_level = 0
        while count_level < n and level:
            level2 = set()
            count_level += 1
            for followee in level:
                level2 = level2.union(self.following(followee)) - history
            history = history.union(level2)
            level = level2
        return level

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

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.078s

OK

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

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


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass


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

    def add_post(self, post_content):
        self.count_posts += 1
        if self.count_posts > 50:
            self.posts.pop(0)
        self.posts.append(
            Post(self.uuid, datetime.datetime.now(), post_content))

    def get_post(self):
        for post in self.posts:
            yield post
        raise StopIteration


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

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


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

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

    def get_user(self, user_uuid):
        for user in self.graph:
            if(user.uuid == user_uuid):
                return user
        raise UserDoesNotExistError

    def delete_user(self, user_uuid):
        del self.graph[self.get_user(user_uuid)]

    def follow(self, follower, followee):
        follower = self.get_user(follower)
        followee = self.get_user(followee)
        self.graph[follower].append(followee)

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

    def is_following(self, follower, followee):
        return followee in self.following(follower)

    def following(self, user_uuid):
        follower = self.get_user(user_uuid)
        return {user.uuid for user in self.graph[follower]}

    def followers(self, user_uuid):
        return {user.uuid for user in self.graph.keys()
                if self.is_following(user.uuid, user_uuid)}

    def friends(self, user_uuid):
        return set(filter(lambda x: self.is_following(x, user_uuid),
                                    self.following(user_uuid)))

    def max_distance(self, user_uuid):
        level = {user_uuid}
        history = {user_uuid}
        dist = 0
        while level:
            level2 = set()
            for user in level:
                level2 = level2.union(self.following(user)) - history

            history = history.union(level2)
            dist += 1
            level = level2
        return max(0, dist-1)

    def min_distance(self, from_user_uuid, to_user_uuid):
        level = {from_user_uuid}
        history = {from_user_uuid}
        dist = 0
        while level:
            level2 = set()
            for followee in level:
                if followee == to_user_uuid:
                    return dist
                level2 = level2.union(self.following(followee)) - history
                history = history.union(level2)
            dist += 1
            level = level2
        raise UsersNotConnectedError

    def nth_layer_followings(self, user_uuid, n):
        level = {user_uuid}
        count_level = 0
        while count_level < n and level:
            level2 = set()
            count_level += 1
            for followee in level:
                level2 = level2.union(self.following(followee))
            level = level2
        return level

    def generate_feed(self, user_uuid, offset=0, limit=0):
        posts = []
        for followee in self.following(user_uuid):
            posts += self.get_user(followee).posts
        posts.sort(reverse=True, key=lambda x: x.published_at)
        return posts[offset:offset + limit]

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


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass


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

    def add_post(self, post_content):
        self.count_posts += 1
        if self.count_posts > 50:
            self.posts.pop(0)
        self.posts.append(
            Post(self.uuid, datetime.datetime.now(), post_content))

    def get_post(self):
        for post in self.posts:
            yield post
        raise StopIteration


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


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

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

    def get_user(self, user_uuid):
        if user_uuid in self.graph:
                return self.graph[user_uuid][0]
        raise UserDoesNotExistError

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

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

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

    def is_following(self, follower, followee):
        if follower not in self.graph or followee not in self.graph:
            raise UserDoesNotExistError
        return followee in self.following(follower)

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

    def followers(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        return {user for user in self.graph.keys()
                if self.is_following(user, user_uuid)}

    def friends(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        return set(filter(lambda x: self.is_following(x, user_uuid),
                                    self.following(user_uuid)))

    def max_distance(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        if(not self.following(user_uuid)):
            return math.inf
        level = {user_uuid}
        history = {user_uuid}
        dist = 0
        while level:
            level2 = set()
            for user in level:
                level2 = level2.union(self.following(user)) - history

            history = history.union(level2)
            dist += 1
            level = level2
        return dist-1

    def min_distance(self, from_user_uuid, to_user_uuid):
        if from_user_uuid not in self.graph or to_user_uuid not in self.graph:
            raise UserDoesNotExistError
        level = {from_user_uuid}
        history = {from_user_uuid}
        dist = 0
        while level:
            level2 = set()
            for followee in level:
                if followee == to_user_uuid:
                    return dist
                level2 = level2.union(self.following(followee)) - history
            history = history.union(level2)
            dist += 1
            level = level2
        raise UsersNotConnectedError

    def nth_layer_followings(self, user_uuid, n):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        level = {user_uuid}
        history = {user_uuid}
        count_level = 0
        while count_level < n and level:
            level2 = set()
            count_level += 1
            for followee in level:
                level2 = level2.union(self.following(followee)) - history
                history = history.union(level2)
            level = level2
        return level

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

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


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass


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

    def add_post(self, post_content):
        self.count_posts += 1
        if self.count_posts > 50:
            self.posts.pop(0)
        self.posts.append(
            Post(self.uuid, datetime.datetime.now(), post_content))

    def get_post(self):
        for post in self.posts:
            yield post
        raise StopIteration


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


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

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

    def get_user(self, user_uuid):
        if user_uuid in self.graph:
                return self.graph[user_uuid][0]
        raise UserDoesNotExistError

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

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

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

    def is_following(self, follower, followee):
        if follower not in self.graph or followee not in self.graph:
            raise UserDoesNotExistError
        return followee in self.following(follower)

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

    def followers(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        return {user for user in self.graph.keys()
                if self.is_following(user, user_uuid)}

    def friends(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        return set(filter(lambda x: self.is_following(x, user_uuid),
                                    self.following(user_uuid)))

    def max_distance(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        if(not self.following(user_uuid)):
            return math.inf
        level = {user_uuid}
        history = {user_uuid}
        dist = 0
        while level:
            level2 = set()
            for user in level:
                level2 = level2.union(self.following(user)) - history

            history = history.union(level2)
            dist += 1
            level = level2
        return dist-1

    def min_distance(self, from_user_uuid, to_user_uuid):
        if from_user_uuid not in self.graph or to_user_uuid not in self.graph:
            raise UserDoesNotExistError
        level = {from_user_uuid}
        history = {from_user_uuid}
        dist = 0
        while level:
            level2 = set()
            for followee in level:
                if followee == to_user_uuid:
                    return dist
                level2 = level2.union(self.following(followee)) - history
            history = history.union(level2)
            dist += 1
            level = level2
        raise UsersNotConnectedError

    def nth_layer_followings(self, user_uuid, n):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError
        level = {user_uuid}
        history = {user_uuid}
        count_level = 0
        while count_level < n and level:
            level2 = set()
            count_level += 1
            for followee in level:
                level2 = level2.union(self.following(followee)) - history
            history = history.union(level2)
            level = level2
        return level

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