Created
March 20, 2015 21:44
-
-
Save sguzman/23b80630ebabc8e3a02a to your computer and use it in GitHub Desktop.
Failed attempt to get a quasi-conversion working for a leetcode problem
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> | |
| #include <stdlib.h> | |
| 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); | |
| } | |
| }; | |
| class Solution1 { | |
| public: | |
| string convertToTitle(int n) { | |
| string str; | |
| Solution1::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.rem == 0 && divRem.quot != 0) { | |
| convertToTitle(n - 26, str); | |
| } else 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 testInputConv(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; | |
| }*/ | |
| } | |
| } | |
| static void testInputConv1(arry<int, 6> input, arry<string, 6> expected) { | |
| Solution1 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"}; | |
| const array<int, 6> nums1{1, 2, 3, 26, 27, 28}; | |
| const array<string, 6> expected1{"A", "B", "C", "Z", "AA", "AB"}; | |
| testInputConv(nums, expected); | |
| cout << endl << endl; | |
| testInputConv1(nums1, expected1); | |
| return 0; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment