timeit

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

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

Решение на Социална мрежа от Иван Димитров

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

Към профила на Иван Димитров

Резултати

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

Код

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


class UserDoesNotExistError(Exception):

    pass


class UserAlreadyExistsError(Exception):

    pass


class Post:

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

    def __lt__(self, rhs):
        if self.published_at < rhs.published_at:
            return True
        else:
            return False


class User:

    def __init__(self, name):
        self.full_name = name
        self.uuid = uuid.uuid4()
        self.posts = []
        self._current = 0
        self._follows = []

    def add_post(self, post_content):
        if len(self.posts) < 50:
            published_at = datetime.datetime.now()
            post = Post(self.uuid, published_at, post_content)
            self.posts.append(post)
            self.posts.sort()
        else:
            self.posts.pop(0)
            published_at = datetime.datetime.now()
            post = Post(self.uuid, published_at, post_content)
            self.posts.append(post)
            self.posts.sort()

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


class SocialGraph:

    def __init__(self):
        self.users = dict()

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

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

    def delete_user(self, user_uuid):
        if user_uuid in self.users:
            self.users.pop(user_uuid, None)
        else:
            raise UserDoesNotExistError

    def follow(self, follower, followee):
        if follower in self.users and followee in self.users:
            if followee not in self.users[follower]._follows:
                self.users[follower]._follows.append(followee)
        else:
            raise UserDoesNotExistError

    def unfollow(self, follower, followee):
        if follower in self.users and followee in self.users:
            if followee in self.users[follower]._follows:
                self.users[follower]._follows.remove(followee)
        else:
            raise UserDoesNotExistError

    def is_following(self, follower, followee):
        if follower in self.users and followee in self.users:
            if followee in self.users[follower]._follows:
                return True
            else:
                return False
        else:
            raise UserDoesNotExistError

    def followers(self, user_uuid):
        if user_uuid in self.users:
            return {x.uuid for x in self.users.values()
                    if x._follows.count(user_uuid) == 1}
        else:
            raise UserDoesNotExistError

    def following(self, user_uuid):
        if user_uuid in self.users:
            return {x for x in self.users[user_uuid]._follows}
        else:
            raise UserDoesNotExistError

    def friends(self, user_uuid):
        return {x.uuid for x in self.users.values()
                if x._follows.count(user_uuid) == 1
                and self.users[user_uuid]._follows.count(x.uuid) == 1}

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if user_uuid in self.users:
            feed = []
            user = self.users[user_uuid]
            if limit > len(user.posts):
                limit = len(user.posts)
            for i in range(offset, offset + limit):
                feed.append(user.posts[i])
            return feed
        else:
            raise UserDoesNotExistError

    def min_distance(self, from_user_uuid, to_user_uuid):
        if from_user_uuid in self.users and to_user_uuid in self.users:
            distance = 0
            visited, stack = set(), [self.users[from_user_uuid]]
            while stack:
                current = stack.pop()
                if current.uuid == to_user_uuid:
                    return distance
                if current not in visited:
                    visited.add(current)
                    distance += 1
                    children_uuids = [x for x
                                      in self.users[current.uuid]._follows]
                    child_nodes = [self.users[x] for x in children_uuids]
                    stack.extend(child_nodes)
        else:
            raise UserDoesNotExistError

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

.EF..E..
======================================================================
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
AttributeError: 'SocialGraph' object has no attribute 'max_distance'

======================================================================
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: [] != ['39', '29', '19', '9', '38', '28', '18', '8', '37', '27']

Second list contains 10 additional elements.
First extra element 0:
39

- []
+ ['39', '29', '19', '9', '38', '28', '18', '8', '37', '27']

----------------------------------------------------------------------
Ran 8 tests in 0.072s

FAILED (failures=1, errors=2)

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

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


class UserDoesNotExistError(Exception):

    pass


class UserAlreadyExistsError(Exception):

    pass


class Post:

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

    def __lt__(self, rhs):
        if self.published_at < rhs.published_at:
            return True
        else:
            return False


class User:

    def __init__(self, name):
        self.full_name = name
        self.uuid = uuid.uuid4()
        self.posts = []
        self._current = 0
        self._follows = []

    def add_post(self, post_content):
        if len(self.posts) < 50:
            published_at = datetime.datetime.now()
            post = Post(self.uuid, published_at, post_content)
            self.posts.append(post)
            self.posts.sort()
        else:
            self.posts.pop(0)
            published_at = datetime.datetime.now()
            post = Post(self.uuid, published_at, post_content)
            self.posts.append(post)
            self.posts.sort()

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


class SocialGraph:

    def __init__(self):
        self.users = dict()

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

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

    def delete_user(self, user_uuid):
        if user_uuid in self.users:
            self.users.pop(user_uuid, None)
        else:
            raise UserDoesNotExistError

    def follow(self, follower, followee):
        if follower in self.users and followee in self.users:
            if followee not in self.users[follower]._follows:
                self.users[follower]._follows.append(followee)
        else:
            raise UserDoesNotExistError

    def unfollow(self, follower, followee):
        if follower in self.users and followee in self.users:
            if followee in self.users[follower]._follows:
                self.users[follower]._follows.remove(followee)
        else:
            raise UserDoesNotExistError

    def is_following(self, follower, followee):
        if follower in self.users and followee in self.users:
            if followee in self.users[follower]._follows:
                return True
            else:
                return False
        else:
            raise UserDoesNotExistError

    def followers(self, user_uuid):
        if user_uuid in self.users:
            return {x.uuid for x in self.users.values()
                    if x._follows.count(user_uuid) == 1}
        else:
            raise UserDoesNotExistError

    def following(self, user_uuid):
        if user_uuid in self.users:
            return {x for x in self.users[user_uuid]._follows}
        else:
            raise UserDoesNotExistError

    def friends(self, user_uuid):
        return {x.uuid for x in self.users.values()
                if x._follows.count(user_uuid) == 1
                and self.users[user_uuid]._follows.count(x.uuid) == 1}

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if user_uuid in self.users:
            feed = []
            user = self.users[user_uuid]
            if limit > len(user.posts):
                limit = len(user.posts)
            for i in range(offset, offset + limit):
                feed.append(user.posts[i])
            return feed
        else:
            raise UserDoesNotExistError

    def min_distance(self, from_user_uuid, to_user_uuid):
        if from_user_uuid in self.users and to_user_uuid in self.users:
            distance = 0
            visited, stack = set(), [self.users[from_user_uuid]]
            while stack:
                current = stack.pop()
                if current.uuid == to_user_uuid:
                    return distance
                if current not in visited:
                    visited.add(current)
                    distance += 1
                    children_uuids = [x for x
                                      in self.users[current.uuid]._follows]
                    child_nodes = [self.users[x] for x in children_uuids]
                    stack.extend(child_nodes)
        else:
            raise UserDoesNotExistError