-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Final Value of Variable After Performing Operations
- Loading branch information
Showing
4 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
solutions/final-value-of-variable-after-performing-operations/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
16 changes: 16 additions & 0 deletions
16
solutions/final-value-of-variable-after-performing-operations/solution.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
30 changes: 30 additions & 0 deletions
30
solutions/final-value-of-variable-after-performing-operations/test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |