-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacts.cpp
More file actions
98 lines (86 loc) · 3.19 KB
/
facts.cpp
File metadata and controls
98 lines (86 loc) · 3.19 KB
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
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <utility>
#include <string>
using namespace std;
// facts parser
// <12 in, 1 ft>
// <1ft, 2m>
// <1 yd, 3 ft>
// <1 hr, 3600 sec>
// <365 day, 1 yr>
// <60 min, 1 hr>
// 5 in, return ft you should be to do this
// 5 in, yr cannot do this
class FactParser {
public:
FactParser(const std::vector<std::pair<std::string, std::string>>& facts) {
// extract unit1, val1, unit2, val2
for (const auto& p : facts) {
int index = p.first.find(' ');
std::string left = p.first.substr(0, index);
std::string unit1 = p.first.substr(index + 1);
int val1 = stoi(left);
index = p.second.find(' ');
left = p.second.substr(0, index);
std::string unit2 = p.second.substr(index + 1);
int val2 = stoi(left);
double proportion = static_cast<double>(val2) / val1;
proportions[unit1][unit2] = proportion;
double backwardProportion = static_cast<double>(val1) / val2;
proportions[unit2][unit1] = backwardProportion;
}
printMap();
}
void query(const std::string& info1, const std::string& unit2) {
int index = info1.find(' ');
std::string left = info1.substr(0, index);
std::string unit1 =info1.substr(index + 1);
int unit1Amount = stoi(left);
std::unordered_set<std::string> visited;
std::queue<std::pair<std::string, double>> q; // <unit, propFroStart>
double accumulatedProp = 1;
visited.insert(unit1);
q.push({unit1, 1});
while (!q.empty()) {
std::string node = q.front().first;
double currProp = q.front().second;
if (node == unit2) {
double res = unit1Amount * currProp;
std::cout << unit1Amount << " " << unit1 << " is equal to " << res << " " << unit2 << std::endl;
return;
}
q.pop();
for (const auto& kv : proportions[node]) {
std::string nextNode = kv.first;
double prop = kv.second;
if (visited.count(nextNode)) continue;
double nextProp = currProp * prop;
visited.insert(nextNode);
q.push({nextNode, nextProp});
}
}
std::cout << "Not Available Conversion" << std::endl;
return;
}
private:
std::unordered_map<std::string, std::unordered_map<std::string, double>> proportions;
void printMap() {
for (const auto& kv : proportions) {
for (const auto& pair : kv.second) {
std::cout << "u1, u2, proportion: " << kv.first << ", " << pair.first << ", " << pair.second << std::endl;
}
}
}
};
int main() {
std::vector<std::pair<std::string, std::string>> facts {{"12 in", "1 ft"}, {"1 yd", "3 ft"}, {"5280 ft", "1 mi"}, {"1 hr", "3600 sec"}, {"365 day", "1 yr"}, {"60 min", "1 hr"}, {"24 hr", "1 day"}, {"7 day", "1 week"}};
FactParser fp(facts);
fp.query("1 in", "yr");
fp.query("178 week", "yr");
fp.query("9 sec", "week");
}