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
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
155
156
157
158
159
import uuid
import datetime


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.all_posts = []
        self.date = None

    def add_post(self, post_content):
        newpost = Post(self.uuid, post_content)
        self.all_posts += [newpost]
        if len(self.all_posts) > 50:
            number = len(self.all_posts) - 50
            post_content = post_content[number:]

    def get_post(self):
        if len(self.all_posts) != 0:
            for post in self.all_posts:
                yield post


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

    def print_post(self):
        return self.content, self.published_at


class SocialGraph:
    def __init__(self):
        self.all_users = []
        self.user_graph = {}

    def add_user(self, user):
        if user in self.all_users:
            raise Exception("UserAlreadyExistsError")
            self.all_users += [user]
            self.user_graph[user.uuid] = []

    def get_user(self, user_uuid):
        for x in self.all_users:
            if x.uuid == user_uuid:
                return x.full_name
        raise Exception("UserDoesNotExistError")

        return "Error"

    def delete_user(self, user_id):
        for x in self.all_users:
            if x.uuid == user_id:
                self.all_users.remove(x)
        if user_id in self.user_graph.keys():
            del self.user_graph[user_id]

    def follow(self, follower, followee):
        if follower in self.user_graph.keys():
            if followee not in self.user_graph[follower]:
                self.user_graph[follower] += [followee]

    def unfollow(self, follower, followee):
        if follower in self.user_graph.keys():
            if followee in self.user_graph[follower]:
                self.user_graph[follower].remove(followee)

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

    def followers(self, user_uuid):
        result = []
        for x in self.user_graph:
            for y in self.user_graph[x]:
                if y == user_uuid:
                    result += [x]
        return result

    def following(self, user_uuid):
        if user_uuid in self.user_graph.keys():
            return self.user_graph[user_uuid]
        return []

    def friends(self, user_uuid):
        result = []
        following_list = self.following(user_uuid)
        for x in self.followers(user_uuid):
            if x in following_list:
                result += [x]

    def min_distance(self, from_user_uuid, to_user_uuid):
        self.visited = [from_user_uuid]
        number = 0
        while to_user_uuid not in self.visited \
                and number < len(self.all_users):
            newwave = []
            for x in self.visited:
                for y in self.following(x):
                    if y not in self.visited:
                        newwave += [y]
            self.visited += newwave
            number += 1
            if len(self.all_users) == number \
                    and to_user_uuid not in self.visited:
                number = -1
                return number
        return number

    def nth_layer_followings(self, user_uuid, n):
        self.visited = [user_uuid]
        number = 0
        while(number < n):
            newwave = []
            for x in self.visited:
                for y in self.following(x):
                    if y not in self.visited:
                        if y not in newwave:
                            newwave += [y]
            self.visited += newwave
            number = number + 1
        return newwave

    def max_distance(self, from_user_uuid):
        maxdist = 0
        for x in self.all_users:
            dist = self.min_distance(from_user_uuid, x.uuid)
            if (dist < len(self.all_users) and dist > maxdist):
                maxdist = dist
        return maxdist

    def generate_feed(self, user_uuid, offset, limit):
        all_following_posts = {}
        following_list = []
        time_list = []
        result = []
        count = 0
        for x in self.following(user_uuid):
            for y in self.all_users:
                if y.uuid == x:
                    for post in y.all_posts:
                        all_following_posts[count] = [post.published_at]
                        following_list += [post.content]
                        count += 1
        for x in all_following_posts:
            time_list += [all_following_posts[x]]
        time_list = sorted(time_list)
        time_list = time_list[offset:(offset+limit)]
        for a in time_list:
            for b in all_following_posts:
                if a == all_following_posts[b]:
                    result += [following_list[b]]

        return result

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

EFFFEFF.
======================================================================
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
AttributeError: module 'solution' has no attribute 'UserAlreadyExistsError'

======================================================================
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
TypeError: argument of type 'NoneType' is not iterable

======================================================================
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_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
AssertionError: Lists differ: [] != ['39', '29', '19', '9', '38', '28', '18', '8', '37', '27']

Second list contains 10 additional elements.
First extra element 0:
39

- []
+ ['39', '29', '19', '9', '38', '28', '18', '8', '37', '27']

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

