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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import uuid
# from collections import defaultdict
from datetime import datetime


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

        def add_post(self, post_content):
                if (len(self.posts) <= 50):
                        post = Post(self.uuid, datetime.now(), post_content)
                        self.posts.append(post)
                else:
                        self.posts.pop(0)
                        add_post(self, post_content)

        def get_post(self):
                for post in self.posts[::-1]:
                        yield post

        def __repr__(self):
                return self.full_name #+ " " + str(self.uuid)


class Post:
        def __init__(self, author, published_at, content):
                self.author = author
                self.published_at = published_at
                self.content = content
        def __repr__(self):
                return  "{2} ({1})\n".format(str(self.author), self.published_at, self.content)

class SocialGraph:

        def __init__(self):
                self.graph = {} #defaultdict(list)
                self.users = {}

        def __is_registered(self, user_uuid):
                return user_uuid in self.graph.keys()

        def add_user(self, user):
                if(user.uuid not in self.graph.keys()):
                        self.graph[user.uuid] = set()
                        self.users[user.uuid] = user
                else:
                        raise UserAlreadyExistError(user)

        def get_user(self, user_uuid):
                try:
                        return self.users[user_uuid]
                except KeyError:
                        raise UserDoesNotExistError(user_uuid)

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

        def follow(self, follower_uuid, followee_uuid):
                if not self.__is_registered(followee_uuid):
                        raise UserDoesNotExistError(followee_uuid)
                if not self.__is_registered(follower_uuid):
                        raise UserDoesNotExistError(follower_uuid)
                else:
                        self.graph[follower_uuid].add(followee_uuid)
                        
                # todo follwing

        def unfollow(self, follower_uuid, followee_uuid):
                self.graph[follower_uuid].remove(followee_uuid)

        def is_following(self, follower, followee):
                return followee.uuid in self.graph[follower.uuid]

        def following(self, user_uuid):
                return self.graph[user_uuid]

        def friends(self, user_uuid):
                {other for other in self.graph.keys() if self.is_following(user_uuid, other) and self.is_following(other, user_uuid)}

        def __DFS(self,v,seen=None,path=None):
                if seen is None: seen = []
                if path is None: path = [v]
                
                seen.append(v)
                
                paths = []
                for followee in self.following(v):
                        if followee not in seen:
                                followee_path = path + [followee]
                                paths.append(tuple(followee_path))
                                paths.extend(self.__DFS(followee, seen, followee_path))
                return paths


        def __max_distance_helper(self, user_uuid, count, distances, visited):
                visited.append(user_uuid)
                print(visited)

                if(len(self.following(user_uuid)) == 0):
                        return count

                for followee in self.following(user_uuid):
                        if (followee not in visited):
                                distances.append(self.__max_distance_helper(followee, count + 1, distances, visited))

                return max(distances)

        def max_distance(self, user_uuid):
                all_paths = self.__DFS(user_uuid)
                return len(max(all_paths, key=lambda l: len(l)))

        def __find_shortest_path(self, start, end, path=[]):
                path = path + [start]
                if start == end:
                        return path
                
                shortest = []
                for node in self.graph[start]:
                        if node not in path:
                                newpath = self.__find_shortest_path(node, end, path)
                                if newpath:
                                        if not shortest or len(newpath) < len(shortest):
                                                shortest = newpath
                
                return shortest

        def min_distance(self, from_user_uuid, to_user_uuid):
                min_path = self.__find_shortest_path(from_user_uuid, to_user_uuid)
                if (len(min_path) == 0):
                        raise UsersNotConnectedError(from_user_uuid, to_user_uuid, self.users)
                else:
                        return len(min_path) - 1 
                
        def __nth_layer_followings_helper(self, followees, user_uuid, n, depth, visited):
                 if(n==depth):
                         return followees

                 visited.append(user_uuid)
                 followees = self.following(user_uuid)

                 for followee in followees:
                         if (followee not in visited):
                                 f = self.__nth_layer_followings_helper(followees, followee, n, depth+1, visited)
                                 followees.update(f)
                                 return f
                 return []

        def nth_layer_followings(self, user_uuid, n):
                 return self.__nth_layer_followings_helper([], user_uuid, n, 0,[])


        def generate_feed(self, user_uuid, offset=0, limit=10):
                all_posts = []
                for followee_uuid in self.following(user_uuid):
                        all_posts.extend(self.users[followee_uuid].get_post())

                all_posts.sort(key=lambda p: p.published_at, reverse=True)
                return all_posts[offset:limit+offset]
                
                

