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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import datetime
import uuid
import queue
from collections import defaultdict

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

    def __cmp__(self, other):
        return cmp(self.published_at, other.published_at)


class User:
    def __init__(self, name):
        self.name = name
        self.uuid = uuid.uuid4().int
        self.posts = queue.Queue(50)

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

    def get_post(self):
        length = self.posts.qsize() - 1
        while length >= 0:
            yield self.posts.queue[length]
            length -= 1


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass

    
class SocialGraph:
    def __init__(self):
        self.users = defaultdict()
        self.followers = defaultdict()

    def add_user(self, user):
        if user.uuid in self.users:
            raise UserAlreadyExistsError()
        self.users[user.uuid] = user
        self.followers[user.uuid] = set()

    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):
        if user_uuid not in self.users:
            raise UserDoesNotExistError()
        if user_uuid in self.users:
            self.users.pop(user_uuid)
        for key in self.followers:
            self.followers[key].discard(user_uuid)

    def follow(self, follower, followee):
        self.followers[followee].add(follower)

    def unfollow(self, follower, followee):
        self.followers[followee].discard(follower)

    def is_following(self, follower, followee):
        if follower in self.followers[followee]:
            return True
        return False

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

    def following(self, user_uuid):
        fol = set()
        for key in self.followers:
            if user_uuid in self.followers[key]:
                fol.add(key)
        return fol

    def friends(self, user_uuid):
        fr = set()
        for user in self.followers[user_uuid]:
            if user_uuid in self.followers[user]:
                fr.add(user)
        return fr

    def min_distance(self, from_user_uuid, to_user_uuid):
        start, dist = from_user_uuid, 0
        q = queue.Queue()
        visited = []
        q.put((start,dist))
        while not q.empty():
            st = q.get()
            if st[0] == to_user_uuid:
                return st[1]
            visited.append(st[0])
            dist += 1
            for nu in self.followers[st[0]]:
                if nu not in visited:
                    q.put((nu,dist))
        raise UsersNotConnectedError()

    def nth_layer_followings(self, user_uuid, n):
        start, dist = user_uuid, 0
        q = queue.Queue()
        visited = []
        layer = []
        q.put((start,dist))
        while not q.empty():
            st = q.get()
            if st[1] == n:
                layer.append(st[0])
            visited.append(st[0])
            dist += 1
            for nu in self.followers[st[0]]:
                if nu not in visited:
                    q.put((nu,dist))
        return layer

    def max_distance(self, user_uuid):
        start, dist = user_uuid, 0
        q = queue.Queue()
        visited = []
        maximum = 0
        q.put((start,dist))
        while not q.empty():
            st = q.get()
            if st[1] > maximum:
                maximum = st[1]
            visited.append(st[0])
            dist += 1
            for nu in self.followers[st[0]]:
                if nu not in visited:
                    q.put((nu,dist))
        return maximum
        
    def generate_feed(self, user_uuid, offset=0, limit=10):
        posts = []
        for user in self.followers[user_uuid]:
            for post in get_user(user).get_post():
                posts.append(post)
        posts.sort()
        return posts[offset,limit+1]

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

.FEE.F..
======================================================================
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: list indices must be integers or slices, not tuple

======================================================================
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: 'collections.defaultdict' object is not callable

======================================================================
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: 0 != 2

======================================================================
FAIL: 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
AssertionError: Lists differ: [] != [277753206363883021205503990808683739250]

Second list contains 1 additional elements.
First extra element 0:
277753206363883021205503990808683739250

- []
+ [277753206363883021205503990808683739250]

----------------------------------------------------------------------
Ran 8 tests in 0.070s

FAILED (failures=2, errors=2)

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

Димо обнови решението на 18.04.2016 01:15 (преди над 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import datetime
import uuid
import queue
from collections import defaultdict

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

    def __cmp__(self, other):
        return cmp(self.published_at, other.published_at)


class User:
    def __init__(self, name):
        self.name = name
        self.uuid = uuid.uuid4().int
        self.posts = queue.Queue(50)

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

    def get_post(self):
        length = self.posts.qsize() - 1
        while length >= 0:
            yield self.posts.queue[length]
            length -= 1


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass


class UsersNotConnectedError(Exception):
    pass

    
class SocialGraph:
    def __init__(self):
        self.users = defaultdict()
        self.followers = defaultdict()

    def add_user(self, user):
        if user.uuid in self.users:
            raise UserAlreadyExistsError()
        self.users[user.uuid] = user
        self.followers[user.uuid] = set()

    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):
        if user_uuid not in self.users:
            raise UserDoesNotExistError()
        if user_uuid in self.users:
            self.users.pop(user_uuid)
        for key in self.followers:
            self.followers[key].discard(user_uuid)

    def follow(self, follower, followee):
        self.followers[followee].add(follower)

    def unfollow(self, follower, followee):
        self.followers[followee].discard(follower)

    def is_following(self, follower, followee):
        if follower in self.followers[followee]:
            return True
        return False

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

    def following(self, user_uuid):
        fol = set()
        for key in self.followers:
            if user_uuid in self.followers[key]:
                fol.add(key)
        return fol

    def friends(self, user_uuid):
        fr = set()
        for user in self.followers[user_uuid]:
            if user_uuid in self.followers[user]:
                fr.add(user)
        return fr

    def min_distance(self, from_user_uuid, to_user_uuid):
        start, dist = from_user_uuid, 0
        q = queue.Queue()
        visited = []
        q.put((start,dist))
        while not q.empty():
            st = q.get()
            if st[0] == to_user_uuid:
                return st[1]
            visited.append(st[0])
            dist += 1
            for nu in self.followers[st[0]]:
                if nu not in visited:
                    q.put((nu,dist))
        raise UsersNotConnectedError()

    def nth_layer_followings(self, user_uuid, n):
        start, dist = user_uuid, 0
        q = queue.Queue()
        visited = []
        layer = []
        q.put((start,dist))
        while not q.empty():
            st = q.get()
            if st[1] == n:
                layer.append(st[0])
            visited.append(st[0])
            dist += 1
            for nu in self.followers[st[0]]:
                if nu not in visited:
                    q.put((nu,dist))
        return layer

    def max_distance(self, user_uuid):
        start, dist = user_uuid, 0
        q = queue.Queue()
        visited = []
        maximum = 0
        q.put((start,dist))
        while not q.empty():
            st = q.get()
            if st[1] > maximum:
                maximum = st[1]
            visited.append(st[0])
            dist += 1
            for nu in self.followers[st[0]]:
                if nu not in visited:
                    q.put((nu,dist))
        return maximum
        
    def generate_feed(self, user_uuid, offset=0, limit=10):
        posts = []
        for user in self.followers[user_uuid]:
            for post in get_user(user).get_post():
                posts.append(post)
        posts.sort()
        return posts[offset,limit+1]