diff --git a/CMakeLists.txt b/CMakeLists.txt index b0ff63bd..333fb1be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -524,6 +524,7 @@ add_task(snakes-and-ladders) add_task(snapshot-array) add_task(solving-questions-with-brainpower) add_task(sort-an-array) +add_task(sort-array-by-parity) add_task(sort-characters-by-frequency) add_task(sort-colors) add_task(sort-integers-by-the-number-of-1-bits) diff --git a/solutions/sort-array-by-parity/CMakeLists.txt b/solutions/sort-array-by-parity/CMakeLists.txt new file mode 100644 index 00000000..943fe947 --- /dev/null +++ b/solutions/sort-array-by-parity/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_sort_array_by_parity test.cpp) diff --git a/solutions/sort-array-by-parity/solution.hpp b/solutions/sort-array-by-parity/solution.hpp new file mode 100644 index 00000000..f43db7b3 --- /dev/null +++ b/solutions/sort-array-by-parity/solution.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +class Solution { +public: + static std::vector sortArrayByParity(std::vector nums) { + std::partition(nums.begin(), nums.end(), [](int a) { return a % 2 == 0; }); + return nums; + } +}; diff --git a/solutions/sort-array-by-parity/test.cpp b/solutions/sort-array-by-parity/test.cpp new file mode 100644 index 00000000..5faf77ad --- /dev/null +++ b/solutions/sort-array-by-parity/test.cpp @@ -0,0 +1,15 @@ +#include + +#include + +TEST_CASE("Simple") { + std::vector nums{0}; + REQUIRE(nums == Solution::sortArrayByParity(nums)); +} + +TEST_CASE("Is Partitioned") { + std::vector nums{3, 1, 2, 4}; + const auto actual = Solution::sortArrayByParity(nums); + REQUIRE(std::is_partitioned(actual.begin(), actual.end(), + [](int a) { return a % 2 == 0; })); +}