timeit

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

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

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

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

Към профила на Антонио Илиев

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 1 успешни тест(а)
  • 7 неуспешни тест(а)

Код

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
import uuid
import queue
import datetime
class Post:
        def __init__(self, author, published_at, content):
                self.author = author
                self.published_at = published_at
                self.content = content
                
class UserAlreadyExistsError(Exception):
        pass

        
class UserDoesNotExistError(Exception):
        pass
                
                
class UsersNotConnectedError(Exception):
        pass


class User:
        
        def __init__(self, full_name, uuid = uuid.uuid4(), pqueue = queue.Queue(50)):
                self.full_name = full_name
                self.uuid = uuid
                self.pqueue = pqueue
                
        def add_post(self, content):
                p_post = Post(self.uuid, datetime.datetime.now(), content)
                if self.pqueue.full():
                        self.pqueue.get()
                else:
                        self.pqueue.put(p_post)


class Vertex:
    def __init__(self, node, uuid):
        self.id = node
        self.adjacent = {}
        self.uuid = uuid

    def __str__(self):
        return str(self.id) + ' adjacent: ' + str([x.id for x in self.adjacent])

    def add_neighbor(self, neighbor, uuid):
        self.adjacent[neighbor.uuid] = neighbor

    def delete_neighbor(self, neighbor):
        del self.adjacent[neighbor]

    def get_connections(self):
        return self.adjacent.keys()  

    def get_id(self):
        return self.id

    def get_weight(self, neighbor):
        return self.adjacent[neighbor]

                
class SocialGraph:

        def __init__(self):
                self.vert_dict = {}
                self.num_vertices = 0

        def __iter__(self):
                return iter(self.vert_dict.values())

        def add_user(self, user):
                if user.uuid in self.vert_dict:
                        raise UserAlreadyExistsError
                else:
                        self.num_vertices = self.num_vertices + 1
                        new_vertex = Vertex(user, user.uuid)
                        self.vert_dict[user.uuid] = new_vertex
                        return new_vertex
        
        def delete_user(self, user_uuid):
                del self.vert_dict[user_uuid]

        def get_user(self, user_uuid):
                if n in self.vert_dict:
                        return self.vert_dict[user_uuid]
                else:
                        raise UserDoesNotExistError()

        def follow(self, follower, followee):
                if follower not in self.vert_dict:
                        raise UserDoesNotExistError
                        
                if followee not in self.vert_dict:
                        raise UserDoesNotExistError
                        

                self.vert_dict[follower.uuid].add_neighbor(self.vert_dict[followee.uuid])
                
        
        def unfollow(self, follower, followee):
                self.vert_dict[follower.uuid].delete_neighbor(self.vert_dict[followee.uuid])

        def get_vertices(self):
                return self.vert_dict.keys()
                
        def following(self, user_uuid):
                return set(self.vert_dict[user_uuid].get_connections())
        
        def is_following(self, follower, followee):
                return followee in self.vert_dict[follower].adjacent
                

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

