From 747622f724f64062d18b7c1e61b4d0a1e78637a3 Mon Sep 17 00:00:00 2001 From: Ace-Krypton Date: Mon, 2 Oct 2023 17:16:47 +0200 Subject: [PATCH] Added Remove Colored Pieces if Both Neighbors are the Same Color --- CMakeLists.txt | 11 ++++++ .../include/solution.hpp | 2 +- .../include/solution.hpp | 24 ++++++++++++ .../tests/test.cpp | 38 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp create mode 100644 Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/tests/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6608520..20f33f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1239,4 +1239,15 @@ add_executable(Dota2Senate target_link_libraries( Dota2Senate GTest::gtest_main +) + +# Remove Colored Pieces if Both Neighbors are the Same Color +add_executable(RemoveColoredPiecesIfBothNeighborsAreTheSameColor + Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp + Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/tests/test.cpp +) + +target_link_libraries( + RemoveColoredPiecesIfBothNeighborsAreTheSameColor + GTest::gtest_main ) \ No newline at end of file diff --git a/Medium/ProductOfArrayExceptSelf/include/solution.hpp b/Medium/ProductOfArrayExceptSelf/include/solution.hpp index 66fa2f9..6d5ae03 100644 --- a/Medium/ProductOfArrayExceptSelf/include/solution.hpp +++ b/Medium/ProductOfArrayExceptSelf/include/solution.hpp @@ -1,9 +1,9 @@ #pragma once #include -#include #include #include +#include #include #include diff --git a/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp b/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp new file mode 100644 index 0000000..ece7f6f --- /dev/null +++ b/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include +#include + +class Solution { +public: + static auto winner_of_game(const std::string &colors) -> bool { + int32_t bob = 0; + int32_t alice = 0; + + for (size_t i = 1; i < colors.size() - 1; ++i) { + if (colors[i - 1] == colors[i] + && colors[i] == colors[i + 1]) { + if(colors[i] == 'A') ++alice; + if(colors[i] == 'B') ++bob; + } + } + + return (alice >= (bob + 1)); + } +}; diff --git a/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/tests/test.cpp b/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/tests/test.cpp new file mode 100644 index 0000000..b55dd0d --- /dev/null +++ b/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/tests/test.cpp @@ -0,0 +1,38 @@ +#include "../include/solution.hpp" + +class RemoveColoredPiecesIfBothNeighborsAreTheSameColor : public ::testing::Test { +protected: + ~RemoveColoredPiecesIfBothNeighborsAreTheSameColor() override = default; +}; + +TEST_F(RemoveColoredPiecesIfBothNeighborsAreTheSameColor, FirstTest) { + std::string colors = "AAABABB"; + + bool result = Solution::winner_of_game(colors); + bool expected = true; + + ASSERT_EQ(result, expected); +} + +TEST_F(RemoveColoredPiecesIfBothNeighborsAreTheSameColor, SecondTest) { + std::string colors = "AA"; + + bool result = Solution::winner_of_game(colors); + bool expected = false; + + ASSERT_EQ(result, expected); +} + +TEST_F(RemoveColoredPiecesIfBothNeighborsAreTheSameColor, ThirdTest) { + std::string colors = "ABBBBBBBAAA"; + + bool result = Solution::winner_of_game(colors); + bool expected = false; + + ASSERT_EQ(result, expected); +} + +auto main(int argc, char **argv) -> int { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}