From babd7cec71378a49cd646ac5b2ee589f22f4aad1 Mon Sep 17 00:00:00 2001 From: Philippe Rzetelski Date: Thu, 11 Nov 2021 18:29:22 +1030 Subject: [PATCH 1/2] Passed! --- CodeWars/src/Snail/VS_Proj/Source.cpp | 90 +++++++++++ CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj | 147 ++++++++++++++++++ .../src/Snail/VS_Proj/VS_Proj.vcxproj.filters | 22 +++ .../src/Snail/VS_Proj/VS_Proj.vcxproj.user | 4 + 4 files changed, 263 insertions(+) create mode 100644 CodeWars/src/Snail/VS_Proj/Source.cpp create mode 100644 CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj create mode 100644 CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj.filters create mode 100644 CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj.user diff --git a/CodeWars/src/Snail/VS_Proj/Source.cpp b/CodeWars/src/Snail/VS_Proj/Source.cpp new file mode 100644 index 0000000..fd980f8 --- /dev/null +++ b/CodeWars/src/Snail/VS_Proj/Source.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +void snail( + const std::vector>& snail_map, + std::vector& result, + int topRow, + int rightColumn, + int bottomRow, + int leftColumn, + int n) +{ + + if (topRow > bottomRow) + return; + + for (int i = leftColumn; i <= rightColumn; ++i) + result.push_back(snail_map[topRow][i]); + + for (int i = topRow + 1; i <= bottomRow; ++i) + result.push_back(snail_map[i][rightColumn]); + + for (int i = rightColumn - 1; i >= leftColumn; --i) + result.push_back(snail_map[bottomRow][i]); + + for (int i = bottomRow - 1; i >= leftColumn + 1 ; --i) + result.push_back(snail_map[i][leftColumn]); + + + snail( + snail_map, + result, + topRow + 1, + rightColumn - 1, + bottomRow - 1, + leftColumn + 1, + n-1); + +} +std::vector snail(const std::vector> &snail_map) +{ + if (snail_map.size()==1 && !snail_map[0].size()) + return std::vector{}; + if (snail_map.size()==1) + return std::vector{snail_map[0][0]}; + std::vector result; + snail( + snail_map, + result, + 0, + snail_map.size() - 1, + snail_map.size() - 1, + 0, + snail_map.size()); + + return result; +} +int main() +{ + + std::vector> v = {{1,2,3}, {8,9,4}, {7,6,5}}; + std::vector expected = {1,2,3,4,5,6,7,8,9}; + assert(snail(v) == expected); + + auto res = snail(v); + for (auto elem : res) + std::cout << elem << ", "; + + v = {{1,2,3,4}, {12,13,14,5}, {11,16,15,6}, {10,9,8,7}}; + expected = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; + assert(snail(v) == expected); + + v = {{1,2}, {4,3}}; + expected = {1,2,3,4}; + assert(snail(v) == expected); + + + v = {{}}; + expected = {}; + assert(snail(v) == expected); + + v = {{1}}; + expected = {1}; + assert(snail(v) == expected); + + + + return 0; +} \ No newline at end of file diff --git a/CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj b/CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj new file mode 100644 index 0000000..0a77b88 --- /dev/null +++ b/CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {1e9bef5e-1a63-4087-aa16-faf337b87595} + VSProj + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj.filters b/CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj.filters new file mode 100644 index 0000000..3e7e62e --- /dev/null +++ b/CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj.user b/CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/CodeWars/src/Snail/VS_Proj/VS_Proj.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file From 073bd67f2e50a8825912d9e5f66d7d47d622dcec Mon Sep 17 00:00:00 2001 From: Philippe Rzetelski Date: Thu, 11 Nov 2021 18:50:59 +1030 Subject: [PATCH 2/2] Parent function refactor --- CodeWars/src/Snail/VS_Proj/Source.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/CodeWars/src/Snail/VS_Proj/Source.cpp b/CodeWars/src/Snail/VS_Proj/Source.cpp index fd980f8..ef48781 100644 --- a/CodeWars/src/Snail/VS_Proj/Source.cpp +++ b/CodeWars/src/Snail/VS_Proj/Source.cpp @@ -40,19 +40,21 @@ void snail( } std::vector snail(const std::vector> &snail_map) { - if (snail_map.size()==1 && !snail_map[0].size()) - return std::vector{}; - if (snail_map.size()==1) - return std::vector{snail_map[0][0]}; + const size_t mapSize = snail_map.size(); std::vector result; + + if (mapSize == 1) + return !snail_map[0].size() ? + result : std::vector{ snail_map[0][0] }; + snail( snail_map, result, 0, - snail_map.size() - 1, - snail_map.size() - 1, + mapSize - 1, + mapSize - 1, 0, - snail_map.size()); + mapSize); return result; }