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


class UserDoesNotExistError(Exception):
    def __init__(self):
        self.message = "The user does not exists"


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


class UsersNotConnectedError(Exception):
    def __init__(self):
        self.message = "The users are not connected"


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


class User:
    def __init__(self, full_name):
        self.full_name = full_name;
        self.uuid = uuid.uuid4();
        self.posts = [];

    def add_post(self, post_content):
        if len(self.posts) == 50:
            self.posts.remove(self.posts[0]);
        self.posts.append(Post(self.uuid, post_content));

    def get_post(self):
        return iter(self.posts);


class SocialGraph:
    def __init__(self):
        self.users = {};
        self.followers = defaultdict(list);
        self.followees = defaultdict(list);

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

        self.users[user.uuid] = user;

    def get_user(self, uuid):
        if uuid not in self.users:
            raise UserDoesNotExistError()
        return self.users[uuid];

    def delete_user(self, uuid):
        if uuid not in self.users:
            raise UserDoesNotExistError()
        del self.users[uuid];

    def follow(self, follower, followee):
        self.followees[follower] = followee;
        self.followers[followee] = follower;

    def unfollow(self, follower, followee):
        self.followees[follower].remove(followee);
        self.followers[followee].remove(follower);

    def is_following(self, follower, followee):
        return follower in self.followers[followee];

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

        return set(self.followers[user_uuid]);

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

        return set(self.followees[user_uuid]);

    def friends(self, user_uuid):
        return self.followers[user_uuid].intersection(self.followees[user_uuid])

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

.EEEEE..
======================================================================
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_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: argument of type 'UUID' 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
AttributeError: 'list' object has no attribute 'intersection'

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

----------------------------------------------------------------------
Ran 8 tests in 0.066s

FAILED (errors=5)

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

Деян обнови решението на 17.04.2016 21:10 (преди над 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
import uuid
from collections import defaultdict
from datetime import datetime


class UserDoesNotExistError(Exception):
    def __init__(self):
        self.message = "The user does not exists"


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


class UsersNotConnectedError(Exception):
    def __init__(self):
        self.message = "The users are not connected"


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


class User:
    def __init__(self, full_name):
        self.full_name = full_name;
        self.uuid = uuid.uuid4();
        self.posts = [];

    def add_post(self, post_content):
        if len(self.posts) == 50:
            self.posts.remove(self.posts[0]);
        self.posts.append(Post(self.uuid, post_content));

    def get_post(self):
        return iter(self.posts);


class SocialGraph:
    def __init__(self):
        self.users = {};
        self.followers = defaultdict(list);
        self.followees = defaultdict(list);

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

        self.users[user.uuid] = user;

    def get_user(self, uuid):
        if uuid not in self.users:
            raise UserDoesNotExistError()
        return self.users[uuid];

    def delete_user(self, uuid):
        if uuid not in self.users:
            raise UserDoesNotExistError()
        del self.users[uuid];

    def follow(self, follower, followee):
        self.followees[follower] = followee;
        self.followers[followee] = follower;

    def unfollow(self, follower, followee):
        self.followees[follower].remove(followee);
        self.followers[followee].remove(follower);

    def is_following(self, follower, followee):
        return follower in self.followers[followee];

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

        return set(self.followers[user_uuid]);

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

        return set(self.followees[user_uuid]);

    def friends(self, user_uuid):
        return self.followers[user_uuid].intersection(self.followees[user_uuid])

Имаш доста неминаващи примерни тестове. Пребори се с тях първо.

Отделно, дублирането на информация във followers и followees е пипкаво, сам си поставяш капани, в които можеш да изпуснеш да добавиш/махнеш нещо и на двете места. Ако смяташ, че удобството и спестяването на сметки си струват дублирането на информация, по-скоро изнеси тази функционалност в отделен клас.