timeit

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

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

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

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

Към профила на Виктор Драганов

Резултати

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

Код

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

MAX_POSTS = 50


class Post:

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


class User:

    def __init__(self, name):
        self.full_name = name
        self.uuid = uuid.uuid4()
        self.posts = []
        # sets containing uuids
        self.following = set()
        self.followers = set()

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        self.posts.append(post)
        if len(self.posts) > MAX_POSTS:
            self.posts.pop(0)

    def get_post(self):
        next_index = 0
        while next_index < len(self.posts):
            yield self.posts[next_index]
            next_index += 1
        #raise StopIteration

    def follow(self, followee):
        self.following.add(followee.uuid)
        followee.followers.add(self.uuid)

    def unfollow(self, followee):
        self.following.remove(followee.uuid)
        followee.followers.remove(self.uuid)


class SocialGraph:

    def __init__(self):
        self.users = {}

    def _check_user(self, uuid):
        if not uuid in self.users.keys():
            raise UserDoesNotExistError

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

    def get_user(self, uuid):
        self._check_user(uuid)
        return self.users[uuid]

    def delete_user(self, uuid):
        self._check_user(uuid)
        for follower in self.users[uuid].followers:
            self.users[follower].following.remove(uuid)
        del self.users[uuid]

    def follow(self, follower, followee):
        if follower == followee:
            raise UsersNotConnectedError
        self.users[follower].follow(self.users[followee])

    def unfollow(self, follower, followee):
        self.users[follower].unfollow(self.users[followee])

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

    def friends(self, uuid):
        friends = set()
        for followee in self.users[uuid].following:
            if self.is_following(followee, uuid):
                friends.add(followee)
        return friends

    def following(self, uuid):
        return self.users[uuid].following

    def followers(self, uuid):
        return self.users[uuid].followers

    def min_distance(self, from_user_uuid, to_user_uuid):
        from_user = self.users[from_user_uuid]
        to_user = self.users[to_user_uuid]
        if (not from_user.following and not from_user.followers):
            return math.inf
        if (not to_user.following and not to_user.followers):
            return math.inf
        if self.friends(from_user_uuid, to_user_uuid):
            return 1

    def max_distance(self, uuid):
        pass

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


class UserAlreadyExistsError(BaseException):
    pass


class UserDoesNotExistError(BaseException):
    pass


class UsersNotConnectedError(BaseException):
    pass

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

.F...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'

======================================================================
FAIL: 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
AssertionError: None != 2

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

FAILED (failures=1, errors=1)

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

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

MAX_POSTS = 50


class Post:

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


class User:

    def __init__(self, name):
        self.full_name = name
        self.uuid = uuid.uuid4()
        self.posts = []
        # sets containing uuids
        self.following = set()
        self.followers = set()

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        self.posts.append(post)
        if len(self.posts) > MAX_POSTS:
            self.posts.pop(0)

    def get_post(self):
        next_index = 0
        while next_index < len(self.posts):
            yield self.posts[next_index]
            next_index += 1
        #raise StopIteration

    def follow(self, followee):
        self.following.add(followee.uuid)
        followee.followers.add(self.uuid)

    def unfollow(self, followee):
        self.following.remove(followee.uuid)
        followee.followers.remove(self.uuid)


class SocialGraph:

    def __init__(self):
        self.users = {}

    def _check_user(self, uuid):
        if not uuid in self.users.keys():
            raise UserDoesNotExistError

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

    def get_user(self, uuid):
        self._check_user(uuid)
        return self.users[uuid]

    def delete_user(self, uuid):
        self._check_user(uuid)
        for follower in self.users[uuid].followers:
            self.users[follower].following.remove(uuid)
        del self.users[uuid]

    def follow(self, follower, followee):
        self.users[follower].follow(self.users[followee])

    def unfollow(self, follower, followee):
        self.users[follower].unfollow(self.users[followee])

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

    def friends(self, uuid):
        friends = set()
        for followee in self.users[uuid].following:
            if self.is_following(followee, uuid):
                friends.add(followee)
        return friends

    def following(self, uuid):
        return self.users[uuid].following

    def followers(self, uuid):
        return self.users[uuid].followers

    def min_distance(self):
        pass

    def max_distance(self, uuid):
        pass

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


class UserAlreadyExistsError(BaseException):
    pass


class UserDoesNotExistError(BaseException):
    pass


class UsersNotConnectedError(BaseException):
    pass

Виктор обнови решението на 18.04.2016 16: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
import datetime
import uuid
import math

MAX_POSTS = 50


class Post:

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


class User:

    def __init__(self, name):
        self.full_name = name
        self.uuid = uuid.uuid4()
        self.posts = []
        # sets containing uuids
        self.following = set()
        self.followers = set()

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        self.posts.append(post)
        if len(self.posts) > MAX_POSTS:
            self.posts.pop(0)

    def get_post(self):
        next_index = 0
        while next_index < len(self.posts):
            yield self.posts[next_index]
            next_index += 1
        #raise StopIteration

    def follow(self, followee):
        self.following.add(followee.uuid)
        followee.followers.add(self.uuid)

    def unfollow(self, followee):
        self.following.remove(followee.uuid)
        followee.followers.remove(self.uuid)


class SocialGraph:

    def __init__(self):
        self.users = {}

    def _check_user(self, uuid):
        if not uuid in self.users.keys():
            raise UserDoesNotExistError

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

    def get_user(self, uuid):
        self._check_user(uuid)
        return self.users[uuid]

    def delete_user(self, uuid):
        self._check_user(uuid)
        for follower in self.users[uuid].followers:
            self.users[follower].following.remove(uuid)
        del self.users[uuid]

    def follow(self, follower, followee):
        if follower == followee:
            raise UsersNotConnectedError
        self.users[follower].follow(self.users[followee])

    def unfollow(self, follower, followee):
        self.users[follower].unfollow(self.users[followee])

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

    def friends(self, uuid):
        friends = set()
        for followee in self.users[uuid].following:
            if self.is_following(followee, uuid):
                friends.add(followee)
        return friends

    def following(self, uuid):
        return self.users[uuid].following

    def followers(self, uuid):
        return self.users[uuid].followers

    def min_distance(self, from_user_uuid, to_user_uuid):
        from_user = self.users[from_user_uuid]
        to_user = self.users[to_user_uuid]
        if (not from_user.following and not from_user.followers):
            return math.inf
        if (not to_user.following and not to_user.followers):
            return math.inf
        if self.friends(from_user_uuid, to_user_uuid):
            return 1

    def max_distance(self, uuid):
        pass

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


class UserAlreadyExistsError(BaseException):
    pass


class UserDoesNotExistError(BaseException):
    pass


class UsersNotConnectedError(BaseException):
    pass