Skip to content

Commit 083e34c

Browse files
committed
Merge branch 'develop'
2 parents 1ffc97c + 40ee2b7 commit 083e34c

34 files changed

+3892
-253
lines changed

.github/workflows/cmake.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ jobs:
4141
run: ./vcpkg install gsl:x64-linux gtest:x64-linux benchmark:x64-linux fmt:x64-linux
4242

4343
- name: Configure CMake
44-
run: cmake --preset linux-${{env.BUILD_TYPE_LOWERCASE}} -S ${{github.workspace}}/cpp-algorithm -B ${{github.workspace}}/cpp-algorithm/out/build/linux-x64-${{env.BUILD_TYPE_LOWERCASE}}
44+
working-directory: ${{github.workspace}}/cpp-algorithm
45+
run: make configure
4546

4647
- name: Build
47-
working-directory: ${{github.workspace}}/cpp-algorithm/out/build/linux-x64-${{env.BUILD_TYPE_LOWERCASE}}
48-
run: cmake --build . --config ${{env.BUILD_TYPE}} --target all --parallel 6
48+
working-directory: ${{github.workspace}}/cpp-algorithm
49+
run: make build
4950

5051
- name: Test
51-
working-directory: ${{github.workspace}}/cpp-algorithm/out/build/linux-x64-${{env.BUILD_TYPE_LOWERCASE}}
52-
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure
52+
working-directory: ${{github.workspace}}/cpp-algorithm
53+
run: make test

README.md

+141-117
Large diffs are not rendered by default.

README_ko-KR.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
<!-- markdownlint-disable MD033 MD036 -->
44