EEEEEEE.
======================================================================
ERROR: test_add_get_and_delete_user (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/d20160530-24280-1s7dtej/test.py", line 16, in setUp
    self.graph.add_user(self.eric)
  File "/tmp/d20160530-24280-1s7dtej/solution.py", line 73, in add_user
    raise UserAlreadyExistsError
solution.UserAlreadyExistsError

======================================================================
ERROR: test_distnaces (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/d20160530-24280-1s7dtej/test.py", line 16, in setUp
    self.graph.add_user(self.eric)
  File "/tmp/d20160530-24280-1s7dtej/solution.py", line 73, in add_user
    raise UserAlreadyExistsError
solution.UserAlreadyExistsError

======================================================================
ERROR: test_feed (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/d20160530-24280-1s7dtej/test.py", line 16, in setUp
    self.graph.add_user(self.eric)
  File "/tmp/d20160530-24280-1s7dtej/solution.py", line 73, in add_user
    raise UserAlreadyExistsError
solution.UserAlreadyExistsError

======================================================================
ERROR: test_following (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/d20160530-24280-1s7dtej/test.py", line 16, in setUp
    self.graph.add_user(self.eric)
  File "/tmp/d20160530-24280-1s7dtej/solution.py", line 73, in add_user
    raise UserAlreadyExistsError
solution.UserAlreadyExistsError

======================================================================
ERROR: test_friends (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/d20160530-24280-1s7dtej/test.py", line 16, in setUp
    self.graph.add_user(self.eric)
  File "/tmp/d20160530-24280-1s7dtej/solution.py", line 73, in add_user
    raise UserAlreadyExistsError
solution.UserAlreadyExistsError

======================================================================
ERROR: test_layer_followings (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/d20160530-24280-1s7dtej/test.py", line 16, in setUp
    self.graph.add_user(self.eric)
  File "/tmp/d20160530-24280-1s7dtej/solution.py", line 73, in add_user
    raise UserAlreadyExistsError
solution.UserAlreadyExistsError

======================================================================
ERROR: 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
AttributeError: 'User' object has no attribute 'get_post'

----------------------------------------------------------------------
Ran 8 tests in 0.034s

FAILED (errors=7)

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

Антонио обнови решението на 18.04.2016 16:53 (преди над 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
import uuid
import queue
import datetime
class Post:
        def __init__(self, author, published_at, content):
                self.author = author
                self.published_at = published_at
                self.content = content
                
class UserAlreadyExistsError(Exception):
        pass

        
class UserDoesNotExistError(Exception):
        pass
                
                
class UsersNotConnectedError(Exception):
        pass


class User:
        
        def __init__(self, full_name, uuid = uuid.uuid4(), pqueue = queue.Queue(50)):
                self.full_name = full_name
                self.uuid = uuid
                self.pqueue = pqueue
                
        def add_post(self, content):
                p_post = Post(self.uuid, datetime.datetime.now(), content)
                if self.pqueue.full():
                        self.pqueue.get()
                else:
                        self.pqueue.put(p_post)


class Vertex:
    def __init__(self, node, uuid):
        self.id = node
        self.adjacent = {}
        self.uuid = uuid

    def __str__(self):
        return str(self.id) + ' adjacent: ' + str([x.id for x in self.adjacent])

    def add_neighbor(self, neighbor, uuid):
        self.adjacent[neighbor.uuid] = neighbor

    def delete_neighbor(self, neighbor):
        del self.adjacent[neighbor]

    def get_connections(self):
        return self.adjacent.keys()  

    def get_id(self):
        return self.id

    def get_weight(self, neighbor):
        return self.adjacent[neighbor]

                
class SocialGraph:

        def __init__(self):
                self.vert_dict = {}
                self.num_vertices = 0

        def __iter__(self):
                return iter(self.vert_dict.values())

        def add_user(self, user):
                if user.uuid in self.vert_dict:
                        raise UserAlreadyExistsError
                else:
                        self.num_vertices = self.num_vertices + 1
                        new_vertex = Vertex(user, user.uuid)
                        self.vert_dict[user.uuid] = new_vertex
                        return new_vertex
        
        def delete_user(self, user_uuid):
                del self.vert_dict[user_uuid]

        def get_user(self, user_uuid):
                if n in self.vert_dict:
                        return self.vert_dict[user_uuid]
                else:
                        raise UserDoesNotExistError()

        def follow(self, follower, followee):
                if follower not in self.vert_dict:
                        raise UserDoesNotExistError
                        
                if followee not in self.vert_dict:
                        raise UserDoesNotExistError
                        

                self.vert_dict[follower.uuid].add_neighbor(self.vert_dict[followee.uuid])
                
        
        def unfollow(self, follower, followee):
                self.vert_dict[follower.uuid].delete_neighbor(self.vert_dict[followee.uuid])

        def get_vertices(self):
                return self.vert_dict.keys()
                
        def following(self, user_uuid):
                return set(self.vert_dict[user_uuid].get_connections())
        
        def is_following(self, follower, followee):
                return followee in self.vert_dict[follower].adjacent