Created
August 5, 2019 21:54
-
-
Save keddad/db3118203937bfa92423abf2830b3d47 to your computer and use it in GitHub Desktop.
This file contains 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<map> | |
#include <string> | |
class Person { | |
public: | |
void ChangeFirstName(int year, const std::string &first_name) { | |
firstNameCache[year] = first_name; | |
firstChanged = true; | |
} | |
void ChangeLastName(int year, const std::string &last_name) { | |
lastNameCache[year] = last_name; | |
secondChanged = true; | |
} | |
std::string GetFullName(int year) { | |
std::string out; | |
if (!firstChanged and !secondChanged) { | |
out = "Incognito"; | |
} else if (!firstChanged) { | |
out = getEarliest(lastNameCache, year) + " with unknown first name"; | |
} else if (!secondChanged) { | |
out = getEarliest(firstNameCache, year) + " with unknown last name"; | |
} else { | |
out = getEarliest(firstNameCache, year) + " " + getEarliest(lastNameCache, year); | |
} | |
return out; | |
} | |
private: | |
std::string getEarliest(std::map<int, std::string> &m, int year) { | |
int currentInt = -1; | |
std::string out; | |
for (const auto &elem : m) { | |
if (elem.first <= year && elem.first > currentInt) { | |
currentInt = elem.first; | |
out = elem.second; | |
} | |
} | |
return out; | |
} | |
bool firstChanged = false; | |
bool secondChanged = false; | |
std::map<int, std::string> firstNameCache; | |
std::map<int, std::string> lastNameCache; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment