-
Notifications
You must be signed in to change notification settings - Fork 0
/
igloo.py
84 lines (77 loc) · 2.8 KB
/
igloo.py
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
# igloo.py
# the connections will be represented by an undirected graph
# this will be implemented using adjacency lists
# this version is simplified in that it treats connections as bidirectional and does not account for verified/unverified connections
# returns 2nd degree connections in a list
def search(searchName):
results = []
if searchName not in connections:
return None
else:
names = connections.get(searchName)
for i in range(len(names)): # for each connection
#if connections.has_key(names[i]): #that name has entered connections => it 100% will, because it will at least have searchName in it
# add all of the values associated with that name key
secondOrdConns = connections.get(names[i])
results = results + secondOrdConns
finalResults = set(results)
finalResults.remove(searchName)
return finalResults
# returns 2nd degree connections as formatted text
def searchbyCL(searchName):
finalResults = search(searchName)
if finalResults == None:
return "You are not in our database. Join by typing add and entering your name!"
if len(finalResults) == 0:
return "You have no 2nd degree connections in our system. Try adding more connections and checking again!"
else:
cleanresults = ', '.join(list(finalResults))
return "Your 2nd degree connections are: " + cleanresults
# private function, only used internally
def addConnection(yourname, connection):
if yourname in connections:
names = connections.get(yourname)
# next check if the connection is already a value
else:
names = []
newConnections = {yourname: names}
connections.update(newConnections)
for i in range(len(names)):
if names[i] == connection:
return connections
# otherwise, if it exits the for loop, the name isnt there so we have to add it
names.append(connection)
return connections
def getConnections(yourname):
return connections.get(yourname, "")
# adds a connection to the connections dict and returns the entire updated dict
def add(yourname, connection):
connections = addConnection(yourname, connection)
connections = addConnection(connection, yourname)
return connections
def addbyCL():
while True:
names = []
yourname = input("Please enter your name: (or done to exit)")
if yourname == 'done':
break
while True:
connection = input("Please enter a contact: (or done to exit)")
if connection == 'done':
break
else: # add the connection to the list
add(yourname, connection)
connections = {}
if __name__ == '__main__':
# run program via command line
while True:
mode = input("Would you like to add connections or search for your 2nd order connections? ")
if mode == "add":
addbyCL()
elif mode == "search":
searchName = input("Please enter your name: ")
print (search(searchName))
elif mode == 'exit':
break
else:
print("Please type 'add', 'search', or 'exit'")