From e27f2cd01cc63ab0f03face43ff394b0fe9e3218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BE=D1=80=D0=B5=D0=B2=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Fri, 20 Oct 2023 10:25:21 +0300 Subject: [PATCH] Final Value of Variable After Performing Operations --- CMakeLists.txt | 1 + .../CMakeLists.txt | 1 + .../solution.hpp | 16 ++++++++++ .../test.cpp | 30 +++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 solutions/final-value-of-variable-after-performing-operations/CMakeLists.txt create mode 100644 solutions/final-value-of-variable-after-performing-operations/solution.hpp create mode 100644 solutions/final-value-of-variable-after-performing-operations/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e9dc0d65..89810724 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,6 +219,7 @@ add_task(factorial-trailing-zeroes) add_task(fair-distribution-of-cookies) add_task(faulty-keyboard) add_task(fibonacci-number) +add_task(final-value-of-variable-after-performing-operations) add_task(find-a-peak-element-ii) add_task(find-all-anagrams-in-a-string) add_task(find-all-lonely-numbers-in-the-array) diff --git a/solutions/final-value-of-variable-after-performing-operations/CMakeLists.txt b/solutions/final-value-of-variable-after-performing-operations/CMakeLists.txt new file mode 100644 index 00000000..7a9a5cbd --- /dev/null +++ b/solutions/final-value-of-variable-after-performing-operations/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_final_value_of_variable_after_performing_operations test.cpp) diff --git a/solutions/final-value-of-variable-after-performing-operations/solution.hpp b/solutions/final-value-of-variable-after-performing-operations/solution.hpp new file mode 100644 index 00000000..8decbf22 --- /dev/null +++ b/solutions/final-value-of-variable-after-performing-operations/solution.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +class Solution { +public: + static int + finalValueAfterOperations(const std::vector &operations) { + int x = 0; + for (const auto &op : operations) { + x += op[1] == '+' ? 1 : -1; + } + return x; + } +}; diff --git a/solutions/final-value-of-variable-after-performing-operations/test.cpp b/solutions/final-value-of-variable-after-performing-operations/test.cpp new file mode 100644 index 00000000..8b3bd8a2 --- /dev/null +++ b/solutions/final-value-of-variable-after-performing-operations/test.cpp @@ -0,0 +1,30 @@ +#include + +#include + +TEST_CASE("Simple") { + struct TestCase { + std::vector operations; + int expected; + }; + + std::vector test_cases{ + { + .operations{"--X", "X++", "X++"}, + .expected = 1, + }, + { + .operations{"++X", "++X", "X++"}, + .expected = 3, + }, + { + .operations{"X++", "++X", "--X", "X--"}, + .expected = 0, + }, + }; + + for (const auto &[operations, expected] : test_cases) { + const auto actual = Solution::finalValueAfterOperations(operations); + REQUIRE(expected == actual); + } +}