Skip to content

Commit 077d529

Browse files
committedNov 11, 2024
phase 1 done?!
1 parent 5faa370 commit 077d529

File tree

6 files changed

+267
-56
lines changed

6 files changed

+267
-56
lines changed
 

‎.vscode/settings.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,14 @@
9292
"*.tcc": "cpp",
9393
"memory_resource": "cpp",
9494
"stop_token": "cpp",
95-
"cfloat": "cpp"
95+
"cfloat": "cpp",
96+
"__verbose_abort": "cpp",
97+
"execution": "cpp",
98+
"print": "cpp",
99+
"variant": "cpp"
96100
},
97101
"lldb.displayFormat": "auto",
98102
"lldb.dereferencePointers": true,
99103
"lldb.showDisassembly": "auto",
104+
"C_Cpp.errorSquiggles": "disabled",
100105
}

‎MODULE.bazel

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
###############################################################################
2+
# Bazel now uses Bzlmod by default to manage external dependencies.
3+
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
4+
#
5+
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
6+
###############################################################################

‎MODULE.bazel.lock

+110
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ Chevron
241241
Time taken by function: 2 ms
242242
```
243243

244-
- What is the runtime of your algorithm?
245-
- (Optional) Can you do it faster than `O(n)`?
244+
- What is the runtime of your algorithm? 8 ms
245+
- (Optional) Can you do it faster than `O(n)`?
246246

247247
## Item 2-1: Find the place's coordinates in the Map (Phase 1)
248248

‎WORKSPACE

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ git_repository(
3333

3434
new_local_repository(
3535
name = "opencv",
36-
path = "/opt/homebrew/Cellar/opencv/4.9.0_5",
36+
path = "/opt/homebrew/Cellar/opencv/4.10.0_12",
3737
build_file = "opencv.BUILD",
3838
)
3939

4040
new_local_repository(
4141
name = "ncurses",
42-
path = "/opt/homebrew/Cellar/ncurses/6.4",
42+
path = "/opt/homebrew/Cellar/ncurses/6.5",
4343
build_file = "ncurses.BUILD",
4444
)

‎src/lib/trojanmap.cc

+141-51
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* @param {std::string} id : location id
1111
* @return {double} : latitude
1212
*/
13-
double TrojanMap::GetLat(const std::string &id) {
13+
double TrojanMap::GetLat(const std::string &id)
14+
{
1415
return 0;
1516
}
1617

@@ -21,7 +22,8 @@ double TrojanMap::GetLat(const std::string &id) {
2122
* @param {std::string} id : location id
2223
* @return {double} : longitude
2324
*/
24-
double TrojanMap::GetLon(const std::string &id) {
25+
double TrojanMap::GetLon(const std::string &id)
26+
{
2527
return 0;
2628
}
2729

@@ -32,7 +34,8 @@ double TrojanMap::GetLon(const std::string &id) {
3234
* @param {std::string} id : location id
3335
* @return {std::string} : name
3436
*/
35-
std::string TrojanMap::GetName(const std::string &id) {
37+
std::string TrojanMap::GetName(const std::string &id)
38+
{
3639
return "";
3740
}
3841

@@ -43,7 +46,8 @@ std::string TrojanMap::GetName(const std::string &id) {
4346
* @param {std::string} id : location id
4447
* @return {std::vector<std::string>} : neighbor ids
4548
*/
46-
std::vector<std::string> TrojanMap::GetNeighborIDs(const std::string &id) {
49+
std::vector<std::string> TrojanMap::GetNeighborIDs(const std::string &id)
50+
{
4751
return {};
4852
}
4953

@@ -55,7 +59,8 @@ std::vector<std::string> TrojanMap::GetNeighborIDs(const std::string &id) {
5559
* @param {std::string} name : location name
5660
* @return {std::string} : id
5761
*/
58-
std::string TrojanMap::GetID(const std::string &name) {
62+
std::string TrojanMap::GetID(const std::string &name)
63+
{
5964
std::string res = "";
6065
return res;
6166
}
@@ -67,8 +72,21 @@ std::string TrojanMap::GetID(const std::string &name) {
6772
* @param {std::string} name : location name
6873
* @return {std::pair<double,double>} : (lat, lon)
6974
*/
70-
std::pair<double, double> TrojanMap::GetPosition(std::string name) {
75+
std::pair<double, double> TrojanMap::GetPosition(std::string name)
76+
{
7177
std::pair<double, double> results(-1, -1);
78+
79+
for (const auto &entry : data)
80+
{
81+
const Node &node = entry.second; // retrieve current node
82+
83+
// if node matches name
84+
if (node.name == name)
85+
{
86+
results.first = node.lat; // latitude
87+
results.second = node.lon; // longitude
88+
}
89+
}
7290
return results;
7391
}
7492

@@ -78,7 +96,8 @@ std::pair<double, double> TrojanMap::GetPosition(std::string name) {
7896
* @param {std::string} b : second string
7997
* @return {int} : edit distance between two strings
8098
*/
81-
int TrojanMap::CalculateEditDistance(std::string a, std::string b) {
99+
int TrojanMap::CalculateEditDistance(std::string a, std::string b)
100+
{
82101
return 0;
83102
}
84103

@@ -89,7 +108,8 @@ int TrojanMap::CalculateEditDistance(std::string a, std::string b) {
89108
* @param {std::string} name : location name
90109
* @return {std::string} tmp : the closest name
91110
*/
92-
std::string TrojanMap::FindClosestName(std::string name) {
111+
std::string TrojanMap::FindClosestName(std::string name)
112+
{
93113
std::string tmp = ""; // Start with a dummy word
94114
return tmp;
95115
}
@@ -101,8 +121,49 @@ std::string TrojanMap::FindClosestName(std::string name) {
101121
* @param {std::string} name : partial name
102122
* @return {std::vector<std::string>} : a vector of full names
103123
*/
104-
std::vector<std::string> TrojanMap::Autocomplete(std::string name) {
124+
std::vector<std::string> TrojanMap::Autocomplete(std::string name)
125+
{
105126
std::vector<std::string> results;
127+
128+
// test case: input empty --> return empty vector
129+
if (name.empty())
130+
{
131+
return results;
132+
}
133+
134+
// convert input string to lowercase
135+
for (size_t i = 0; i < name.length(); i++)
136+
{
137+
name[i] = tolower(name[i]); // found tolower() funciton on geeksforgeeks.org
138+
}
139+
140+
// iterate through all nodes in data map
141+
for (const auto &entry : data)
142+
{
143+
const Node &node = entry.second; // retrieve current node
144+
std::string node_name = node.name; // retreieve name of current node
145+
146+
// convert node name --> lowercase
147+
std::transform(node_name.begin(), node_name.end(), node_name.begin(), ::tolower);
148+
149+
// check if node name starts with string
150+
bool match = true; // assume initially matches
151+
for (size_t i = 0; i < name.length(); i++)
152+
{
153+
if (node_name[i] != name[i]) // if does not match, switch to false
154+
{
155+
match = false;
156+
break; // exit loop since mismatch found
157+
}
158+
}
159+
160+
// if match found, add to vector
161+
if (match)
162+
{
163+
results.push_back(node.name);
164+
}
165+
}
166+
// return vector list
106167
return results;
107168
}
108169

@@ -112,7 +173,8 @@ std::vector<std::string> TrojanMap::Autocomplete(std::string name) {
112173
*
113174
* @return {std::vector<std::string>} : all unique location categories
114175
*/
115-
std::vector<std::string> TrojanMap::GetAllCategories() {
176+
std::vector<std::string> TrojanMap::GetAllCategories()
177+
{
116178
return {};
117179
}
118180

@@ -125,7 +187,8 @@ std::vector<std::string> TrojanMap::GetAllCategories() {
125187
* @return {std::vector<std::string>} : ids
126188
*/
127189
std::vector<std::string> TrojanMap::GetAllLocationsFromCategory(
128-
std::string category) {
190+
std::string category)
191+
{
129192
std::vector<std::string> res;
130193
return res;
131194
}
@@ -139,7 +202,8 @@ std::vector<std::string> TrojanMap::GetAllLocationsFromCategory(
139202
* names
140203
* @return {std::vector<std::string>} : ids
141204
*/
142-
std::vector<std::string> TrojanMap::GetLocationRegex(std::regex location) {
205+
std::vector<std::string> TrojanMap::GetLocationRegex(std::regex location)
206+
{
143207
return {};
144208
}
145209

@@ -150,13 +214,14 @@ std::vector<std::string> TrojanMap::GetLocationRegex(std::regex location) {
150214
* The distance is in mile.
151215
* The distance is calculated using the Haversine formula.
152216
* https://en.wikipedia.org/wiki/Haversine_formula
153-
*
217+
*
154218
* @param {std::string} a : a_id
155219
* @param {std::string} b : b_id
156220
* @return {double} : distance in mile
157221
*/
158222
double TrojanMap::CalculateDistance(const std::string &a_id,
159-
const std::string &b_id) {
223+
const std::string &b_id)
224+
{
160225
// Do not change this function
161226
Node a = data[a_id];
162227
Node b = data[b_id];
@@ -173,14 +238,16 @@ double TrojanMap::CalculateDistance(const std::string &a_id,
173238
* CalculatePathLength: Calculates the total path length for the locations
174239
* inside the vector.
175240
* We have provided the code for you. Please do not need to change this function.
176-
*
241+
*
177242
* @param {std::vector<std::string>} path : path
178243
* @return {double} : path length
179244
*/
180-
double TrojanMap::CalculatePathLength(const std::vector<std::string> &path) {
245+
double TrojanMap::CalculatePathLength(const std::vector<std::string> &path)
246+
{
181247
// Do not change this function
182248
double sum = 0;
183-
for (int i = 0; i < int(path.size()) - 1; i++) {
249+
for (int i = 0; i < int(path.size()) - 1; i++)
250+
{
184251
sum += CalculateDistance(path[i], path[i + 1]);
185252
}
186253
return sum;
@@ -195,7 +262,8 @@ double TrojanMap::CalculatePathLength(const std::vector<std::string> &path) {
195262
* @return {std::vector<std::string>} : path
196263
*/
197264
std::vector<std::string> TrojanMap::CalculateShortestPath_Dijkstra(
198-
std::string location1_name, std::string location2_name) {
265+
std::string location1_name, std::string location2_name)
266+
{
199267
std::vector<std::string> path;
200268
return path;
201269
}
@@ -210,7 +278,8 @@ std::vector<std::string> TrojanMap::CalculateShortestPath_Dijkstra(
210278
* @return {std::vector<std::string>} : path
211279
*/
212280
std::vector<std::string> TrojanMap::CalculateShortestPath_Bellman_Ford(
213-
std::string location1_name, std::string location2_name) {
281+
std::string location1_name, std::string location2_name)
282+
{
214283
std::vector<std::string> path;
215284
return path;
216285
}
@@ -220,37 +289,41 @@ std::vector<std::string> TrojanMap::CalculateShortestPath_Bellman_Ford(
220289
* path which visit all the places and back to the start point.
221290
*
222291
* @param {std::vector<std::string>} input : a list of locations needs to visit
223-
* @return {std::pair<double, std::vector<std::vector<std::string>>} : a pair of total distance and the all the progress to get final path,
292+
* @return {std::pair<double, std::vector<std::vector<std::string>>} : a pair of total distance and the all the progress to get final path,
224293
* for example: {10.3, {{0, 1, 2, 3, 4, 0}, {0, 1, 2, 3, 4, 0}, {0, 4, 3, 2, 1, 0}}},
225-
* where 10.3 is the total distance,
294+
* where 10.3 is the total distance,
226295
* and the first vector is the path from 0 and travse all the nodes and back to 0,
227296
* and the second vector is the path shorter than the first one,
228297
* and the last vector is the shortest path.
229298
*/
230299
// Please use brute force to implement this function, ie. find all the permutations.
231300
std::pair<double, std::vector<std::vector<std::string>>> TrojanMap::TravelingTrojan_Brute_force(
232-
std::vector<std::string> location_ids) {
301+
std::vector<std::string> location_ids)
302+
{
233303
std::pair<double, std::vector<std::vector<std::string>>> records;
234304
return records;
235305
}
236306

237307
// Please use backtracking to implement this function
238308
std::pair<double, std::vector<std::vector<std::string>>> TrojanMap::TravelingTrojan_Backtracking(
239-
std::vector<std::string> location_ids) {
309+
std::vector<std::string> location_ids)
310+
{
240311
std::pair<double, std::vector<std::vector<std::string>>> records;
241312
return records;
242313
}
243314

244315
// Hint: https://en.wikipedia.org/wiki/2-opt
245316
std::pair<double, std::vector<std::vector<std::string>>> TrojanMap::TravelingTrojan_2opt(
246-
std::vector<std::string> location_ids){
317+
std::vector<std::string> location_ids)
318+
{
247319
std::pair<double, std::vector<std::vector<std::string>>> records;
248320
return records;
249321
}
250322

251323
// This is optional
252324
std::pair<double, std::vector<std::vector<std::string>>> TrojanMap::TravelingTrojan_3opt(
253-
std::vector<std::string> location_ids){
325+
std::vector<std::string> location_ids)
326+
{
254327
std::pair<double, std::vector<std::vector<std::string>>> records;
255328
return records;
256329
}
@@ -259,7 +332,7 @@ std::pair<double, std::vector<std::vector<std::string>>> TrojanMap::TravelingTro
259332
* Given CSV filename, it read and parse locations data from CSV file,
260333
* and return locations vector for topological sort problem.
261334
* We have provided the code for you. Please do not need to change this function.
262-
* Example:
335+
* Example:
263336
* Input: "topologicalsort_locations.csv"
264337
* File content:
265338
* Name
@@ -271,13 +344,15 @@ std::pair<double, std::vector<std::vector<std::string>>> TrojanMap::TravelingTro
271344
* @return {std::vector<std::string>} : locations
272345
*/
273346
std::vector<std::string> TrojanMap::ReadLocationsFromCSVFile(
274-
std::string locations_filename) {
347+
std::string locations_filename)
348+
{
275349
std::vector<std::string> location_names_from_csv;
276350
std::fstream fin;
277351
fin.open(locations_filename, std::ios::in);
278352
std::string line, word;
279353
getline(fin, line);
280-
while (getline(fin, word)) {
354+
while (getline(fin, word))
355+
{
281356
location_names_from_csv.push_back(word);
282357
}
283358
fin.close();
@@ -288,7 +363,7 @@ std::vector<std::string> TrojanMap::ReadLocationsFromCSVFile(
288363
* Given CSV filenames, it read and parse dependencise data from CSV file,
289364
* and return dependencies vector for topological sort problem.
290365
* We have provided the code for you. Please do not need to change this function.
291-
* Example:
366+
* Example:
292367
* Input: "topologicalsort_dependencies.csv"
293368
* File content:
294369
* Source,Destination
@@ -300,16 +375,19 @@ std::vector<std::string> TrojanMap::ReadLocationsFromCSVFile(
300375
* @return {std::vector<std::vector<std::string>>} : dependencies
301376
*/
302377
std::vector<std::vector<std::string>> TrojanMap::ReadDependenciesFromCSVFile(
303-
std::string dependencies_filename) {
378+
std::string dependencies_filename)
379+
{
304380
std::vector<std::vector<std::string>> dependencies_from_csv;
305381
std::fstream fin;
306382
fin.open(dependencies_filename, std::ios::in);
307383
std::string line, word;
308384
getline(fin, line);
309-
while (getline(fin, line)) {
385+
while (getline(fin, line))
386+
{
310387
std::stringstream s(line);
311388
std::vector<std::string> dependency;
312-
while (getline(s, word, ',')) {
389+
while (getline(s, word, ','))
390+
{
313391
dependency.push_back(word);
314392
}
315393
dependencies_from_csv.push_back(dependency);
@@ -329,9 +407,10 @@ std::vector<std::vector<std::string>> TrojanMap::ReadDependenciesFromCSVFile(
329407
*/
330408
std::vector<std::string> TrojanMap::DeliveringTrojan(
331409
std::vector<std::string> &locations,
332-
std::vector<std::vector<std::string>> &dependencies) {
410+
std::vector<std::vector<std::string>> &dependencies)
411+
{
333412
std::vector<std::string> result;
334-
return result;
413+
return result;
335414
}
336415

337416
/**
@@ -341,11 +420,11 @@ std::vector<std::string> TrojanMap::DeliveringTrojan(
341420
* @param {std::vector<double>} square: four vertexes of the square area
342421
* @return {bool} : in square or not
343422
*/
344-
bool TrojanMap::inSquare(std::string id, std::vector<double> &square) {
423+
bool TrojanMap::inSquare(std::string id, std::vector<double> &square)
424+
{
345425
return true;
346426
}
347427

348-
349428
/**
350429
* GetSubgraph: Give four vertexes of the square area, return a list of location
351430
* ids in the squares
@@ -355,7 +434,8 @@ bool TrojanMap::inSquare(std::string id, std::vector<double> &square) {
355434
* @return {std::vector<std::string>} subgraph : list of location ids in the
356435
* square
357436
*/
358-
std::vector<std::string> TrojanMap::GetSubgraph(std::vector<double> &square) {
437+
std::vector<std::string> TrojanMap::GetSubgraph(std::vector<double> &square)
438+
{
359439
// include all the nodes in subgraph
360440
std::vector<std::string> subgraph;
361441
return subgraph;
@@ -370,7 +450,8 @@ std::vector<std::string> TrojanMap::GetSubgraph(std::vector<double> &square) {
370450
* @param {std::vector<double>} square: four vertexes of the square area
371451
* @return {bool}: whether there is a cycle or not
372452
*/
373-
bool TrojanMap::CycleDetection(std::vector<std::string> &subgraph, std::vector<double> &square) {
453+
bool TrojanMap::CycleDetection(std::vector<std::string> &subgraph, std::vector<double> &square)
454+
{
374455
return false;
375456
}
376457

@@ -385,7 +466,8 @@ bool TrojanMap::CycleDetection(std::vector<std::string> &subgraph, std::vector<d
385466
* @param {int} k: search numbers
386467
* @return {std::vector<std::string>}: location name that meets the requirements
387468
*/
388-
std::vector<std::string> TrojanMap::FindNearby(std::string attributesName, std::string name, double r, int k) {
469+
std::vector<std::string> TrojanMap::FindNearby(std::string attributesName, std::string name, double r, int k)
470+
{
389471
std::vector<std::string> res;
390472
return res;
391473
}
@@ -398,9 +480,10 @@ std::vector<std::string> TrojanMap::FindNearby(std::string attributesName, std::
398480
* @return {std::vector<std::string> } : the shortest path
399481
*/
400482
std::vector<std::string> TrojanMap::TrojanPath(
401-
std::vector<std::string> &location_names) {
402-
std::vector<std::string> res;
403-
return res;
483+
std::vector<std::string> &location_names)
484+
{
485+
std::vector<std::string> res;
486+
return res;
404487
}
405488

406489
/**
@@ -409,28 +492,32 @@ std::vector<std::string> TrojanMap::TrojanPath(
409492
* @param {std::vector<std::pair<double, std::vector<std::string>>>} Q : a list of queries
410493
* @return {std::vector<bool> } : existence of the path
411494
*/
412-
std::vector<bool> TrojanMap::Queries(const std::vector<std::pair<double, std::vector<std::string>>>& q) {
413-
std::vector<bool> ans(q.size());
414-
return ans;
495+
std::vector<bool> TrojanMap::Queries(const std::vector<std::pair<double, std::vector<std::string>>> &q)
496+
{
497+
std::vector<bool> ans(q.size());
498+
return ans;
415499
}
416500

417501
/**
418502
* CreateGraphFromCSVFile: Read the map data from the csv file
419503
* We have provided the code for you. Please do not need to change this function.
420504
*/
421-
void TrojanMap::CreateGraphFromCSVFile() {
505+
void TrojanMap::CreateGraphFromCSVFile()
506+
{
422507
// Do not change this function
423508
std::fstream fin;
424509
fin.open("src/lib/data.csv", std::ios::in);
425510
std::string line, word;
426511

427512
getline(fin, line);
428-
while (getline(fin, line)) {
513+
while (getline(fin, line))
514+
{
429515
std::stringstream s(line);
430516

431517
Node n;
432518
int count = 0;
433-
while (getline(s, word, ',')) {
519+
while (getline(s, word, ','))
520+
{
434521
word.erase(std::remove(word.begin(), word.end(), '\''), word.end());
435522
word.erase(std::remove(word.begin(), word.end(), '"'), word.end());
436523
word.erase(std::remove(word.begin(), word.end(), '{'), word.end());
@@ -443,10 +530,13 @@ void TrojanMap::CreateGraphFromCSVFile() {
443530
n.lon = stod(word);
444531
else if (count == 3)
445532
n.name = word;
446-
else {
533+
else
534+
{
447535
word.erase(std::remove(word.begin(), word.end(), ' '), word.end());
448-
if (isalpha(word[0])) n.attributes.insert(word);
449-
if (isdigit(word[0])) n.neighbors.push_back(word);
536+
if (isalpha(word[0]))
537+
n.attributes.insert(word);
538+
if (isdigit(word[0]))
539+
n.neighbors.push_back(word);
450540
}
451541
count++;
452542
}

0 commit comments

Comments
 (0)
Please sign in to comment.