5-
![C++ CMake Test](https://github.com/codejsha/algorithm-examples/actions/workflows/cmake.yml/badge.svg) ![Python Poetry Test](https://github.com/codejsha/algorithm-examples/actions/workflows/poetry.yml/badge.svg) ![Java Gradle Test](https://github.com/codejsha/algorithm-examples/actions/workflows/gradle.yml/badge.svg) ![Go Test](https://github.com/codejsha/algorithm-examples/actions/workflows/go.yml/badge.svg)
5+
![C++ CMake Test](https://github.com/codejsha/algorithm-examples/actions/workflows/cmake.yml/badge.svg) ![Python Poetry Test](https://github.com/codejsha/algorithm-examples/actions/workflows/poetry.yml/badge.svg) ![Go Test](https://github.com/codejsha/algorithm-examples/actions/workflows/go.yml/badge.svg) ![Java Gradle Test](https://github.com/codejsha/algorithm-examples/actions/workflows/gradle.yml/badge.svg)
66

77
[English](README.md) | [Korean](README_ko-KR.md)
88

9-
이 리포지토리는 알고리즘, 데이터 구조, 문제 해결에 대한 구현 예제들로 구성되어 있습니다. 예제들은 C++, Python, Java, Go로 작성되었으며, 각 언어는 다음과 같은 테스트 프레임워크를 사용합니다: [Google Test](https://google.github.io/googletest/)(C++), [pytest](https://docs.pytest.org/)(Python), [JUnit](https://junit.org/)(Java), [go test](https://pkg.go.dev/cmd/go)(Go). 테스트를 실행하여 알고리즘 로직에 대한 메서드/함수를 수행합니다.
9+
이 리포지토리는 알고리즘, 데이터 구조, 문제 해결 접근방식의 예제들을 제공하며 실제 응용을 위한 목적으로 구현되었습니다. 예제들은 C++, Python, Go, Java로 구현되었으며, 각 언어는 테스트 프레임워크를 활용합니다. 테스트를 실행하여 알고리즘 로직에 대한 메서드/함수를 수행할 수 있습니다.
1010

1111
## Project Environments
1212

@@ -16,5 +16,5 @@
1616
| -------- | ------- | ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
1717
| C++ | C++20 | [CMake](https://cmake.org/) | [GNU Scientific Library (GSL)](https://www.gnu.org/software/gsl/), [Google Test](https://google.github.io/googletest/), [Google Benchmark](https://github.com/google/benchmark), [fmt](https://github.com/fmtlib/fmt) | [vcpkg](https://github.com/microsoft/vcpkg) |
1818
| Python | 3.12 | [Poetry](https://python-poetry.org/) | [NumPy](https://numpy.org/), [SciPy](https://www.scipy.org/), [NetworkX](https://networkx.org/), [pytest](https://docs.pytest.org/), [pytest-benchmark](https://pytest-benchmark.readthedocs.io/en/latest/) | |
19+
| Go | 1.22 | [Go](https://pkg.go.dev/cmd/go) | [Gonum](https://github.com/gonum/gonum), [Testify](https://github.com/stretchr/testify) | |
1920
| Java | 21 | [Gradle](https://gradle.org/) | [Google Guava](https://github.com/google/guava), [JUnit](https://junit.org/), [Java Microbenchmark Harness (JMH)](https://github.com/openjdk/jmh) | |
20-
| Go | 1.22 | [Go](https://pkg.go.dev/cmd/go) | [Testify](https://github.com/stretchr/testify) | |

cpp-algorithm/.editorconfig

+38-36
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ ij_java_wrap_long_lines = false
346346
ij_java_wrap_semicolon_after_call_chain = false
347347

348348
[*.less]
349-
indent_size = 2
349+
ij_less_indent_size = 2
350350
ij_less_align_closing_brace_with_properties = false
351351
ij_less_blank_lines_around_nested_selector = 1
352352
ij_less_blank_lines_between_blocks = 1
@@ -369,9 +369,9 @@ ij_less_use_double_quotes = true
369369
ij_less_value_alignment = 0
370370

371371
[*.proto]
372-
indent_size = 2
373-
tab_width = 2
374-
ij_continuation_indent_size = 4
372+
ij_protobuf_indent_size = 2
373+
ij_protobuf_continuation_indent_size = 4
374+
ij_protobuf_tab_width = 2
375375
ij_protobuf_keep_blank_lines_in_code = 1
376376
ij_protobuf_keep_indents_on_empty_lines = false
377377
ij_protobuf_keep_line_breaks = true
@@ -382,7 +382,7 @@ ij_protobuf_spaces_within_braces = false
382382
ij_protobuf_spaces_within_brackets = false
383383

384384
[*.sass]
385-
indent_size = 2
385+
ij_sass_indent_size = 2
386386
ij_sass_align_closing_brace_with_properties = false
387387
ij_sass_blank_lines_around_nested_selector = 1
388388
ij_sass_blank_lines_between_blocks = 1
@@ -404,9 +404,9 @@ ij_sass_use_double_quotes = true
404404
ij_sass_value_alignment = 0
405405

406406
[*.scala]
407-
indent_size = 2
408-
tab_width = 2
409-
ij_continuation_indent_size = 2
407+
ij_scala_indent_size = 2
408+
ij_scala_continuation_indent_size = 2
409+
ij_scala_tab_width = 2
410410
ij_scala_align_composite_pattern = true
411411
ij_scala_align_extends_with = 0
412412
ij_scala_align_group_field_declarations = false
@@ -605,7 +605,7 @@ ij_scala_wrap_first_method_in_call_chain = false
605605
ij_scala_wrap_long_lines = false
606606

607607
[*.scss]
608-
indent_size = 2
608+
ij_scss_indent_size = 2
609609
ij_scss_align_closing_brace_with_properties = false
610610
ij_scss_blank_lines_around_nested_selector = 1
611611
ij_scss_blank_lines_between_blocks = 1
@@ -628,9 +628,9 @@ ij_scss_use_double_quotes = true
628628
ij_scss_value_alignment = 0
629629

630630
[*.vue]
631-
indent_size = 2
632-
tab_width = 2
633-
ij_continuation_indent_size = 4
631+
ij_vue_indent_size = 2
632+
ij_vue_continuation_indent_size = 4
633+
ij_vue_tab_width = 2
634634
ij_vue_indent_children_of_top_level = template
635635
ij_vue_interpolation_new_line_after_start_delimiter = true
636636
ij_vue_interpolation_new_line_before_end_delimiter = true
@@ -666,7 +666,8 @@ ij_xml_space_inside_empty_tag = false
666666
ij_xml_text_wrap = normal
667667

668668
[{*.ats,*.cts,*.mts,*.ts}]
669-
ij_continuation_indent_size = 4
669+
ij_typescript_indent_size = 2
670+
ij_typescript_continuation_indent_size = 4
670671
ij_typescript_align_imports = false
671672
ij_typescript_align_multiline_array_initializer_expression = false
672673
ij_typescript_align_multiline_binary_operation = false
@@ -812,7 +813,7 @@ ij_typescript_spaces_within_brackets = false
812813
ij_typescript_spaces_within_catch_parentheses = false
813814
ij_typescript_spaces_within_for_parentheses = false
814815
ij_typescript_spaces_within_if_parentheses = false
815-
ij_typescript_spaces_within_imports = false
816+
ij_typescript_spaces_within_imports = true
816817
ij_typescript_spaces_within_interpolation_expressions = false
817818
ij_typescript_spaces_within_method_call_parentheses = false
818819
ij_typescript_spaces_within_method_parentheses = false
@@ -840,8 +841,8 @@ ij_typescript_while_on_new_line = false
840841
ij_typescript_wrap_comments = false
841842

842843
[{*.bash,*.sh,*.zsh}]
843-
indent_size = 2
844-
tab_width = 2
844+
ij_shell_indent_size = 2
845+
ij_shell_tab_width = 2
845846
ij_shell_binary_ops_start_line = false
846847
ij_shell_keep_column_alignment_padding = false
847848
ij_shell_minify_program = false
@@ -850,7 +851,8 @@ ij_shell_switch_cases_indented = false
850851
ij_shell_use_unix_line_separator = true
851852

852853
[{*.cjs,*.js}]
853-
ij_continuation_indent_size = 4
854+
ij_javascript_indent_size = 2
855+
ij_javascript_continuation_indent_size = 4
854856
ij_javascript_align_imports = false
855857
ij_javascript_align_multiline_array_initializer_expression = false
856858
ij_javascript_align_multiline_binary_operation = false
@@ -992,7 +994,7 @@ ij_javascript_spaces_within_brackets = false
992994
ij_javascript_spaces_within_catch_parentheses = false
993995
ij_javascript_spaces_within_for_parentheses = false
994996
ij_javascript_spaces_within_if_parentheses = false
995-
ij_javascript_spaces_within_imports = false
997+
ij_javascript_spaces_within_imports = true
996998
ij_javascript_spaces_within_interpolation_expressions = false
997999
ij_javascript_spaces_within_method_call_parentheses = false
9981000
ij_javascript_spaces_within_method_parentheses = false
@@ -1250,22 +1252,22 @@ ij_dts_wrap_long_lines = false
12501252

12511253
[{*.go,*.go2}]
12521254
indent_style = tab
1253-
ij_continuation_indent_size = 4
1254-
ij_go_GROUP_CURRENT_PROJECT_IMPORTS = false
1255+
ij_go_continuation_indent_size = 4
1256+
ij_go_GROUP_CURRENT_PROJECT_IMPORTS = true
12551257
ij_go_add_leading_space_to_comments = true
1256-
ij_go_add_parentheses_for_single_import = false
1258+
ij_go_add_parentheses_for_single_import = true
12571259
ij_go_call_parameters_new_line_after_left_paren = true
12581260
ij_go_call_parameters_right_paren_on_new_line = true
12591261
ij_go_call_parameters_wrap = off
12601262
ij_go_fill_paragraph_width = 80
1261-
ij_go_group_stdlib_imports = false
1262-
ij_go_import_sorting = gofmt
1263+
ij_go_group_stdlib_imports = true
1264+
ij_go_import_sorting = goimports
12631265
ij_go_keep_indents_on_empty_lines = false
12641266
ij_go_local_group_mode = project
12651267
ij_go_local_package_prefixes =
1266-
ij_go_move_all_imports_in_one_declaration = false
1267-
ij_go_move_all_stdlib_imports_in_one_group = false
1268-
ij_go_remove_redundant_import_aliases = false
1268+
ij_go_move_all_imports_in_one_declaration = true
1269+
ij_go_move_all_stdlib_imports_in_one_group = true
1270+
ij_go_remove_redundant_import_aliases = true
12691271
ij_go_run_go_fmt_on_reformat = true
12701272
ij_go_use_back_quotes_for_imports = false
12711273
ij_go_wrap_comp_lit = off
@@ -1283,7 +1285,7 @@ ij_go_wrap_func_result_newline_before_rparen = true
12831285
[{*.gql,*.graphql,*.graphqls}]
12841286

12851287
[{*.graphqlconfig,*.graphqlrc,*.har,*.jsb2,*.jsb3,*.json,*.jsonc,*.postman_collection,*.postman_collection.json,*.postman_environment,*.postman_environment.json,.babelrc,.eslintrc,.prettierrc,.stylelintrc,.ws-context,bowerrc,jest.config}]
1286-
indent_size = 2
1288+
ij_json_indent_size = 2
12871289
ij_json_array_wrapping = split_into_lines
12881290
ij_json_keep_blank_lines_in_code = 0
12891291
ij_json_keep_indents_on_empty_lines = false
@@ -1328,8 +1330,8 @@ ij_html_space_inside_empty_tag = false
13281330
ij_html_text_wrap = normal
13291331

13301332
[{*.http,*.rest}]
1331-
indent_size = 0
1332-
ij_continuation_indent_size = 4
1333+
ij_http-request_indent_size = 0
1334+
ij_http-request_continuation_indent_size = 4
13331335
ij_http-request_call_parameters_wrap = normal
13341336
ij_http-request_method_parameters_wrap = split_into_lines
13351337
ij_http-request_space_before_comma = true
@@ -1428,8 +1430,8 @@ ij_kotlin_wrap_expression_body_functions = 1
14281430
ij_kotlin_wrap_first_method_in_call_chain = false
14291431

14301432
[{*.markdown,*.md}]
1431-
indent_size = 2
1432-
tab_width = 2
1433+
ij_markdown_indent_size = 2
1434+
ij_markdown_tab_width = 2
14331435
ij_markdown_force_one_space_after_blockquote_symbol = true
14341436
ij_markdown_force_one_space_after_header_symbol = true
14351437
ij_markdown_force_one_space_after_list_bullet = true
@@ -1483,9 +1485,9 @@ ij_mermaid_within_round = false
14831485
ij_mermaid_within_square = false
14841486

14851487
[{*.pb,*.textproto,*.txtpb}]
1486-
indent_size = 2
1487-
tab_width = 2
1488-
ij_continuation_indent_size = 4
1488+
ij_prototext_indent_size = 2
1489+
ij_prototext_continuation_indent_size = 4
1490+
ij_prototext_tab_width = 2
14891491
ij_prototext_keep_blank_lines_in_code = 1
14901492
ij_prototext_keep_indents_on_empty_lines = false
14911493
ij_prototext_keep_line_breaks = true
@@ -1569,7 +1571,7 @@ ij_python_use_continuation_indent_for_parameters = true
15691571
ij_python_wrap_long_lines = false
15701572

15711573
[{*.qml,*.qmltypes}]
1572-
ij_continuation_indent_size = 4
1574+
ij_qmllang_continuation_indent_size = 4
15731575
ij_qmllang_align_imports = false
15741576
ij_qmllang_align_multiline_array_initializer_expression = false
15751577
ij_qmllang_align_multiline_binary_operation = false
@@ -1751,7 +1753,7 @@ ij_qute_keep_indents_on_empty_lines = false
17511753
ij_toml_keep_indents_on_empty_lines = false
17521754

17531755
[{*.yaml,*.yml,.clang-format,.clang-tidy,_clang-format}]
1754-
indent_size = 2
1756+
ij_yaml_indent_size = 2
17551757
ij_yaml_align_values_properties = do_not_align
17561758
ij_yaml_autoinsert_sequence_marker = true
17571759
ij_yaml_block_mapping_on_new_line = false

cpp-algorithm/.gitignore

+1-6
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,5 @@ fabric.properties
128128
.idea/
129129
.vs/
130130
.vscode/
131-
out/
132-
cmake-build-debug/
133-
cmake-build-debug-visual-studio/
134-
cmake-build-release/
135-
cmake-build-release-visual-studio/
136-
Folder.DotSettings.user
137131
README_link.md
132+
!/Makefile

cpp-algorithm/Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
BUILD_TYPE:=debug
2+
3+
UNAME_S:=$(shell uname -s)
4+
ifeq ($(UNAME_S),Linux)
5+
PRESET_NAME:=linux-$(BUILD_TYPE)
6+
else ifeq ($(UNAME_S),Darwin)
7+
PRESET_NAME:=macos-$(BUILD_TYPE)
8+
else ifeq ($(UNAME_S),Windows_NT)
9+
PRESET_NAME:=windows-$(BUILD_TYPE)
10+
else
11+
$(error Unsupported platform: $(UNAME_S))
12+
endif
13+
14+
BUILD_DIR:=$(PWD)/out/build/$(PRESET_NAME)
15+
16+
.PHONY: default
17+
default:
18+
@echo "Please specify the target"
19+
20+
.PHONY: clean
21+
clean:
22+
rm -rf $(BUILD_DIR)
23+
24+
.PHONY: configure
25+
configure:
26+
cmake --preset $(PRESET_NAME)
27+
28+
.PHONY: build
29+
build:
30+
cmake --build $(BUILD_DIR) --target all --parallel 6
31+
32+
.PHONY: test
33+
test:
34+
ctest --test-dir $(BUILD_DIR)

cpp-algorithm/src/array/dutch_national_flag.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
auto DutchFlag::DutchFlagPartition1(const int pivot_index, std::vector<Color>& arr) -> std::vector<Color>
44
{
5-
const auto pivot = arr[pivot_index];
5+
const Color pivot = arr[pivot_index];
66

77
for (int i = 0; i < static_cast<Color>(arr.size()); ++i)
88
{
@@ -32,7 +32,7 @@ auto DutchFlag::DutchFlagPartition1(const int pivot_index, std::vector<Color>& a
3232

3333
auto DutchFlag::DutchFlagPartition2(const int pivot_index, std::vector<Color>& arr) -> std::vector<Color>
3434
{
35-
const auto pivot = arr[pivot_index];
35+
const Color pivot = arr[pivot_index];
3636

3737
int smaller = 0;
3838
for (int i = 0; i < static_cast<Color>(arr.size()); ++i)
@@ -57,7 +57,7 @@ auto DutchFlag::DutchFlagPartition2(const int pivot_index, std::vector<Color>& a
5757

5858
auto DutchFlag::DutchFlagPartition3(const int pivot_index, std::vector<Color>& arr) -> std::vector<Color>
5959
{
60-
const auto pivot = arr[pivot_index];
60+
const Color pivot = arr[pivot_index];
6161

6262
int smaller = 0;
6363
int equal = 0;

cpp-algorithm/src/array/random_data_sampling.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ auto RandomDataSampling::OfflineRandomSampling(const int k, std::vector<int>& ar
1414
return std::vector<int>{arr.begin(), arr.begin() + k};
1515
}
1616

17+
auto RandomDataSampling::OnlineRandomSampling(std::vector<int>::const_iterator begin,
18+
const std::vector<int>::const_iterator end,
19+
const int k)
20+
-> std::vector<int>
21+
{
22+
std::vector<int> running_sample;
23+
// save the first k elements
24+
for (int i = 0; i < k; ++i)
25+
{
26+
running_sample.emplace_back(*begin++);
27+
}
28+
29+
std::default_random_engine seed((std::random_device())());
30+
int num_seen_so_far = k;
31+
while (begin != end)
32+
{
33+
int x = *begin++;
34+
++num_seen_so_far;
35+
36+
// generate a random number in [0, num_seen_so_far].
37+
// if the generated number exists in [0, k), replace the element with x.
38+
const int idx_to_replace = std::uniform_int_distribution<int>{0, num_seen_so_far - 1}(seed);
39+
if (idx_to_replace < k)
40+
{
41+
running_sample[idx_to_replace] = x;
42+
}
43+
}
44+
45+
return running_sample;
46+
}
47+
1748
auto RandomDataSampling::ComputeRandomPermutation(const int n) -> std::vector<int>
1849
{
1950
auto permutation = std::vector<int>(n);

cpp-algorithm/src/array/random_data_sampling.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66
namespace RandomDataSampling
77
{
88
/**
9-
* \brief Randomly select k elements from the array.
9+
* \brief Select the k elements randomly from the array with uniform probability.
10+
* Let A be an array with n distinct elements. Design an algorithm to return a random subset of k elements from A, where every subset has an equal chance of being chosen.
1011
* \param k sample size
1112
* \param arr input array
1213
* \return result array
1314
*/
1415
auto OfflineRandomSampling(int k, std::vector<int>& arr) -> std::vector<int>;
1516

16-
// TODO: Implement OnlineRandomSampling
1717
/**
18-
* \brief Randomly select k elements from the array.
18+
* \brief Select the k elements randomly from the array with uniform probability.
19+
* Design an algorithm that reads data and creates a random subset of size k, where each item has an equal chance of being included.
1920
* \param begin begin iterator
2021
* \param end end iterator
2122
* \param k sample size
2223
* \return result array
2324
*/
24-
auto OnlineRandomSampling(const std::vector<int>::const_iterator& begin,
25-
const std::vector<int>::const_iterator& end, int k)
25+
auto OnlineRandomSampling(std::vector<int>::const_iterator begin,
26+
std::vector<int>::const_iterator end,
27+
int k)
2628
-> std::vector<int>;
2729

2830
/**

cpp-algorithm/src/array/stock_trading.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ auto StockTrading::BuyAndSellStockOnce(const std::vector<int>& prices) -> double
2727

2828
for (double price : prices)
2929
{
30-
auto max_profit_sell_today = price - min_price_so_far;
30+
double max_profit_sell_today = price - min_price_so_far;
3131
max_profit = std::max(max_profit, max_profit_sell_today);
3232
min_price_so_far = std::min(min_price_so_far, price);
3333
}

0 commit comments

Comments
 (0)