Created
March 20, 2015 18:45
-
-
Save sguzman/19ae35401962fb2e76d9 to your computer and use it in GitHub Desktop.
Testing my conversion 10->26. Will work on a more general framework for dec -> any-ary
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 <cstdlib> | |
| #include <array> | |
| using namespace std; | |
| template <typename A> | |
| using conref = const A&; | |
| template <typename A, size_t sz> | |
| using arry = conref<array<A,sz>> const; | |
| class Solution { | |
| public: | |
| string convertToTitle(int n) { | |
| string str; | |
| Solution::convertToTitle(n, str); | |
| return str; | |
| } | |
| private: | |
| constexpr static const int base{26}; | |
| static void convertToTitle(int n, string& str) { | |
| auto divRem = div(n, base); | |
| if (divRem.quot != 0) { | |
| convertToTitle(divRem.quot, str); | |
| } | |
| str += decto26(divRem.rem); | |
| } | |
| static inline char decto26(int n) { | |
| constexpr static const array<char,base> chartConverion {'A', 'B', 'C', 'D', | |
| 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', | |
| 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; | |
| return chartConverion.at(n); | |
| } | |
| }; | |
| static void testInput(arry<int, 8> input, arry<string, 8> expected) { | |
| Solution sol; | |
| for (int i = 0; i < input.size(); ++i) { | |
| auto& in = input.at(i); | |
| auto result = sol.convertToTitle(in); | |
| auto& shouldBe = expected.at(i); | |
| auto isWork = result == shouldBe; | |
| if (isWork) { | |
| cout << "CORRECT: "; | |
| } else { | |
| cout << "FAILURE: "; | |
| } | |
| cout << "Input=" << in << " Result=" << result | |
| << " Expected=" << shouldBe << endl; | |
| /* if (!isWork) { | |
| return; | |
| }*/ | |
| } | |
| } | |
| int main() { | |
| constexpr const size_t sz = 8; | |
| const array<int, sz> nums{0, 1, 2, 3, 25, 26, 27, 28}; | |
| const array<string, sz> expected{"A", "B", "C", "D", "Z", "BA", "BB", "BC"}; | |
| testInput(nums, expected); | |
| return 0; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment