timeit

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

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

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

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

Към профила на Веселин Иванов

Резултати

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

Код

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
import uuid
from datetime import datetime
from collections import deque


class User:
    def __init__(self, fullname):
        self.fullname = fullname
        self.uuid = uuid.uuid4()
        self.posts = deque(maxlen=50)

    def add_post(self, post_content):
        new_post = Post(self.uuid, datetime.now(), post_content)
        self.posts.append(new_post)

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


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 not in self.graph:
            raise UserDoesNotExistError()
        return self.graph[user_uuid][0]

    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()
        followed = self.graph[follower][1]
        followed[followee] = self.graph[followee][0]

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

    def is_following(self, follower, followee):
        if follower not in self.graph:
            raise UserDoesNotExistError()
        return followee in self.graph[follower][1]

    def followers(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError()
        return {key for key, value in self.graph if user_uuid in value[1]}

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

    def friends(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError()
        return self.followers(user_uuid) & self.following(user_uuid)

    def max_distance(self, user_uuid):
        pass

    def min_distance(self, from_user_uuid, to_user_uuid):
        pass

    def nth_layer_followings(self, user_uuid, n):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError()
        pass

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError()
        pass


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass

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

.FEEEE..
======================================================================
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_following (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: 'UUID' object is not iterable

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

======================================================================
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.064s

FAILED (failures=1, errors=4)

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

Веселин обнови решението на 18.04.2016 16:59 (преди над 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
import uuid
from datetime import datetime
from collections import deque


class User:
    def __init__(self, fullname):
        self.fullname = fullname
        self.uuid = uuid.uuid4()
        self.posts = deque(maxlen=50)

    def add_post(self, post_content):
        new_post = Post(self.uuid, datetime.now(), post_content)
        self.posts.append(new_post)

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


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 not in self.graph:
            raise UserDoesNotExistError()
        return self.graph[user_uuid][0]

    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()
        followed = self.graph[follower][1]
        followed[followee] = self.graph[followee][0]

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

    def is_following(self, follower, followee):
        if follower not in self.graph:
            raise UserDoesNotExistError()
        return followee in self.graph[follower][1]

    def followers(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError()
        return {key for key, value in self.graph if user_uuid in value[1]}

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

    def friends(self, user_uuid):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError()
        return self.followers(user_uuid) & self.following(user_uuid)

    def max_distance(self, user_uuid):
        pass

    def min_distance(self, from_user_uuid, to_user_uuid):
        pass

    def nth_layer_followings(self, user_uuid, n):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError()
        pass

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if user_uuid not in self.graph:
            raise UserDoesNotExistError()
        pass


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass