Last active
September 29, 2021 00:51
-
-
Save iKrishneel/645c8a7ac1754a6d35f1dba433a92ed7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <map> | |
struct Catelog { | |
std::string region_name; | |
int version; | |
int dbytes; | |
Catelog(const std::string r, const int v, const int d) { | |
region_name = r; | |
version = v; | |
dbytes = d; | |
} | |
}; | |
using CatelogMap = std::map<int, Catelog>; | |
std::vector<std::string> break_string(const std::string &str, const std::string &deliminator) { | |
std::vector<std::string> strings; | |
std::string::size_type pos = 0; | |
std::string::size_type prev = 0; | |
while ((pos = str.find(deliminator, prev)) != std::string::npos) { | |
auto x = str.substr(prev, pos - prev); | |
if (!x.empty()) { | |
strings.push_back(x); | |
} | |
prev = pos + 1; | |
} | |
auto x = str.substr(prev); | |
if (!x.empty()) { | |
strings.push_back(x); | |
} | |
return strings; | |
} | |
CatelogMap build_catelogs(const std::string &str) { | |
CatelogMap catelogs; | |
auto strings = break_string(str, "\n"); | |
for (auto c_str: strings) { | |
auto x = break_string(c_str, ","); | |
std::string r = x[0]; | |
auto v = std::atoi(x[1].c_str()); | |
auto d = std::atoi(x[2].c_str()); | |
catelogs.insert({v, Catelog(r, v, d)}); | |
} | |
return catelogs; | |
} | |
int deserialize(const std::string &str, const std::string &t, const std::string &u) { | |
auto installs = break_string(t, ","); | |
int installed_version = std::atoi(installs[1].c_str()); | |
std::string installed_region = installs[0]; | |
auto catelogs = build_catelogs(str); | |
int max_version = -1; | |
for (auto it = catelogs.begin(); it != catelogs.end(); it++) { | |
max_version = it->second.region_name == u ? std::max(it->first, max_version) : max_version; | |
} | |
if (max_version == -1) { | |
return -1; | |
} | |
int total_download = 0; | |
for (int i = installed_version + 1; i < max_version + 1; i++) { | |
auto it = catelogs.find(i); | |
if (it != catelogs.end()) { | |
total_download += it->second.dbytes; | |
} | |
} | |
return total_download; | |
} | |
int main() { | |
std::string s = "Fiji,1,25000\n,Fiji,2,3000\n,Fiji,3,1000"; | |
std::string t = "Fiji,1"; | |
std::string u = "Fiji"; | |
int output = -1; | |
output = deserialize(s, t, u); | |
std::cout << "Output: " << output << "\n"; | |
s = "Fiji,1,25000\n,Fiji,2,3000\n,Fiji,3,1000"; | |
t = "Fiji,1"; | |
u = "India"; | |
output = deserialize(s, t, u); | |
std::cout << "Output: " << output << "\n"; | |
s = "Fiji,1,25000\n,Fiji,2,3000\n,Fiji,3,1000"; | |
t = "Fiji,0"; | |
u = "Fiji"; | |
output = deserialize(s, t, u); | |
std::cout << "Output: " << output << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment