Skip to content

Instantly share code, notes, and snippets.

@ilpropheta
Created November 10, 2015 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilpropheta/ff2a0be9c909019aa0f2 to your computer and use it in GitHub Desktop.
Save ilpropheta/ff2a0be9c909019aa0f2 to your computer and use it in GitHub Desktop.
// my solution for: http://www.geeksforgeeks.org/check-if-a-given-number-is-fancy/
// Precondition: i only contains digit chars
bool is_fancy(const string& i)
{
static const map<char, char> toFancy{
{'0', '0'}, {'1', '1'},
{'6', '9'}, {'8', '8'}, {'9', '6'},
{'2', 0}, {'3', 0}, {'4', 0}, {'5', 0}, {'7', 0},
};
auto fw = i.begin(); // forward
auto bw = i.rbegin(); // backward
while ((fw != end(i)) && (toFancy.at(*bw++) == *fw++));
return fw == end(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment