timeit

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

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

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

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

Към профила на Йоан Динков

Резултати

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

Код

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


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.posts = queue.Queue(50)
        self.followers = set()
        self.following = set()

    def add_post(self, post_content):
        self.posts.put(Post(self.uuid, post_content))

    def get_post(self):
        for i in range(50):
            post = self.posts.get()
            self.posts.put(post)
            yield post


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


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

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

        self.users[user.uuid] = user

    def get_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError

        return self.users[user_uuid]

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

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

        follower.following.add(followee_uuid)
        follower.followers.add(follower_uuid)

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

        if followee_uuid in follower.following:
            follower.following.remove(followee_uuid)

        if follower_uuid in followee.followers:
            followee.followers.remove(follower_uuid)

    def is_following(self, follower_uuid, followee_uuid):
        follower = self.users[follower_uuid]

        return followee_uuid in follower.following

    def followers(self, user_uuid):
        user = self.users[user_uuid]

        return user.followers

    def following(self, user_uuid):
        user = self.users[user_uuid]

        return user.following

    def friends(self, user_uuid):
        user = self.users[user_uuid]
        return [id for id in user.following if
                user.uuid in self.users[id].following]

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

EEEF.EE.
======================================================================
ERROR: test_add_get_and_delete_user (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
KeyError: UUID('2aa3c573-ef02-40f2-8bba-9eb95bf4e86a')

======================================================================
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'

======================================================================
ERROR: test_add_post (test.TestUser)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 63, in thread
    raise TimeoutError
TimeoutError

======================================================================
FAIL: 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
AssertionError: False is not true

----------------------------------------------------------------------
Ran 8 tests in 2.085s

FAILED (failures=1, errors=5)

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

Йоан обнови решението на 17.04.2016 23:03 (преди над 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
import uuid
import queue
import datetime


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.posts = queue.Queue(50)
        self.followers = set()
        self.following = set()

    def add_post(self, post_content):
        self.posts.put(Post(self.uuid, post_content))

    def get_post(self):
        for i in range(50):
            post = self.posts.get()
            self.posts.put(post)
            yield post


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


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

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

        self.users[user.uuid] = user

    def get_user(self, user_uuid):
        if user_uuid not in self.users:
            raise UserDoesNotExistError

        return self.users[user_uuid]

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

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

        follower.following.add(followee_uuid)
        follower.followers.add(follower_uuid)

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

        if followee_uuid in follower.following:
            follower.following.remove(followee_uuid)

        if follower_uuid in followee.followers:
            followee.followers.remove(follower_uuid)

    def is_following(self, follower_uuid, followee_uuid):
        follower = self.users[follower_uuid]

        return followee_uuid in follower.following

    def followers(self, user_uuid):
        user = self.users[user_uuid]

        return user.followers

    def following(self, user_uuid):
        user = self.users[user_uuid]

        return user.following

    def friends(self, user_uuid):
        user = self.users[user_uuid]
        return [id for id in user.following if
                user.uuid in self.users[id].following]