Skip to content

Commit

Permalink
Solved roman-numerals
Browse files Browse the repository at this point in the history
  • Loading branch information
orhtej2 committed Jun 4, 2023
1 parent 4fbcc82 commit 819a25b
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ enable_testing()
include(GoogleTest)

add_subdirectory( two-sum )
add_subdirectory( longest-substring-without-repeating-characters )
add_subdirectory( longest-substring-without-repeating-characters )
add_subdirectory( roman-numerals )
25 changes: 25 additions & 0 deletions roman-numerals/CMakeLists.txt
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)
13 changes: 13 additions & 0 deletions roman-numerals/runner.cpp
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;
}
42 changes: 42 additions & 0 deletions roman-numerals/solution.hpp
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;
}
};
53 changes: 53 additions & 0 deletions roman-numerals/tests.cpp
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
);

0 comments on commit 819a25b

Please sign in to comment.