Skip to content

Commit 63a75ce

Browse files
committed
feat: add programmers-154538
1 parent a1c0c8b commit 63a75ce

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

coding-test-cpp-practice/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ add_subdirectory(problem43164)
77
add_subdirectory(problem64063)
88
add_subdirectory(problem72411)
99
add_subdirectory(problem132265)
10+
add_subdirectory(problem154538)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
enable_testing()
2+
find_package(GTest REQUIRED)
3+
4+
if(WIN32)
5+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
6+
endif(WIN32)
7+
8+
macro(add_library_target target)
9+
add_library(${target} STATIC "${target}.cpp")
10+
target_link_libraries(${target} PRIVATE ${ARGN})
11+
set_property(TARGET ${target} PROPERTY CXX_STANDARD 17)
12+
endmacro()
13+
14+
macro(add_target_test target)
15+
string(REPLACE "_test" "" target_name ${target})
16+
add_executable(${target} "${target}.cpp" "${target_name}.cpp")
17+
target_link_libraries(${target} PRIVATE GTest::gtest GTest::gtest_main ${ARGN})
18+
if(MSVC)
19+
### Edit and Continue for CMake projects
20+
target_compile_options(${target} PUBLIC "/Zi")
21+
target_link_options(${target} PUBLIC "/INCREMENTAL")
22+
endif()
23+
gtest_discover_tests(${target})
24+
endmacro()
25+
26+
add_library_target(converting_number)
27+
28+
add_target_test(converting_number_test)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "converting_number.h"
2+
3+
#include <queue>
4+
#include <unordered_set>
5+
#include <vector>
6+
7+
int bfs(int input, int target, const std::vector<std::function<int(int)>>& operations)
8+
{
9+
struct State
10+
{
11+
int value;
12+
int steps;
13+
};
14+
15+
std::queue<State> q;
16+
std::unordered_set<int> visited;
17+
18+
q.push({input, 0});
19+
visited.insert(input);
20+
21+
while (!q.empty())
22+
{
23+
auto current = q.front();
24+
q.pop();
25+
26+
if (current.value == target)
27+
{
28+
return current.steps;
29+
}
30+
31+
for (const auto& op : operations)
32+
{
33+
int next_value = op(current.value);
34+
if (next_value <= target && visited.find(next_value) == visited.end())
35+
{
36+
visited.insert(next_value);
37+
q.push({next_value, current.steps + 1});
38+
}
39+
}
40+
}
41+
42+
return -1;
43+
}
44+
45+
int ConvertingNumber::ConvertingNumberMinStep(int x, int y, const int n)
46+
{
47+
std::function<int(int, int)> multiply = [](int x, int k) { return x * k; };
48+
std::function<int(int, int)> add = [](int x, int k) { return x + k; };
49+
50+
auto multiply_three = [multiply](auto&& number) {
51+
return multiply(std::forward<decltype(number)>(number), 3);
52+
};
53+
auto multiply_two = [multiply](auto&& number) {
54+
return multiply(std::forward<decltype(number)>(number), 2);
55+
};
56+
auto add_n = [add, n](auto&& number) {
57+
return add(std::forward<decltype(number)>(number), n);
58+
};
59+
60+
std::vector<std::function<int(int)>> functions = {multiply_three, multiply_two, add_n};
61+
auto result = bfs(x, y, functions);
62+
return (result == 0) ? -1 : result;
63+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef CODING_TEST_CPP_PRACTICE_CONVERTING_NUMBER_H
2+
#define CODING_TEST_CPP_PRACTICE_CONVERTING_NUMBER_H
3+
4+
/**
5+
* not submitted to site yet
6+
*/
7+
namespace ConvertingNumber
8+
{
9+
/**
10+
* @brief Find the minimum number of steps to convert x to y
11+
* @param x input value
12+
* @param y target value
13+
* @param n operand value for addition
14+
* @return the minimum number of steps to convert x to y
15+
*/
16+
int ConvertingNumberMinStep(int x, int y, int n);
17+
}
18+
19+
#endif
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "converting_number.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
GTEST_TEST(ConvertingNumberMinStep, Successful1)
6+
{
7+
const auto x = 10;
8+
const auto y = 40;
9+
const auto n = 5;
10+
const auto expected = 2;
11+
12+
const auto actual = ConvertingNumber::ConvertingNumberMinStep(x, y, n);
13+
EXPECT_EQ(expected, actual);
14+
}
15+
16+
GTEST_TEST(ConvertingNumberMinStep, Successful2)
17+
{
18+
const auto x = 10;
19+
const auto y = 40;
20+
const auto n = 30;
21+
const auto expected = 1;
22+
23+
const auto actual = ConvertingNumber::ConvertingNumberMinStep(x, y, n);
24+
EXPECT_EQ(expected, actual);
25+
}
26+
27+
GTEST_TEST(ConvertingNumberMinStep, Failed1)
28+
{
29+
const auto x = 2;
30+
const auto y = 5;
31+
const auto n = 4;
32+
const auto expected = -1;
33+
34+
const auto actual = ConvertingNumber::ConvertingNumberMinStep(x, y, n);
35+
EXPECT_EQ(expected, actual);
36+
}

0 commit comments

Comments
 (0)