class UserDoesNotExistError(Exception):
        def __init__(self, user_uuid):
                self.user_uuid = user_uuid

        def __str__(self):
                return "User with id: " + self.user_uuid + " does not exist!"


class UserAlreadyExistError(Exception):
        def __init__(self, user):
                self.user = user

        def __str__(self):
                return "User with name " + self.user.full_name + " already exist!"


class UsersNotConnectedError(Exception):
        def __init__(self, user1_uuid, user2_uuid, users):
                self.user1_uuid = user1_uuid
                self.user2_uuid = user2_uuid
                self.users = users

        def __str__(self):
                return "The users {0} and {1} are not connected!".format(self.users[self.user1_uuid], self.users[self.user2_uuid])

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

EFFEE.E.
======================================================================
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_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
AttributeError: 'UUID' object has no attribute 'uuid'

======================================================================
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: 'UUID' object has no attribute 'uuid'

======================================================================
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
NameError: name 'add_post' is not defined

======================================================================
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: 3 != 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: ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0'] != ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

First differing element 0:
9
0

- ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
+ ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

----------------------------------------------------------------------
Ran 8 tests in 0.065s

FAILED (failures=2, errors=4)

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

Христо обнови решението на 18.04.2016 12: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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import uuid
from datetime import datetime


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

        def add_post(self, post_content):
                if (len(self.posts) <= 50):
                        post = Post(self.uuid, datetime.now(), post_content)
                        self.posts.append(post)
                else:
                        self.posts.pop(0)
                        add_post(self, post_content)

        def get_post(self):
                for post in self.posts[::-1]:
                        yield post

        def __repr__(self):
                return self.full_name #+ " " + str(self.uuid)


class Post:
        def __init__(self, author, published_at, content):
                self.author = author
                self.published_at = published_at
                self.content = content
        def __repr__(self):
                return  "{2} ({1})\n".format(str(self.author), self.published_at, self.content)

class SocialGraph:

        def __init__(self):
                self.graph = {}
                self.users = {}

        def __is_registered(self, user_uuid):
                return user_uuid in self.graph.keys()

        def add_user(self, user):
                if(user.uuid not in self.graph.keys()):
                        self.graph[user.uuid] = set()
                        self.users[user.uuid] = user
                else:
                        raise UserAlreadyExistError(user)

        def get_user(self, user_uuid):
                try:
                        return self.users[user_uuid]
                except KeyError:
                        raise UserDoesNotExistError(user_uuid)

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

        def follow(self, follower_uuid, followee_uuid):
                if not self.__is_registered(followee_uuid):
                        raise UserDoesNotExistError(followee_uuid)
                if not self.__is_registered(follower_uuid):
                        raise UserDoesNotExistError(follower_uuid)
                else:
                        self.graph[follower_uuid].add(followee_uuid)
                        
                # todo follwing

        def unfollow(self, follower_uuid, followee_uuid):
                self.graph[follower_uuid].remove(followee_uuid)

        def is_following(self, follower, followee):
                return followee.uuid in self.graph[follower.uuid]

        def following(self, user_uuid):
                return self.graph[user_uuid]

        def friends(self, user_uuid):
                {other for other in self.graph.keys() if self.is_following(user_uuid, other) and self.is_following(other, user_uuid)}

        def __DFS(self,v,seen=None,path=None):
                if seen is None: seen = []
                if path is None: path = [v]
                
                seen.append(v)
                
                paths = []
                for followee in self.following(v):
                        if followee not in seen:
                                followee_path = path + [followee]
                                paths.append(tuple(followee_path))
                                paths.extend(self.__DFS(followee, seen, followee_path))
                return paths


        def __max_distance_helper(self, user_uuid, count, distances, visited):
                visited.append(user_uuid)
                print(visited)

                if(len(self.following(user_uuid)) == 0):
                        return count

                for followee in self.following(user_uuid):
                        if (followee not in visited):
                                distances.append(self.__max_distance_helper(followee, count + 1, distances, visited))

                return max(distances)

        def max_distance(self, user_uuid):
                all_paths = self.__DFS(user_uuid)
                # return len(max(all_paths, key=lambda l: len(l)))
                path = max(all_paths, key=lambda l: len(l))
                for uuid in path:
                        print (self.users[uuid])
                return path

                # return self.__max_distance_helper(user_uuid, 0, [], [])

        def __find_shortest_path(self, start, end, path=[]):
                path = path + [start]
                if start == end:
                        return path
                
                shortest = []
                for node in self.graph[start]:
                        if node not in path:
                                newpath = self.__find_shortest_path(node, end, path)
                                if newpath:
                                        if not shortest or len(newpath) < len(shortest):
                                                shortest = newpath
                
                return shortest

        def min_distance(self, from_user_uuid, to_user_uuid):
                min_path = self.__find_shortest_path(from_user_uuid, to_user_uuid)
                if (len(min_path) == 0):
                        raise UsersNotConnectedError(from_user_uuid, to_user_uuid, self.users)
                else:
                        return len(min_path) - 1 
                
        # def __nth_layer_followings_helper(self, folowees, user_uuid, n, depth):
        #         if(n==depth):
        #                 return followees

        #         visited.append(user_uuid)

        #         for followee in self.following(user_uuid):
        #                 if (followee not in visited):
        #                         self.__nth_layer_followings_helper(self.following(user_uuid), followee,n,depth+1,visited)

        # def nth_layer_followings(self, user_uuid, n):
        #         return self.__nth_layer_followings_helper([], user_uuid, n, 0,[])


        def generate_feed(self, user_uuid, offset=0, limit=10):
                all_posts = []
                for followee_uuid in self.following(user_uuid):
                        all_posts.extend(self.users[followee_uuid].get_post())

                all_posts.sort(key=lambda p: p.published_at, reverse=True)
                return all_posts[offset:limit+offset]
                
                

