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


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.followers = set()
        self.following = set()
        self._posts = []

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

    def get_post(self):
        if len(self._posts):
            yield self._posts[0]


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.users = []

    def add_user(self, user):
        if self._user_in_graph(user.uuid):
            raise UserAlreadyExistsError()
        self.users.append(user)

    def get_user(self, user_uuid):
        if not self._user_in_graph(user_uuid):
            raise UserDoesNotExistError()
        user = self._get_user_by_uuid(user_uuid)
        return user

    def delete_user(self, user_uuid):
        if not self._user_in_graph(user_uuid):
            raise UserDoesNotExistError
        user = self._get_user_by_uuid(user_uuid)
        self.users.remove(user)
        return self.users

    def follow(self, follower, followee):
        followed_user = self._get_user_by_uuid(followee)
        following_user = self._get_user_by_uuid(follower)
        followed_user.followers.add(follower)
        following_user.following.add(followee)

    def unfollow(self, follower, followee):
        followed_user = self._get_user_by_uuid(followee)
        following_user = self._get_user_by_uuid(follower)
        followed_user.followers.remove(follower)
        following_user.following.remove(followee)
        return user.followers

    def is_following(self, follower, followee):
        user = self._get_user_by_uuid(followee)
        return follower in user.followers

    def followers(self, user_uuid):
        user = self._get_user_by_uuid(user_uuid)
        return user.followers

    def following(self, user_uuid):
        user = self._get_user_by_uuid(user_uuid)
        return user.following

    def friends(self, user_uuid):
        user = self._get_user_by_uuid(user_uuid)
        friends = user.followers & user.following
        return friends

    def _user_in_graph(self, user_uuid):
        is_added = False
        user = self._get_user_by_uuid(user_uuid)
        if isinstance(user, User):
            is_added = True
        return is_added

    def _get_user_by_uuid(self, user_uuid):
        for user in self.users:
            if user_uuid is user.uuid:
                return user


class UserAlreadyExistsError(Exception):
    def __init__(self):
        self.message = "This user already exists"


class UserDoesNotExistError(Exception):
    def __init__(self):
        self.message = "This user is not in the graph"

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

.EE..EF.
======================================================================
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_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
AttributeError: 'SocialGraph' object has no attribute 'generate_feed'

======================================================================
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_add_post (test.TestUser)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 1 != 50

----------------------------------------------------------------------
Ran 8 tests in 0.065s

FAILED (failures=1, errors=3)

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

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


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.followers = set()
        self.following = set()
        self._posts = []

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

    def get_post(self):
        if len(self._posts):
            yield self._posts[0]


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.users = []

    def add_user(self, user):
        if self._user_in_graph(user.uuid):
            raise UserAlreadyExistsError()
        self.users.append(user)

    def get_user(self, user_uuid):
        if not self._user_in_graph(user_uuid):
            raise UserDoesNotExistError()
        user = self._get_user_by_uuid(user_uuid)
        return user

    def delete_user(self, user_uuid):
        if not self._user_in_graph(user_uuid):
            raise UserDoesNotExistError
        user = self._get_user_by_uuid(user_uuid)
        self.users.remove(user)
        return self.users

    def follow(self, follower, followee):
        followed_user = self._get_user_by_uuid(followee)
        following_user = self._get_user_by_uuid(follower)
        followed_user.followers.add(follower)
        following_user.following.add(followee)

    def unfollow(self, follower, followee):
        followed_user = self._get_user_by_uuid(followee)
        following_user = self._get_user_by_uuid(follower)
        followed_user.followers.remove(follower)
        following_user.following.remove(followee)
        return user.followers

    def is_following(self, follower, followee):
        user = self._get_user_by_uuid(followee)
        return follower in user.followers

    def followers(self, user_uuid):
        user = self._get_user_by_uuid(user_uuid)
        return user.followers

    def following(self, user_uuid):
        user = self._get_user_by_uuid(user_uuid)
        return user.following

    def friends(self, user_uuid):
        user = self._get_user_by_uuid(user_uuid)
        friends = user.followers & user.following
        return friends

    def _user_in_graph(self, user_uuid):
        is_added = False
        user = self._get_user_by_uuid(user_uuid)
        if isinstance(user, User):
            is_added = True
        return is_added

    def _get_user_by_uuid(self, user_uuid):
        for user in self.users:
            if user_uuid is user.uuid:
                return user


class UserAlreadyExistsError(Exception):
    def __init__(self):
        self.message = "This user already exists"


class UserDoesNotExistError(Exception):
    def __init__(self):
        self.message = "This user is not in the graph"