-
Notifications
You must be signed in to change notification settings - Fork 0
/
302separation
executable file
·139 lines (127 loc) · 4.85 KB
/
302separation
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from sys import argv, stdout
from math import sqrt
# Do not modify this class, his default comportement is necessary for the program
class BadArgumentError(Exception):
def __init__(self, message, errors = "BadArgumentError"):
super().__init__(message)
self.errors = errors
# Default object index
class Separation():
""" Definition of Separation class """
def __init__(self, argument):
""" Initialise Separation's instance and check little errors """
self.dict, self.dict_tmp, sorted_dict = {}, {}, {}
self.fd = open(argument[1], "r")
self.buff = self.fd.read().rstrip().split('\n')
self.buff = [i.replace(" is friends with ", "|") for i in self.buff]
for i in self.buff:
self.dict_tmp[i.split('|')[0]] = []
[self.dict_tmp[i.split('|')[0]].append(i.split('|')[1]) for i in self.buff]
self.dict.update(self.dict_tmp)
for i in self.buff:
try:
[self.dict[i.split('|')[1]].append(i.split('|')[0])]
except KeyError:
self.dict[i.split('|')[1]] = []
[self.dict[i.split('|')[1]].append(i.split('|')[0])]
self.len = 0
for key, value in sorted(self.dict.items()):
sorted_dict[key] = value
self.len += 1
self.dict = sorted_dict
if len(argument) is 3:
self.n = int(argument[2])
elif len(argument) is 4:
self.p1, self.p2 = argument[2], argument[3]
def recursiveCheckFriend(self, count, startName, seenNameList):
""" Recursive function for check best path to link two people """
count += 1
seenNameList.append(startName)
if self.count is not -1 and count > self.count:
return
for i in self.dict[startName]:
tag = 0
if i == self.endName:
self.count = count
return
for j in seenNameList:
if j == i:
tag = 1
if tag == 0:
self.recursiveCheckFriend(count, i, seenNameList)
del seenNameList[len(seenNameList) - 1]
def findTowPeople(self, startName, endName):
""" Calculate value for ./302separation file p1 p2 """
self.seenName = []
self.startName = startName
self.endName = endName
self.count = -1
if self.startName == self.endName:
for i in self.dict:
if i == self.startName:
self.count = 0
if self.count != 0:
raise Exception("not a valid people")
return
self.recursiveCheckFriend(0, startName, [])
def printMatriceFriend(self, nb, mode = 0):
""" Print matrice of direct friend """
print(end=('\n' if mode is 1 else ''))
for i in self.dict:
k = 0
for j in self.dict:
isfriend = 0
self.findTowPeople(i, j)
if self.count is -1:
isfriend = 0
elif self.count <= nb:
isfriend = self.count
print(isfriend, end=(' ' if k < self.len - 1 else ''))
if k > 30:
exit(0)
k += 1
print()
print(end=('\n' if mode is 1 else ''))
def findAllPeople(self, nb):
""" Calculate value for ./302separation file n """
[print(i) for i in self.dict]
self.printMatriceFriend(1, 1)
self.printMatriceFriend(nb)
def computeValue(self):
""" Calculate all printable value required by printValue method """
if len(argv) is 4:
self.findTowPeople(self.p1, self.p2)
print("degree of separation between {} and {}: {}"
.format(self.startName, self.endName, self.count))
else:
self.findAllPeople(self.n)
def printValue(self):
""" Print all computed value into the tab """
def manHelp():
""" This function will print the help
>>> manHelp()
USAGE
./302separation file (n | p1 p2)"""
print("USAGE\n\t../302separation file (n | p1 p2)")
# Do not put more information in this function, it's must be clearer as possible
def main():
""" Main function who perform program's core action like arguments resolution """
if len(argv) is not 3 and len(argv) is not 4:
raise BadArgumentError("You must pass a valid file's name")
if argv[1] == "-h":
manHelp()
exit(0)
obj = Separation(argv)
obj.computeValue()
obj.printValue()
# Don't touch at this except if u don't worry of problems
if __name__ == "__main__":
try:
main()
except BaseException as error:
stdout.write(str(type(error).__name__) + ": {}\n".format(error))
exit(84)
except SystemExit:
exit(0)