class UserDoesNotExistError(Exception):
        def __init__(self, user_uuid):
                self.user_uuid = user_uuid

        def __str__(self):
                return "User with id: " + self.user_uuid + " does not exist!"


class UserAlreadyExistError(Exception):
        def __init__(self, user):
                self.user = user

        def __str__(self):
                return "User with name " + self.user.full_name + " already exist!"


class UsersNotConnectedError(Exception):
        def __init__(self, user1_uuid, user2_uuid, users):
                self.user1_uuid = user1_uuid
                self.user2_uuid = user2_uuid
                self.users = users

        def __str__(self):
                return "The users {0} and {1} are not connected!".format(self.users[self.user1_uuid], self.users[self.user2_uuid])

Христо обнови решението на 18.04.2016 16:37 (преди над 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import uuid
# from collections import defaultdict
from datetime import datetime


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

        def add_post(self, post_content):
                if (len(self.posts) <= 50):
                        post = Post(self.uuid, datetime.now(), post_content)
                        self.posts.append(post)
                else:
                        self.posts.pop(0)
                        add_post(self, post_content)

        def get_post(self):
                for post in self.posts[::-1]:
                        yield post

        def __repr__(self):
                return self.full_name #+ " " + str(self.uuid)


class Post:
        def __init__(self, author, published_at, content):
                self.author = author
                self.published_at = published_at
                self.content = content
        def __repr__(self):
                return  "{2} ({1})\n".format(str(self.author), self.published_at, self.content)

class SocialGraph:

        def __init__(self):
                self.graph = {} #defaultdict(list)
                self.users = {}

        def __is_registered(self, user_uuid):
                return user_uuid in self.graph.keys()

        def add_user(self, user):
                if(user.uuid not in self.graph.keys()):
                        self.graph[user.uuid] = set()
                        self.users[user.uuid] = user
                else:
                        raise UserAlreadyExistError(user)

        def get_user(self, user_uuid):
                try:
                        return self.users[user_uuid]
                except KeyError:
                        raise UserDoesNotExistError(user_uuid)

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

        def follow(self, follower_uuid, followee_uuid):
                if not self.__is_registered(followee_uuid):
                        raise UserDoesNotExistError(followee_uuid)
                if not self.__is_registered(follower_uuid):
                        raise UserDoesNotExistError(follower_uuid)
                else:
                        self.graph[follower_uuid].add(followee_uuid)
                        
                # todo follwing

        def unfollow(self, follower_uuid, followee_uuid):
                self.graph[follower_uuid].remove(followee_uuid)

        def is_following(self, follower, followee):
                return followee.uuid in self.graph[follower.uuid]

        def following(self, user_uuid):
                return self.graph[user_uuid]

        def friends(self, user_uuid):
                {other for other in self.graph.keys() if self.is_following(user_uuid, other) and self.is_following(other, user_uuid)}

        def __DFS(self,v,seen=None,path=None):
                if seen is None: seen = []
                if path is None: path = [v]
                
                seen.append(v)
                
                paths = []
                for followee in self.following(v):
                        if followee not in seen:
                                followee_path = path + [followee]
                                paths.append(tuple(followee_path))
                                paths.extend(self.__DFS(followee, seen, followee_path))
                return paths


        def __max_distance_helper(self, user_uuid, count, distances, visited):
                visited.append(user_uuid)
                print(visited)

                if(len(self.following(user_uuid)) == 0):
                        return count

                for followee in self.following(user_uuid):
                        if (followee not in visited):
                                distances.append(self.__max_distance_helper(followee, count + 1, distances, visited))

                return max(distances)

        def max_distance(self, user_uuid):
                all_paths = self.__DFS(user_uuid)
                return len(max(all_paths, key=lambda l: len(l)))

        def __find_shortest_path(self, start, end, path=[]):
                path = path + [start]
                if start == end:
                        return path
                
                shortest = []
                for node in self.graph[start]:
                        if node not in path:
                                newpath = self.__find_shortest_path(node, end, path)
                                if newpath:
                                        if not shortest or len(newpath) < len(shortest):
                                                shortest = newpath
                
                return shortest

        def min_distance(self, from_user_uuid, to_user_uuid):
                min_path = self.__find_shortest_path(from_user_uuid, to_user_uuid)
                if (len(min_path) == 0):
                        raise UsersNotConnectedError(from_user_uuid, to_user_uuid, self.users)
                else:
                        return len(min_path) - 1 
                
        def __nth_layer_followings_helper(self, followees, user_uuid, n, depth, visited):
                 if(n==depth):
                         return followees

                 visited.append(user_uuid)
                 followees = self.following(user_uuid)

                 for followee in followees:
                         if (followee not in visited):
                                 f = self.__nth_layer_followings_helper(followees, followee, n, depth+1, visited)
                                 followees.update(f)
                                 return f
                 return []

        def nth_layer_followings(self, user_uuid, n):
                 return self.__nth_layer_followings_helper([], user_uuid, n, 0,[])


        def generate_feed(self, user_uuid, offset=0, limit=10):
                all_posts = []
                for followee_uuid in self.following(user_uuid):
                        all_posts.extend(self.users[followee_uuid].get_post())

                all_posts.sort(key=lambda p: p.published_at, reverse=True)
                return all_posts[offset:limit+offset]
                
                

class UserDoesNotExistError(Exception):
        def __init__(self, user_uuid):
                self.user_uuid = user_uuid

        def __str__(self):
                return "User with id: " + self.user_uuid + " does not exist!"


class UserAlreadyExistError(Exception):
        def __init__(self, user):
                self.user = user

        def __str__(self):
                return "User with name " + self.user.full_name + " already exist!"


class UsersNotConnectedError(Exception):
        def __init__(self, user1_uuid, user2_uuid, users):
                self.user1_uuid = user1_uuid
                self.user2_uuid = user2_uuid
                self.users = users

        def __str__(self):
                return "The users {0} and {1} are not connected!".format(self.users[self.user1_uuid], self.users[self.user2_uuid])

Христо обнови решението на 18.04.2016 16: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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import uuid
# from collections import defaultdict
from datetime import datetime


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

        def add_post(self, post_content):
                if (len(self.posts) <= 50):
                        post = Post(self.uuid, datetime.now(), post_content)
                        self.posts.append(post)
                else:
                        self.posts.pop(0)
                        add_post(self, post_content)

        def get_post(self):
                for post in self.posts[::-1]:
                        yield post

        def __repr__(self):
                return self.full_name #+ " " + str(self.uuid)


class Post:
        def __init__(self, author, published_at, content):
                self.author = author
                self.published_at = published_at
                self.content = content
        def __repr__(self):
                return  "{2} ({1})\n".format(str(self.author), self.published_at, self.content)

class SocialGraph:

        def __init__(self):
                self.graph = {} #defaultdict(list)
                self.users = {}

        def __is_registered(self, user_uuid):
                return user_uuid in self.graph.keys()

        def add_user(self, user):
                if(user.uuid not in self.graph.keys()):
                        self.graph[user.uuid] = set()
                        self.users[user.uuid] = user
                else:
                        raise UserAlreadyExistError(user)

        def get_user(self, user_uuid):
                try:
                        return self.users[user_uuid]
                except KeyError:
                        raise UserDoesNotExistError(user_uuid)

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

        def follow(self, follower_uuid, followee_uuid):
                if not self.__is_registered(followee_uuid):
                        raise UserDoesNotExistError(followee_uuid)
                if not self.__is_registered(follower_uuid):
                        raise UserDoesNotExistError(follower_uuid)
                else:
                        self.graph[follower_uuid].add(followee_uuid)
                        
                # todo follwing

        def unfollow(self, follower_uuid, followee_uuid):
                self.graph[follower_uuid].remove(followee_uuid)

        def is_following(self, follower, followee):
                return followee.uuid in self.graph[follower.uuid]

        def following(self, user_uuid):
                return self.graph[user_uuid]

        def friends(self, user_uuid):
                {other for other in self.graph.keys() if self.is_following(user_uuid, other) and self.is_following(other, user_uuid)}

        def __DFS(self,v,seen=None,path=None):
                if seen is None: seen = []
                if path is None: path = [v]
                
                seen.append(v)
                
                paths = []
                for followee in self.following(v):
                        if followee not in seen:
                                followee_path = path + [followee]
                                paths.append(tuple(followee_path))
                                paths.extend(self.__DFS(followee, seen, followee_path))
                return paths


        def __max_distance_helper(self, user_uuid, count, distances, visited):
                visited.append(user_uuid)
                print(visited)

                if(len(self.following(user_uuid)) == 0):
                        return count

                for followee in self.following(user_uuid):
                        if (followee not in visited):
                                distances.append(self.__max_distance_helper(followee, count + 1, distances, visited))

                return max(distances)

        def max_distance(self, user_uuid):
                all_paths = self.__DFS(user_uuid)
                return len(max(all_paths, key=lambda l: len(l)))

        def __find_shortest_path(self, start, end, path=[]):
                path = path + [start]
                if start == end:
                        return path
                
                shortest = []
                for node in self.graph[start]:
                        if node not in path:
                                newpath = self.__find_shortest_path(node, end, path)
                                if newpath:
                                        if not shortest or len(newpath) < len(shortest):
                                                shortest = newpath
                
                return shortest

        def min_distance(self, from_user_uuid, to_user_uuid):
                min_path = self.__find_shortest_path(from_user_uuid, to_user_uuid)
                if (len(min_path) == 0):
                        raise UsersNotConnectedError(from_user_uuid, to_user_uuid, self.users)
                else:
                        return len(min_path) - 1 
                
        def __nth_layer_followings_helper(self, followees, user_uuid, n, depth, visited):
                 if(n==depth):
                         return followees

                 visited.append(user_uuid)
                 followees = self.following(user_uuid)

                 for followee in followees:
                         if (followee not in visited):
                                 f = self.__nth_layer_followings_helper(followees, followee, n, depth+1, visited)
                                 followees.update(f)
                                 return f
                 return []

        def nth_layer_followings(self, user_uuid, n):
                 return self.__nth_layer_followings_helper([], user_uuid, n, 0,[])


        def generate_feed(self, user_uuid, offset=0, limit=10):
                all_posts = []
                for followee_uuid in self.following(user_uuid):
                        all_posts.extend(self.users[followee_uuid].get_post())

                all_posts.sort(key=lambda p: p.published_at, reverse=True)
                return all_posts[offset:limit+offset]
                
                

class UserDoesNotExistError(Exception):
        def __init__(self, user_uuid):
                self.user_uuid = user_uuid

        def __str__(self):
                return "User with id: " + self.user_uuid + " does not exist!"


class UserAlreadyExistError(Exception):
        def __init__(self, user):
                self.user = user

        def __str__(self):
                return "User with name " + self.user.full_name + " already exist!"


class UsersNotConnectedError(Exception):
        def __init__(self, user1_uuid, user2_uuid, users):
                self.user1_uuid = user1_uuid
                self.user2_uuid = user2_uuid
                self.users = users

        def __str__(self):
                return "The users {0} and {1} are not connected!".format(self.users[self.user1_uuid], self.users[self.user2_uuid])