add module that provides answers to the questions
This commit is contained in:
parent
2098a38486
commit
c488cfa1a9
26
exam/ex01/connectivity.py
Normal file
26
exam/ex01/connectivity.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
def can_reach(title, connection):
|
||||||
|
cursor = connection.cursor()
|
||||||
|
cursor.execute("SELECT COUNT(destination) FROM links WHERE destination=?", (title, ))
|
||||||
|
count = cursor.fetchone()[0]
|
||||||
|
return count > 0
|
||||||
|
|
||||||
|
def shortest_path(center, title, connection):
|
||||||
|
if(not can_reach(title, connection)):
|
||||||
|
return []
|
||||||
|
|
||||||
|
cursor = connection.cursor()
|
||||||
|
current_title = title
|
||||||
|
path = deque()
|
||||||
|
while(current_title != center):
|
||||||
|
path.append(current_title)
|
||||||
|
cursor.execute('''SELECT links.source
|
||||||
|
FROM links
|
||||||
|
LEFT JOIN dijkstra_helper ON links.destination=dijkstra_helper.name
|
||||||
|
WHERE links.destination=:title
|
||||||
|
ORDER BY dijkstra_helper.value ASC
|
||||||
|
LIMIT 1''', {"title": current_title})
|
||||||
|
current_title = cursor.fetchone()[0]
|
||||||
|
return list(reversed(path))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user