-
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.
- Loading branch information
Showing
5 changed files
with
135 additions
and
1 deletion.
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
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,25 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
project(roman_numerals) | ||
|
||
# GoogleTest requires at least C++14 | ||
set(CMAKE_CXX_STANDARD 14) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
enable_testing() | ||
|
||
add_executable( | ||
roman_numerals_runner | ||
runner.cpp | ||
) | ||
|
||
add_executable( | ||
roman_numerals_tests | ||
tests.cpp | ||
) | ||
target_link_libraries( | ||
roman_numerals_tests | ||
GTest::gtest_main | ||
) | ||
include_directories(${gtest_SOURCE_DIR}/include ${COMMON_INCLUDES}) | ||
|
||
gtest_discover_tests(roman_numerals_tests) |
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,13 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include "solution.hpp" | ||
|
||
int main() | ||
{ | ||
const std::string input("XCXLV"); | ||
Solution s; | ||
std::cout << "\"" << input << "\": " << s.romanToInt(input) << std::endl; | ||
|
||
return 1; | ||
} |
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,42 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
class Solution { | ||
public: | ||
int romanToInt(const std::string& s) | ||
{ | ||
int result = 0; | ||
char prev = 'z'; | ||
for (const char c : s) | ||
{ | ||
switch (c) | ||
{ | ||
case 'I': | ||
result += 1; | ||
break; | ||
case 'V': | ||
result += prev == 'I' ? 3 : 5; | ||
break; | ||
case 'X': | ||
result += prev == 'I' ? 8 : 10; | ||
break; | ||
case 'L': | ||
result += prev == 'X' ? 30 : 50; | ||
break; | ||
case 'C': | ||
result += prev == 'X' ? 80 : 100; | ||
break; | ||
case 'D': | ||
result += prev == 'C' ? 300 :500; | ||
break; | ||
case 'M': | ||
result += prev == 'C' ? 800 :1000; | ||
break; | ||
} | ||
prev = c; | ||
} | ||
|
||
return result; | ||
} | ||
}; |
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,53 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "solution.hpp" | ||
|
||
class RomanNumeralsTest :public ::testing::TestWithParam<std::pair<std::string, int>> { | ||
protected: | ||
Solution solution; | ||
}; | ||
|
||
TEST_P(RomanNumeralsTest, CheckSolution) { | ||
std::string input = std::get<0>(GetParam()); | ||
int expected = std::get<1>(GetParam()); | ||
|
||
int actual = solution.romanToInt(input); | ||
|
||
EXPECT_EQ(expected, actual); | ||
} | ||
|
||
auto values = ::testing::Values( | ||
std::make_pair("I", 1), | ||
std::make_pair("II", 2), | ||
std::make_pair("III", 3), | ||
std::make_pair("V", 5), | ||
std::make_pair("VI", 6), | ||
std::make_pair("IV", 4), | ||
std::make_pair("VII", 7), | ||
std::make_pair("X", 10), | ||
std::make_pair("XXVII", 27), | ||
std::make_pair("XXI", 21), | ||
std::make_pair("IX", 9), | ||
std::make_pair("XXIX", 29), | ||
std::make_pair("L", 50), | ||
std::make_pair("LI", 51), | ||
std::make_pair("XLVI", 46), | ||
std::make_pair("C", 100), | ||
std::make_pair("CXX", 120), | ||
std::make_pair("XCXLV", 135), | ||
std::make_pair("CXC", 190), | ||
std::make_pair("D", 500), | ||
std::make_pair("DC", 600), | ||
std::make_pair("CDLXIII", 463), | ||
std::make_pair("M", 1000), | ||
std::make_pair("MCMXCIX", 1999) | ||
); | ||
|
||
INSTANTIATE_TEST_CASE_P( | ||
RomanNumeralsTests, | ||
RomanNumeralsTest, | ||
values | ||
); |