Skip to content

Instantly share code, notes, and snippets.

@TechMaster
Created October 15, 2017 16:56
Show Gist options
  • Select an option

  • Save TechMaster/fa5076be4feb8b02b4ede393e7eddf92 to your computer and use it in GitHub Desktop.

Select an option

Save TechMaster/fa5076be4feb8b02b4ede393e7eddf92 to your computer and use it in GitHub Desktop.
Demo Catch.hpp to test the function move all even numbers to left, all odd numbers to right
#include <iostream>
#include <vector>
using namespace std;
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
vector<int> separateOddEven(vector<int> arr) {
auto left = arr.begin();
auto right = arr.end() - 1;
while (left < right) {
if (*left % 2 == 0) {
left++;
} else if (*right % 2 == 0) {
auto temp = *left;
*left = *right;
*right = temp;
}
if (*right % 2 == 1) {
right--;
}
}
return arr;
}
TEST_CASE("Separate Odd Event", "[separate]") {
REQUIRE(separateOddEven({1, 2}) == vector<int>({2, 1}));
REQUIRE(separateOddEven({1, 3, 2}) == vector<int>({2, 3, 1}));
REQUIRE(separateOddEven({9, 10, 1, 2, 3, 4, 6, 8, 7, 12}) == vector<int>({12, 10, 8, 2, 6, 4, 3, 1, 7, 9}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment