Start on the Route Finding assignment part 0. have the parsing almost complete?

This commit is contained in:
Charles Descamps 2025-02-14 23:51:48 -07:00
parent f23ca49f82
commit 39253dfe5e
2 changed files with 91 additions and 0 deletions

View file

@ -1,4 +1,41 @@
# Define Searcher, hSLD, SearchNode. # Define Searcher, hSLD, SearchNode.
class SearchNode:
def __init__(self, label, value):
self.label = label
self.value = value
class Searcher:
def __init__(self, mapfile):
self.mapfile = mapfile
# load the mapfile's structure
with open(self.mapfile) as mapper:
mapContents = mapper.read()
mapContents = mapContents.split("\n")
for mapIter in range(len(mapContents)):
mapContents[mapIter] = mapContents[mapIter].split(", ")
for firstDimMapIter in range(len(mapContents) - 1):
for secondDimMapIter in range(len(mapContents[firstDimMapIter])):
mapContents[firstDimMapIter][secondDimMapIter] = mapContents[firstDimMapIter][secondDimMapIter].strip("')(][")
mapContents.pop(len(mapContents) - 1)
for tupleIter in range(len(mapContents)):
mapContents[tupleIter] = tuple(mapContents[tupleIter])
# print(mapContents) # list of tuples
# print(mapContents[0]) # tuple of strings (data)
# [first node, second node, path cost, first node x, first node y, second node x, second node y]
# 0 1 2 3 4 5 6
# print(mapContents[0][0]) # data, string format
# make two dictionaries of nodes, iterating through list of tuples to make note of every node's paths and locations
nodeLoc = dict()
nodePaths = dict()
for nodeIter in range(len(mapContents)):
# these node locations are fine, because they should technically be in only one place.
nodeLoc[mapContents[nodeIter][0]] = tuple((mapContents[nodeIter][3], mapContents[nodeIter][4]))
nodeLoc[mapContents[nodeIter][1]] = tuple((mapContents[nodeIter][5], mapContents[nodeIter][6]))
# however, this is only taking the first node path it gets to...
# I probably need a list instead, that gets updated if the node is not in the node's child list.
nodePaths[mapContents[nodeIter][0]] = tuple((mapContents[nodeIter][1], mapContents[nodeIter][2]))
print(nodeLoc)
print(nodePaths)
# (b) Show your program loading in the 30-node sample file. # (b) Show your program loading in the 30-node sample file.
s=Searcher("30node.txt") s=Searcher("30node.txt")

54
RouteFinding/30node.txt Normal file
View file

@ -0,0 +1,54 @@
('N', 'AB', 299, [589, 414], [776, 1])
('U', 'H', 72, [227, 758], [294, 878])
('V', 'U', 185, [1, 712], [227, 758])
('S', 'N', 69, [502, 382], [589, 414])
('N', 'AA', 100, [589, 414], [481, 450])
('AA', 'S', 52, [481, 450], [502, 382])
('AA', 'B', 91, [481, 450], [394, 422])
('K', 'M', 138, [343, 838], [448, 925])
('H', 'K', 41, [294, 878], [343, 838])
('K', 'U', 78, [343, 838], [227, 758])
('U', 'C', 64, [227, 758], [316, 737])
('W', 'A', 84, [240, 428], [287, 473])
('V', 'W', 399, [1, 712], [240, 428])
('A', 'U', 337, [287, 473], [227, 758])
('Y', 'N', 166, [518, 543], [589, 414])
('I', 'Y', 272, [750, 737], [518, 543])
('I', 'F', 413, [750, 737], [470, 644])
('Y', 'AA', 62, [518, 543], [481, 450])
('P', 'F', 201, [520, 810], [470, 644])
('R', 'P', 176, [487, 971], [520, 810])
('M', 'P', 85, [448, 925], [520, 810])
('Q', 'E', 449, [569, 230], [328, 114])
('AB', 'Q', 477, [776, 1], [569, 230])
('Q', 'D', 63, [569, 230], [489, 259])
('D', 'E', 231, [489, 259], [328, 114])
('N', 'Q', 192, [589, 414], [569, 230])
('Q', 'S', 126, [569, 230], [502, 382])
('S', 'D', 102, [502, 382], [489, 259])
('AD', 'V', 599, [134, 238], [1, 712])
('AC', 'E', 227, [351, 298], [328, 114])
('D', 'AC', 134, [489, 259], [351, 298])
('S', 'AC', 131, [502, 382], [351, 298])
('B', 'AC', 119, [394, 422], [351, 298])
('B', 'AE', 48, [394, 422], [347, 470])
('AE', 'W', 84, [347, 470], [240, 428])
('K', 'X', 77, [343, 838], [418, 798])
('C', 'X', 119, [316, 737], [418, 798])
('X', 'F', 196, [418, 798], [470, 644])
('AC', 'O', 108, [351, 298], [225, 251])
('O', 'E', 219, [225, 251], [328, 114])
('C', 'J', 54, [316, 737], [359, 667])
('J', 'F', 64, [359, 667], [470, 644])
('AE', 'J', 266, [347, 470], [359, 667])
('U', 'J', 178, [227, 758], [359, 667])
('T', 'Y', 65, [455, 523], [518, 543])
('F', 'T', 99, [470, 644], [455, 523])
('J', 'T', 203, [359, 667], [455, 523])
('AE', 'T', 87, [347, 470], [455, 523])
('T', 'AA', 93, [455, 523], [481, 450])
('L', 'AC', 99, [179, 306], [351, 298])
('W', 'L', 171, [240, 428], [179, 306])
('L', 'O', 67, [179, 306], [225, 251])
('L', 'AD', 88, [179, 306], [134, 238])
('E', 'G', 100, [328, 114], [250, 75])