Skip to content

Commit 323350e

Browse files
committed
Merge branch 'develop' into main
2 parents 70e6d87 + c4b849b commit 323350e

File tree

9 files changed

+243
-0
lines changed

9 files changed

+243
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ add_subdirectory(problem43163)
66
add_subdirectory(problem43164)
77
add_subdirectory(problem64063)
88
add_subdirectory(problem72411)
9+
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(cutting_roll_cake)
27+
28+
add_target_test(cutting_roll_cake_test)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "cutting_roll_cake.h"
2+
3+
#include <unordered_map>
4+
5+
int CuttingRollCake::CuttingRollCake(std::vector<int>& topping)
6+
{
7+
auto total_map = std::unordered_map<int, int>{};
8+
for (const auto& item : topping)
9+
{
10+
++total_map[item];
11+
}
12+
13+
auto left_map = std::unordered_map<int, int>{};
14+
auto same_topping_case = 0;
15+
for (auto current : topping)
16+
{
17+
++left_map[current];
18+
--total_map[current];
19+
20+
if (total_map[current] == 0)
21+
{
22+
total_map.erase(current);
23+
}
24+
25+
if (left_map.size() == total_map.size())
26+
{
27+
++same_topping_case;
28+
}
29+
}
30+
31+
return same_topping_case;
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef CODING_TEST_CPP_PRACTICE_CUTTING_ROLL_CAKE_H
2+
#define CODING_TEST_CPP_PRACTICE_CUTTING_ROLL_CAKE_H
3+
4+
#include <vector>
5+
6+
namespace CuttingRollCake
7+
{
8+
/**
9+
* @brief Find the number of cases that the left and right pieces of cake have the same topping
10+
* @param topping topping array of cake
11+
* @return the number of cases that the left and right pieces of cake have the same topping
12+
*/
13+
int CuttingRollCake(std::vector<int>& topping);
14+
}
15+
16+
#endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "cutting_roll_cake.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
GTEST_TEST(CuttingRollCake, Successful1)
6+
{
7+
auto topping = std::vector<int>{1, 2, 1, 3, 1, 4, 1, 2};
8+
const auto expected = 2;
9+
const auto actual = CuttingRollCake::CuttingRollCake(topping);
10+
EXPECT_EQ(expected, actual);
11+
}
12+
13+
GTEST_TEST(CuttingRollCake, Successful2)
14+
{
15+
auto topping = std::vector<int>{1, 2, 3, 1, 4};
16+
const auto expected = 0;
17+
const auto actual = CuttingRollCake::CuttingRollCake(topping);
18+
EXPECT_EQ(expected, actual);
19+
}
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)