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
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
import uuid 
import datetime 
class User(object): 
    def __init__(self,full_name): 
        self.full_name=full_name 
        self.uuid=uuid.uuid4() 
        self.UserPosts=[] 
    def add_post(self, post_content): 
        a = Post(self.uuid,post_content) 
        if len(self.UserPosts)<50: 
            self.UserPosts.append(a) 
        else: 
            del self.UserPosts[0] 
            self.UserPosts.append(a) 

    
                        
            
    def get_post(self): 
        
        for post in self.UserPosts: 
            yield post 
            
        raise StopIteration 
    
    
        
class Post(object): 
    def __init__(self,author,content): 
        self.author=author 
        self.published_at=datetime.datetime.now().timetuple() 
        self.content=content 

class SocialGraph(object): 
    def __init__(self): 
        self.graph={} 
    def add_user(self, user):
        if user.uuid in self.graph.keys(): 
            raise UserAlreadyExistsError
        self.graph[user.uuid]=(user,user.uuid,user.UserPosts,[]) 
    def get_user(self, user_uuid):
        if user_uuid not in self.graph.keys():
            raise UserDoesNotExistError
        for key,value in self.graph.items(): 
            if key == user_uuid: 
                return value[0]
    def delete_user(self, user_uuid):
        if user_uuid not in self.graph.keys():
            raise UserDoesNotExistError
        del self.graph[user_uuid] 
    def follow(self, follower, followee): 
        for uuids in self.graph[followee][3]: 
            if uuids==follower: 
                return 
        self.graph[followee][3].append(follower) 
    def unfollow(self, follower, followee): 
        for uuids in self.graph[followee][3]: 
            if uuids==follower: 
                self.graph[followee][3].remove(follower) 
    def is_following(self, follower, followee): 
        for uuids in self.graph[followee][3]: 
            if uuids==follower: 
                return True 
        return False 
    def followers(self, user_uuid): 
        return self.graph[user_uuid][3] 
    def following(self, user_uuid): 
        values=set() 
        for key,value in self.graph.items(): 
            if user_uuid in self.graph[key][3]: 
                values.add(key) 
        return values 
    def friends(self, user_uuid): 
        values=set() 
        for key,value in self.graph.items(): 
            if self.is_following(key,user_uuid) and self.is_following(user_uuid,key): 
                values.add(key) 
        return values 
    def max_distance(self, user_uuid): 
        max=0 
        for key in self.graph.keys(): 
            if self.min_distance(user_uuid,key)>max: 
                max=self.min_distance(user_uuid,key) 
        return max 
       
    result = 0        
    taken = set() 
    curent=set() 
    def min_distance(self, from_user_uuid, to_user_uuid):
        
        self.result+= 1
        if to_user_uuid in self.graph[from_user_uuid][3]: 
            return self.result 
        else: 
            for members in self.graph[from_user_uuid][3]: 
                self.min_distance( member , to_user_uuid) 
        if self.result == 0: 
            raise UsersNotConnectedError 
        return self.result
    
   
        
            
    def generate_feed(self, user_uuid, offset=0, limit=10): 
        value=self.following(user_uuid) 
        result=[] 
        for user in value: 
            for item in self.graph[user][3][offset:offset+limit]: 
                result.append(item) 
        return result 
            
            
            
class UserDoesNotExistError(Exception): 
    def __init__(self): 
        self.msg = "UserDoesNotExistError" 
class UserAlreadyExistsError(Exception): 
    def __init__(self): 
        self.msg = "UserAlreadyExistsError" 
class UsersNotConnectedError(Exception): 
    def __init__(self): 
        self.msg = "UsersNotConnectedError" 

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

.FEF.EF.
======================================================================
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: 'UUID' object has no attribute 'content'

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

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

======================================================================
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_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: False is not true

----------------------------------------------------------------------
Ran 8 tests in 0.064s

FAILED (failures=3, errors=2)

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

Емануил обнови решението на 18.04.2016 13:20 (преди над 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
import uuid 
import datetime 
class User(object): 
    def __init__(self,full_name): 
        self.full_name=full_name 
        self.uuid=uuid.uuid4() 
        self.UserPosts=[] 
    def add_post(self, post_content): 
        a = Post(self.uuid,post_content) 
        if len(self.UserPosts)<50: 
            self.UserPosts.append(a) 
        else: 
            del self.UserPosts[0] 
            self.UserPosts.append(a) 

    
                        
            
    def get_post(self): 
        
        for post in self.UserPosts: 
            yield post 
            
        raise StopIteration 
    
    
        
class Post(object): 
    def __init__(self,author,content): 
        self.author=author 
        self.published_at=datetime.datetime.now().timetuple() 
        self.content=content 

class SocialGraph(object): 
    def __init__(self): 
        self.graph={} 
    def add_user(self, user):
        if user.uuid in self.graph.keys(): 
            raise UserAlreadyExistsError
        self.graph[user.uuid]=(user,user.uuid,user.UserPosts,[]) 
    def get_user(self, user_uuid):
        if user_uuid not in self.graph.keys():
            raise UserDoesNotExistError
        for key,value in self.graph.items(): 
            if key == user_uuid: 
                return value[0]
    def delete_user(self, user_uuid):
        if user_uuid not in self.graph.keys():
            raise UserDoesNotExistError
        del self.graph[user_uuid] 
    def follow(self, follower, followee): 
        for uuids in self.graph[followee][3]: 
            if uuids==follower: 
                return 
        self.graph[followee][3].append(follower) 
    def unfollow(self, follower, followee): 
        for uuids in self.graph[followee][3]: 
            if uuids==follower: 
                self.graph[followee][3].remove(follower) 
    def is_following(self, follower, followee): 
        for uuids in self.graph[followee][3]: 
            if uuids==follower: 
                return True 
        return False 
    def followers(self, user_uuid): 
        return self.graph[user_uuid][3] 
    def following(self, user_uuid): 
        values=set() 
        for key,value in self.graph.items(): 
            if user_uuid in self.graph[key][3]: 
                values.add(key) 
        return values 
    def friends(self, user_uuid): 
        values=set() 
        for key,value in self.graph.items(): 
            if self.is_following(key,user_uuid) and self.is_following(user_uuid,key): 
                values.add(key) 
        return values 
    def max_distance(self, user_uuid): 
        max=0 
        for key in self.graph.keys(): 
            if self.min_distance(user_uuid,key)>max: 
                max=self.min_distance(user_uuid,key) 
        return max 
       
    result = 0        
    taken = set() 
    curent=set() 
    def min_distance(self, from_user_uuid, to_user_uuid):
        
        self.result+= 1
        if to_user_uuid in self.graph[from_user_uuid][3]: 
            return self.result 
        else: 
            for members in self.graph[from_user_uuid][3]: 
                self.min_distance( member , to_user_uuid) 
        if self.result == 0: 
            raise UsersNotConnectedError 
        return self.result
    
   
        
            
    def generate_feed(self, user_uuid, offset=0, limit=10): 
        value=self.following(user_uuid) 
        result=[] 
        for user in value: 
            for item in self.graph[user][3][offset:offset+limit]: 
                result.append(item) 
        return result 
            
            
            
class UserDoesNotExistError(Exception): 
    def __init__(self): 
        self.msg = "UserDoesNotExistError" 
class UserAlreadyExistsError(Exception): 
    def __init__(self): 
        self.msg = "UserAlreadyExistsError" 
class UsersNotConnectedError(Exception): 
    def __init__(self): 
        self.msg = "UsersNotConnectedError"