Skip to content

Commit

Permalink
Sort Array By Parity
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Sep 28, 2023
1 parent ff3fe0c commit 6bcc6d8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions solutions/sort-array-by-parity/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_sort_array_by_parity test.cpp)
12 changes: 12 additions & 0 deletions solutions/sort-array-by-parity/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <algorithm>
#include <vector>

class Solution {
public:
static std::vector<int> sortArrayByParity(std::vector<int> nums) {
std::partition(nums.begin(), nums.end(), [](int a) { return a % 2 == 0; });
return nums;
}
};
15 changes: 15 additions & 0 deletions solutions/sort-array-by-parity/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
std::vector<int> nums{0};
REQUIRE(nums == Solution::sortArrayByParity(nums));
}

TEST_CASE("Is Partitioned") {
std::vector<int> 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; }));
}

0 comments on commit 6bcc6d8

Please sign in to comment.