Skip to content

Commit

Permalink
Final Value of Variable After Performing Operations
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 20, 2023
1 parent 9a3e594 commit e27f2cd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_final_value_of_variable_after_performing_operations test.cpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <string>
#include <vector>

class Solution {
public:
static int
finalValueAfterOperations(const std::vector<std::string> &operations) {
int x = 0;
for (const auto &op : operations) {
x += op[1] == '+' ? 1 : -1;
}
return x;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
std::vector<std::string> operations;
int expected;
};

std::vector<TestCase> 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);
}
}

0 comments on commit e27f2cd

Please sign in to comment.