======================================================================
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: [] != [UUID('1f6c679a-3cab-47d0-8e34-8fa69a2426c3')]

Second list contains 1 additional elements.
First extra element 0:
1f6c679a-3cab-47d0-8e34-8fa69a2426c3

- []
+ [UUID('1f6c679a-3cab-47d0-8e34-8fa69a2426c3')]

======================================================================
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: 102 != 50

----------------------------------------------------------------------
Ran 8 tests in 0.080s

FAILED (failures=5, errors=2)

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

Николина обнови решението на 18.04.2016 10:38 (преди над 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
155
156
157
158
159
import uuid
import datetime


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.all_posts = []
        self.date = None

    def add_post(self, post_content):
        newpost = Post(self.uuid, post_content)
        self.all_posts += [newpost]
        if len(self.all_posts) > 50:
            number = len(self.all_posts) - 50
            post_content = post_content[number:]

    def get_post(self):
        if len(self.all_posts) != 0:
            for post in self.all_posts:
                yield post


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

    def print_post(self):
        return self.content, self.published_at


class SocialGraph:
    def __init__(self):
        self.all_users = []
        self.user_graph = {}

    def add_user(self, user):
        if user in self.all_users:
            raise Exception("UserAlreadyExistsError")
            self.all_users += [user]
            self.user_graph[user.uuid] = []

    def get_user(self, user_uuid):
        for x in self.all_users:
            if x.uuid == user_uuid:
                return x.full_name
        raise Exception("UserDoesNotExistError")

        return "Error"

    def delete_user(self, user_id):
        for x in self.all_users:
            if x.uuid == user_id:
                self.all_users.remove(x)
        if user_id in self.user_graph.keys():
            del self.user_graph[user_id]

    def follow(self, follower, followee):
        if follower in self.user_graph.keys():
            if followee not in self.user_graph[follower]:
                self.user_graph[follower] += [followee]

    def unfollow(self, follower, followee):
        if follower in self.user_graph.keys():
            if followee in self.user_graph[follower]:
                self.user_graph[follower].remove(followee)

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

    def followers(self, user_uuid):
        result = []
        for x in self.user_graph:
            for y in self.user_graph[x]:
                if y == user_uuid:
                    result += [x]
        return result

    def following(self, user_uuid):
        if user_uuid in self.user_graph.keys():
            return self.user_graph[user_uuid]
        return []

    def friends(self, user_uuid):
        result = []
        following_list = self.following(user_uuid)
        for x in self.followers(user_uuid):
            if x in following_list:
                result += [x]

    def min_distance(self, from_user_uuid, to_user_uuid):
        self.visited = [from_user_uuid]
        number = 0
        while to_user_uuid not in self.visited \
                and number < len(self.all_users):
            newwave = []
            for x in self.visited:
                for y in self.following(x):
                    if y not in self.visited:
                        newwave += [y]
            self.visited += newwave
            number += 1
            if len(self.all_users) == number \
                    and to_user_uuid not in self.visited:
                number = -1
                return number
        return number

    def nth_layer_followings(self, user_uuid, n):
        self.visited = [user_uuid]
        number = 0
        while(number < n):
            newwave = []
            for x in self.visited:
                for y in self.following(x):
                    if y not in self.visited:
                        if y not in newwave:
                            newwave += [y]
            self.visited += newwave
            number = number + 1
        return newwave

    def max_distance(self, from_user_uuid):
        maxdist = 0
        for x in self.all_users:
            dist = self.min_distance(from_user_uuid, x.uuid)
            if (dist < len(self.all_users) and dist > maxdist):
                maxdist = dist
        return maxdist

    def generate_feed(self, user_uuid, offset, limit):
        all_following_posts = {}
        following_list = []
        time_list = []
        result = []
        count = 0
        for x in self.following(user_uuid):
            for y in self.all_users:
                if y.uuid == x:
                    for post in y.all_posts:
                        all_following_posts[count] = [post.published_at]
                        following_list += [post.content]
                        count += 1
        for x in all_following_posts:
            time_list += [all_following_posts[x]]
        time_list = sorted(time_list)
        time_list = time_list[offset:(offset+limit)]
        for a in time_list:
            for b in all_following_posts:
                if a == all_following_posts[b]:
                    result += [following_list[b]]

        return result