diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 00000000..c4bbcc3a --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,79 @@ +# @Github: https://github.com/Certseeds/CS203_DSAA_template +# @Organization: SUSTech +# @Author: nanoseeds +# @Date: 2020-07-26 16:36:10 +# @LastEditors: nanoseeds +#@LastEditTime: 2020-07-26 16:43:03 +# This is a basic workflow to help you get started with Actions + +name: test for pull_request + +# Controls when the action will run. Triggers the workflow on push or pull request +# events but only for the master branch +on: + pull_request: + branches: [ master ] + push: + branches: [ dev ] +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + test: + name: test for pull_request + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - name: checkout code + uses: actions/checkout@v2 + + # ensure the path and files of project + - name: ensure the path and files of project + run: sudo apt install tree; tree + + - name: Setup cmake + uses: jwlawson/actions-setup-cmake@v1.3 + with: + cmake-version: '3.10.2' + + - name: Use cmake + run: cmake --version + + - name: prepare permission + working-directory: ./script + run: chmod 0755 ./test.sh + + - name: run the test script + working-directory: ./script + run: ./test.sh + + - name: tree + run: tree + + # This workflow contains a single job called "build" + build: + name: test for the release script + needs: test + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - name: checkout code + uses: actions/checkout@v2 + + # ensure the path and files of project + - name: ensure the path and files of project + run: sudo apt install tree; tree + + - name: run script + working-directory: ./script + run: python3 file_template.py + + - name: tree the packet + run: tree + diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9c33dac5..1f00edd6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,8 +13,6 @@ name: test and release on: push: branches: [ master ] - pull_request: - branches: [ master ] # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: @@ -32,10 +30,7 @@ jobs: # ensure the path and files of project - name: ensure the path and files of project - run: | - echo Hello, world! - echo $(pwd) - ls -la + run: sudo apt install tree; tree - name: Setup cmake uses: jwlawson/actions-setup-cmake@v1.3 @@ -45,31 +40,17 @@ jobs: - name: Use cmake run: cmake --version - - name: mkdir for build - run: mkdir ./cmake_build_path + - name: prepare permission + working-directory: ./script + run: chmod 0755 ./test.sh - - name: build the project - working-directory: ./cmake_build_path - run: | - cmake .. - make + - name: run the test script + working-directory: ./script + run: ./test.sh - - name: run the test - working-directory: ./cmake_build_path - run: ./CS203_DSAA_template_test - - - name: run the algorithm_sort_test - working-directory: ./cmake_build_path/algorithm/sort - run: ./basic_algorithms_sort - - - name: run the binary_search test - working-directory: ./cmake_build_path/algorithm/binary_search - run: ./basic_algorithms_binary_search + - name: tree + run: tree - - name: Run a multi-line script - run: | - echo $(pwd) - ls -la # This workflow contains a single job called "build" build: name: publish release @@ -87,11 +68,8 @@ jobs: # ensure the path and files of project - name: ensure the path and files of project - run: | - echo Hello, world! - echo $(pwd) - ls -la - + run: sudo apt install tree; tree + - name: run script working-directory: ./script run: python3 file_template.py @@ -99,9 +77,8 @@ jobs: - name: zip the packet working-directory: ./../ run: | - echo $(pwd) zip -r script_no_need.zip ./CS203_DSAA_template -x "*/.git/*" - echo $(pwd) + tree - name: Create Release id: create_release @@ -109,8 +86,8 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - tag_name: 0.0.6.4 - release_name: 0.0.6.4 + tag_name: 0.0.7 + release_name: 0.0.7 draft: false prerelease: false @@ -130,8 +107,6 @@ jobs: asset_name: script_no_need.zip asset_content_type: application/zip - # Runs a set of commands using the runners shell - - name: Run a multi-line script - run: | - echo $(pwd) - ls -la + - name: tree + run: tree + diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..84bcc567 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,56 @@ +# +# @Github: https://github.com/Certseeds/CS203_DSAA_template +# @Organization: SUSTech +# @Author: nanoseeds +# @Date: 2020-07-27 22:08:31 +# @LastEditors: nanoseeds +# @LastEditTime: 2020-07-27 22:10:08 + +name: test for dev + +# Controls when the action will run. Triggers the workflow on push or pull request +# events but only for the master branch +on: + push: + branches: [ dev ] + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + test: + name: test dev + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - name: checkout code + uses: actions/checkout@v2 + + # ensure the path and files of project + - name: ensure the path and files of project + run: | + sudo apt install tree + tree + + - name: Setup cmake + uses: jwlawson/actions-setup-cmake@v1.3 + with: + cmake-version: '3.10.2' + + - name: Use cmake + run: cmake --version + + - name: prepare permission + working-directory: ./script + run: chmod 0755 ./test.sh + + - name: run the test script + working-directory: ./script + run: ./test.sh + + - name: tree + run: tree + # This workflow contains a single job called "build" + \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4e710fc4..bbf0e3e1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ ## @Author: nanoseeds ## @Date: 2020-07-15 23:48:29 ## @LastEditors: nanoseeds -## @LastEditTime: 2020-07-27 10:19:54 +## @LastEditTime: 2020-07-28 10:16:00 ### ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. @@ -309,6 +309,7 @@ _deps # CMake cmake-build-*/ cmake_build_*/ +build/ # idea .idea/* !.idea/codeStyles diff --git a/.idea/fileTemplates/internal/C Header File.h b/.idea/fileTemplates/internal/C Header File.h index da851e41..8123e90b 100644 --- a/.idea/fileTemplates/internal/C Header File.h +++ b/.idea/fileTemplates/internal/C Header File.h @@ -1,3 +1,4 @@ +#set( $GITHUB_USER = "Certseeds" ) #parse("FILE_HEAD_CPP.h") #parse("LICENSE_MIT_CPP.h") #[[#ifndef]]# ${INCLUDE_GUARD} diff --git a/.idea/fileTemplates/internal/C Source File.c b/.idea/fileTemplates/internal/C Source File.c index 457eeacd..b0f76bd6 100644 --- a/.idea/fileTemplates/internal/C Source File.c +++ b/.idea/fileTemplates/internal/C Source File.c @@ -1,3 +1,4 @@ +#set( $GITHUB_USER = "Certseeds" ) #parse("FILE_HEAD_CPP.h") #parse("LICENSE_MIT_CPP.h") #if (${HEADER_FILENAME}) diff --git a/.idea/fileTemplates/internal/C++ Class.cc b/.idea/fileTemplates/internal/C++ Class.cc index 298426ac..1232fef6 100644 --- a/.idea/fileTemplates/internal/C++ Class.cc +++ b/.idea/fileTemplates/internal/C++ Class.cc @@ -1,3 +1,4 @@ +#set( $GITHUB_USER = "Certseeds" ) #parse("FILE_HEAD_CPP.h") #parse("LICENSE_MIT_CPP.h") #[[#include]]# "${HEADER_FILENAME}" diff --git a/.vscode/settings.json b/.vscode/settings.json index d7f01180..e4eec793 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -73,9 +73,9 @@ }, "git.ignoreLimitWarning": true, "shellcheck.enable": true, // 安装以后默认就是激活的 - "shellcheck.executablePath": "/usr/bin/shellcheck", // 也可以是shellcheck + "shellcheck.executablePath": "/usr/local/bin/shellcheck", // 也可以是shellcheck "shellcheck.run": "onType", // "onType"敲击键盘的时候检查,onSave是保存文件时候检查 - "shellcheck.useWSL": true, + //"shellcheck.useWSL": true, "shellcheck.enableQuickFix": true, "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}", "todo-tree.general.tags": [ @@ -173,7 +173,8 @@ "typeinfo": "cpp", "unordered_map": "cpp", "utility": "cpp", - "variant": "cpp" + "variant": "cpp", + "__functional_03": "cpp" }, "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools" } \ No newline at end of file diff --git a/README.md b/README.md index 37313feb..0ec14dae 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ + PS: 就像下面的修改,增加了两个Bonus_Lab,同时给每个Lab中的C拆分成了C1 && C2 ``` python labs: List[str] = ['01', '02', '03', '03Bouns', '04', '05', '06', '07','07Bouns','08', '09'] - problem_order: List[chr] = ['A', 'B', 'C1', 'C2','D', 'E', 'F', 'G'] + problem_order: List[str] = ['A', 'B', 'C1', 'C2','D', 'E', 'F', 'G'] ``` + 执行代码和测试: + 使用clion打开文件夹,配置好C++环境的基础上,会自动识别`CmakeList.txt`.产生`CS203_DSAA_template`,`CS203_DSAA_template_test`两个可以运行的可选项. @@ -93,13 +93,13 @@ + 比如[判断二分图](./source/lab_00/lab_00_C.cpp),一张图可以有几十上百个node,写在内部占用空间太大. + 而在这里,使用`CS203_redirect`对象,便可以省去手动输入的方式. ``` cpp - const string test_file_path = "./../test/lab_00/lab_00_C_data/"; TEST_CASE("test case 1", "[test 00 C]") { - CS203_redirect cr{test_file_path + "01.data.in", ""}; + CS203_redirect cr{"01.data.in", ""}; // 重定向开始,开始run - auto result_data = isBipartite(read()); + // or CS203_redirect cr{"01.data.in"}; + auto output_data = isBipartite(read()); // 重定向结束 - CHECK_FALSE(result_data); + CHECK_FALSE(output_data); } ``` 只需要准备好输入的数据与结果,就可以从文件中读取,执行后判断结果是否符合预期. @@ -115,18 +115,18 @@ + 一般来说,题目的输出不会太复杂,但是反例也不是没有.:比如专门考输出的[立体图](./source/lab_00/lab_00_D.cpp) + 这种情况下,使用c++的重定向输出就可以较为方便的对输入进行处理,同时保存输出方便调试. ``` cpp - const string test_file_path = "./../test/lab_00/lab_00_D_data/"; - TEST_CASE("test case 2", "[test 00 C]") { + TEST_CASE("test case 2", "[test 00 D]") { SECTION("do") { - CS203_redirect cr{test_file_path + "01.data.in", test_file_path + "01.test.out"}; - cal(read()); + CS203_redirect cr{"01.data.in", "01.test.out"}; + auto input_data = read(); + cal(input_data); }SECTION("compare files") { - CHECK(compareFiles(test_file_path + "01.test.out", test_file_path + "01.data.out")); + CHECK(compareFiles("01.test.out", "01.data.out")); } } ``` 这样就将标准输出重定向到了01.test.out中,并与01.data.out比对. - + 至于比较文件之间的差异,可以使用内置的`compareFiles(const string& path1,const string& path2)`函数进行比较. + + 至于比较文件之间的差异,可以使用内置的`compareFiles(string path1,string path2)`函数进行比较. 参考[文本比对_test_case_2](./test/lab_00/lab_00_D_test.cpp) 5. 为什么要将 `读取` `数据处理` `输出` 分开? diff --git a/algorithm/CMakeLists.txt b/algorithm/CMakeLists.txt index 84f2b50a..ee53b16c 100644 --- a/algorithm/CMakeLists.txt +++ b/algorithm/CMakeLists.txt @@ -7,3 +7,5 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") add_subdirectory(./sort) add_subdirectory(./binary_search) + +add_subdirectory(./tree) diff --git a/algorithm/binary_search/CmakeLists.txt b/algorithm/binary_search/CMakeLists.txt similarity index 69% rename from algorithm/binary_search/CmakeLists.txt rename to algorithm/binary_search/CMakeLists.txt index ca566681..e3fa2511 100644 --- a/algorithm/binary_search/CmakeLists.txt +++ b/algorithm/binary_search/CMakeLists.txt @@ -4,5 +4,7 @@ project(basic_algorithms_binary_search LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") add_executable(${PROJECT_NAME} - ./basic.cpp - ) \ No newline at end of file + #./basic.cpp + ./triple_search.cpp + #rotate_array.cpp + ) \ No newline at end of file diff --git a/algorithm/binary_search/basic.cpp b/algorithm/binary_search/basic.cpp index 6ee06103..e05b3f8d 100644 --- a/algorithm/binary_search/basic.cpp +++ b/algorithm/binary_search/basic.cpp @@ -8,23 +8,23 @@ */ #include -#include #include #include #include "catch_main.hpp" + using Catch::Matchers::Contains; using Catch::Matchers::Equals; using Catch::Matchers::UnorderedEquals; -using std::cout; -using std::endl; using std::tie; using std::tuple; using std::vector; using std::vector; + +// !IMPORTANT [first,last)!! // nums升序,寻找[first,last)内,第一个不小于value的值(x>=value的第一个) -int lower_bound(vector nums, int32_t first, int32_t last, int value) { +int lower_bound(vector &nums, int32_t first, int32_t last, int value) { while (first < last) { int middle = first + (last - first) / 2; if (nums[middle] < value) { @@ -35,20 +35,27 @@ int lower_bound(vector nums, int32_t first, int32_t last, int value) { } return first; } + TEST_CASE("lower_bound", "[test basic binary search]") { vector vec{1, 1, 2, 3, 4, 4, 4, 5, 5, 6}; vector> input_result = { - {1, 0}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {6, 9}}; - for (const auto& i : input_result) { + {1, 0}, + {2, 2}, + {3, 3}, + {4, 4}, + {5, 7}, + {6, 9}}; + for (const auto &i : input_result) { int input{0}; int output{0}; tie(input, output) = i; CHECK(output == lower_bound(vec, 0, vec.size(), input)); } } + // nums升序,寻找[first,last)内,任意等于value的值. -// 任意! -int any_equal(vector nums, int32_t first, int32_t last, int value) { +// 任意! +int any_equal(vector &nums, int32_t first, int32_t last, int value) { while (first < last) { int middle = first + (last - first) / 2; if (nums[middle] == value) { @@ -61,12 +68,18 @@ int any_equal(vector nums, int32_t first, int32_t last, int value) { } return first; } + TEST_CASE("any_equal", "[test basic binary search]") { vector vec{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6}; vector> input_result = { - {1, 0, 1}, {2, 1, 3}, {3, 3, 6}, {4, 6, 10}, {5, 10, 15}, {6, 15, 16}}; + {1, 0, 1}, + {2, 1, 3}, + {3, 3, 6}, + {4, 6, 10}, + {5, 10, 15}, + {6, 15, 16}}; // 0 2 4 8 12 15 - for (const auto& i : input_result) { + for (const auto &i : input_result) { int32_t input{0}; int32_t left{0}; int32_t right{0}; @@ -76,8 +89,9 @@ TEST_CASE("any_equal", "[test basic binary search]") { CHECK(result < right); } } + // nums升序,寻找[first,last)内,第一个大于value的值(x>value的第一个) -int upper_bound(vector nums, int32_t first, int32_t last, int value) { +int upper_bound(vector &nums, int32_t first, int32_t last, int value) { while (first < last) { int middle = first + (last - first) / 2; if (!(nums[middle] > value)) { @@ -88,41 +102,61 @@ int upper_bound(vector nums, int32_t first, int32_t last, int value) { } return first; } + TEST_CASE("upper_bound", "[test basic binary search]") { vector vec{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6}; vector> input_result = { - {1, 1}, {2, 3}, {3, 6}, {4, 10}, {5, 15}, {6, 16}}; - for (const auto& i : input_result) { + {1, 1}, + {2, 3}, + {3, 6}, + {4, 10}, + {5, 15}, + {6, 16}}; + for (const auto &i : input_result) { int input{0}; int output{0}; tie(input, output) = i; CHECK(output == upper_bound(vec, 0, vec.size(), input)); } } + // 升序,【first,last)内,最后一个小于value的值(x nums, int32_t first, int32_t last, int value) { +int lower_bound_warpper(vector &nums, int32_t first, int32_t last, int value) { return lower_bound(nums, first, last, value) - 1; } + TEST_CASE("lower_bound_warpper", "[test basic binary search]") { vector vec{1, 1, 2, 3, 4, 4, 4, 5, 5, 6}; vector> input_result = { - {1, -1}, {2, 1}, {3, 2}, {4, 3}, {5, 6}, {6, 8}}; - for (const auto& i : input_result) { + {1, -1}, + {2, 1}, + {3, 2}, + {4, 3}, + {5, 6}, + {6, 8}}; + for (const auto &i : input_result) { int input{0}; int output{0}; tie(input, output) = i; CHECK(output == lower_bound_warpper(vec, 0, vec.size(), input)); } } + //升序,【first,last)内,最后一个小于等于value的值,(x<=value 最大值) -int upper_bound_warpper(vector nums, int32_t first, int32_t last, int value) { +int upper_bound_warpper(vector &nums, int32_t first, int32_t last, int value) { return upper_bound(nums, first, last, value) - 1; } + TEST_CASE("upper_bound_warpper", "[test basic binary search]") { vector vec{1, 1, 2, 3, 4, 4, 4, 5, 5, 6}; vector> input_result = { - {1, 1}, {2, 2}, {3, 3}, {4, 6}, {5, 8}, {6, 9}}; - for (const auto& i : input_result) { + {1, 1}, + {2, 2}, + {3, 3}, + {4, 6}, + {5, 8}, + {6, 9}}; + for (const auto &i : input_result) { int input{0}; int output{0}; tie(input, output) = i; diff --git a/algorithm/binary_search/rotate_array.cpp b/algorithm/binary_search/rotate_array.cpp new file mode 100644 index 00000000..8e2e1541 --- /dev/null +++ b/algorithm/binary_search/rotate_array.cpp @@ -0,0 +1,102 @@ +/** + * @Github: https://github.com/Certseeds/CS203_DSAA_template + * @Organization: SUSTech + * @Author: nanoseeds + * @Date: 2020-08-01 17:39:45 + * @LastEditors: nanoseeds + * @LICENSE: MIT + */ +/* +MIT License + +CS203_DSAA_template + +Copyright (C) 2020 nanoseeds + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +// refer https://leetcode-cn.com/problems/rotate-array/ +// 189 +#include +#include + +#include "catch_main.hpp" + +using Catch::Matchers::Contains; +using Catch::Matchers::Equals; +using Catch::Matchers::UnorderedEquals; +using std::tie; +using std::tuple; +using std::vector; + +// nums 一个有序数组, 集体右移k个位置(k>=0) +// [first,last] !! +int rorate_array_min(vector &nums, int32_t first, int32_t last) { + while (first < last) { + int middle = (last - first) / 2 + first; + if (nums[middle] > nums[last]) { + first = middle + 1; + } else if (nums[middle] < nums[last]) { + last = middle; + } else { + last--; + } + } + return nums[first]; +} + +TEST_CASE("test case 1", "[test binary search - rotate_array_min]") { + vector vec{3, 4, 5, 1, 2}; + CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 2", "[test binary search - rotate_array_min]") { + vector vec{2, 2, 2, 0, 1}; + CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 3", "[test binary search - rotate_array_min]") { + vector vec{1, 3, 5}; + CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 4", "[test binary search - rotate_array_min]") { + vector vec{3, 3, 1, 3}; + CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 5", "[test binary search - rotate_array_min]") { + vector vec{3, 1}; + CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 6", "[test binary search - rotate_array_min]") { + vector vec{3, 1, 3}; + CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 7", "[test binary search - rotate_array_min]") { + vector vec{5, 6, 7, 1, 2, 3, 4}; + CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 8", "[test binary search - rotate_array_min]") { + vector vec{-1, -100, 3, 99}; + CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1)); +} \ No newline at end of file diff --git a/algorithm/binary_search/triple_search.cpp b/algorithm/binary_search/triple_search.cpp new file mode 100644 index 00000000..122002f8 --- /dev/null +++ b/algorithm/binary_search/triple_search.cpp @@ -0,0 +1,138 @@ +/** + * @Github: https://github.com/Certseeds/CS203_DSAA_template + * @Organization: SUSTech + * @Author: nanoseeds + * @Date: 2020-08-01 17:33:49 + * @LastEditors: nanoseeds + * @LICENSE: MIT + */ +/* +MIT License + +CS203_DSAA_template + +Copyright (C) 2020 nanoseeds + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include +#include +#include +#include "catch_main.hpp" + +using Catch::Matchers::Contains; +using Catch::Matchers::Equals; +using Catch::Matchers::UnorderedEquals; +using std::tie; +using std::tuple; +using std::vector; + +// nums应该为一个先增后减,极大值在中心的数组 +// 此函数目的在于寻找其中间的最大值,[first,last] +int triple_search(vector &nums, int32_t first, int32_t last) { + while (first < last - 1) { + int middle = (last - first) / 2 + first; + int right_middle = (last - middle) / 2 + middle; + if (nums[middle] > nums[right_middle]) { + last = right_middle - 1; + } else if (nums[middle] < nums[right_middle]) { + first = middle + 1; + } else { + last--; + } + } + return std::max(nums[first], nums[last]); +} + +static auto func_factory = [](int w, int x, int y) { + return [w, x, y](int z) -> int { + return -1 * w * (z - x) * (z - x) + y; + }; + // 一个工厂方法,用于生产二次函数 +}; + +TEST_CASE("test case 1", "[test binary search- triple search]") { + constexpr int ratio = 1; // y = -1 * ratio * Math.pow((x-middle),2) + diff + constexpr int first = 0; // begin of array + constexpr int middle = 30; // max value's index + constexpr int last = 100; // last of array + constexpr int diff = 450; // max value of array + auto func = func_factory(ratio, middle, diff); + vector vec(last - first); + std::iota(std::begin(vec), std::end(vec), first); + std::transform(std::begin(vec), std::end(vec), std::begin(vec), func); + //std::copy(std::begin(vec), std::end(vec), std::ostream_iterator(std::cout, " ")); + //std::cout << '\n'; + // prepare data finish + //cout << triple_search(vec, 0, vec.size() - 1 ); + CHECK(*std::max_element(std::begin(vec), std::end(vec)) == triple_search(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 2", "[test binary search- triple search]") { + constexpr int ratio = 3; + constexpr int first = 0; + constexpr int middle = 70; + constexpr int last = 100; + constexpr int diff = 514; + auto func = func_factory(ratio, middle, diff); + vector vec(last - first); + std::iota(std::begin(vec), std::end(vec), first); + std::transform(std::begin(vec), std::end(vec), std::begin(vec), func); +// std::copy(std::begin(vec), std::end(vec), std::ostream_iterator(std::cout, " ")); +// std::cout << '\n'; +// prepare data finish +// cout << triple_search(vec, 0, vec.size() - 1 ); + CHECK(*std::max_element(std::begin(vec), std::end(vec)) == triple_search(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 3", "[test binary search- triple search]") { + constexpr int ratio = 200; + constexpr int first = 0; + constexpr int middle = 50; + constexpr int last = 100; + constexpr int diff = -200; + auto func = func_factory(ratio, middle, diff); + vector vec(last - first); + std::iota(std::begin(vec), std::end(vec), first); + std::transform(std::begin(vec), std::end(vec), std::begin(vec), func); +// std::copy(std::begin(vec), std::end(vec), std::ostream_iterator(std::cout, " ")); +// std::cout << '\n'; +// prepare data finish +// cout << triple_search(vec, 0, vec.size() - 1 ); + CHECK(*std::max_element(std::begin(vec), std::end(vec)) == triple_search(vec, 0, vec.size() - 1)); +} + +TEST_CASE("test case 4", "[test binary search- triple search]") { + constexpr int ratio = 3; + constexpr int first = 10; + constexpr int middle = 60; + constexpr int last = 80; + constexpr int diff = 119; + auto func = func_factory(ratio, middle, diff); + vector vec(last - first); + std::iota(std::begin(vec), std::end(vec), first); + std::transform(std::begin(vec), std::end(vec), std::begin(vec), func); + //std::copy(std::begin(vec), std::end(vec), std::ostream_iterator(std::cout, " ")); + //std::cout << '\n'; + // prepare data finish + //cout << triple_search(vec, 0, vec.size() - 1 ); + CHECK(*std::max_element(std::begin(vec), std::end(vec)) == triple_search(vec, 0, vec.size() - 1)); +} \ No newline at end of file diff --git a/algorithm/tree/CMakeLists.txt b/algorithm/tree/CMakeLists.txt new file mode 100644 index 00000000..58e9b31c --- /dev/null +++ b/algorithm/tree/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.10.2) +project(basic_algorithms_tree LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +add_executable(${PROJECT_NAME} + ./Traverse.cpp + ) \ No newline at end of file diff --git a/algorithm/tree/Traverse.cpp b/algorithm/tree/Traverse.cpp new file mode 100644 index 00000000..d7aad841 --- /dev/null +++ b/algorithm/tree/Traverse.cpp @@ -0,0 +1,262 @@ +/* + * @Github: https://github.com/Certseeds/CS203_DSAA_template + * @Organization: SUSTech + * @Author: nanoseeds + * @Date: 2020-07-30 10:30:53 + * @LastEditors: nanoseeds + * @LastEditTime: 2020-07-30 18:18:53 + */ +/* +MIT License + +CS203_DSAA_template + +Copyright (C) 2020 nanoseeds + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +#include +#include +#include +#include +#include + +#include "TreeNode.hpp" +#include "catch_main.hpp" + +using Catch::Matchers::Equals; +using std::cout; +using std::stack; +using std::vector; +using action = const std::function &; +constexpr char end = '\n'; +constexpr char space = ' '; + +//auto func1 = [](const TreeNode* tn) -> void { cout << tn->val << space; }; +void pre_rec(const TreeNode *root, action func) { + if (root == nullptr) { + return; + } + func(root); + pre_rec(root->left, func); + pre_rec(root->right, func); +} + +void pre_iter1(const TreeNode *root, action func) { + if (root == nullptr) { + return; + } + stack sta; + sta.push(root); + while (!sta.empty()) { + const TreeNode *head = sta.top(); + sta.pop(); + func(head); + if (head->right != nullptr) { + sta.push(head->right); + } + if (head->left != nullptr) { + sta.push(head->left); + } + } +} + +void pre_iter2(TreeNode *root, action func) { + if (root == nullptr) { + return; + } + stack sta; + TreeNode *head = root; + while (head != nullptr || !sta.empty()) { + while (head != nullptr) { + func(head); + sta.push(head->right); + head = head->left; + } + head = sta.top(); + sta.pop(); + } +} + +void pre_iter3(TreeNode *root, action func) { + //作者:sonp + //链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/solution/mo-fang-di-gui-zhi-bian-yi-xing-by-sonp/ + if (root == nullptr) { + return; + } + stack sta; + sta.push(root); + while (!sta.empty()) { + TreeNode *head = sta.top(); + sta.pop(); + if (head != nullptr) { + if (head->right != nullptr) { + sta.push(head->right); + } + if (head->left != nullptr) { + sta.push(head->left); + } + sta.push(head); + sta.push(nullptr); + } else { + func(sta.top()); + sta.pop(); + } + } +} + +void in_rec(const TreeNode *root, action func) { + if (root == nullptr) { + return; + } + in_rec(root->left, func); + func(root); + in_rec(root->right, func); +} + +void in_iter(TreeNode *root, action func) { + if (root == nullptr) { + return; + } + stack sta; + TreeNode *head = root; + while (head != nullptr || !sta.empty()) { + while (head != nullptr) { + sta.push(head); + head = head->left; + } + head = sta.top(); + sta.pop(); + func(head); + head = head->right; + } +} + +void in_iter2(TreeNode *root, action func) { + //作者:sonp + //链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/solution/mo-fang-di-gui-zhi-bian-yi-xing-by-sonp/ + if (root == nullptr) { + return; + } + stack sta; + sta.push(root); + while (!sta.empty()) { + TreeNode *head = sta.top(); + sta.pop(); + if (head != nullptr) { + if (head->right != nullptr) { + sta.push(head->right); + } + sta.push(head); + sta.push(nullptr); + if (head->left != nullptr) { + sta.push(head->left); + } + } else { + func(sta.top()); + sta.pop(); + } + } +} + +void post_rec(const TreeNode *root, action func) { + if (root == nullptr) { + return; + } + post_rec(root->left, func); + post_rec(root->right, func); + func(root); +} + +void post_iter(TreeNode *root, action func) { + //作者:sonp + //链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/solution/mo-fang-di-gui-zhi-bian-yi-xing-by-sonp/ + if (root == nullptr) { + return; + } + stack sta; + sta.push(root); + while (!sta.empty()) { + TreeNode *head = sta.top(); + sta.pop(); + if (head != nullptr) { + sta.push(head); + sta.push(nullptr); + if (head->right != nullptr) { + sta.push(head->right); + } + if (head->left != nullptr) { + sta.push(head->left); + } + } else { + func(sta.top()); + sta.pop(); + } + } +} + +TEST_CASE("traverse basic", "[tree traverse]") { + const vector name{"", "pre order", "in order", "post order"}; + constexpr int32_t tree_nodes = 32; + static int32_t count = 0; + vector vec(tree_nodes); + std::iota(vec.begin(), vec.end(), 0); + vector numvec = TreeNode::numToTree(vec); + TreeNode *head = numvec[0]; + static vector last(tree_nodes); + vector res; + auto func2 = [&res](const TreeNode *tn) -> void { res.push_back(tn->val); }; + SECTION("pre_rec") { + count++; + pre_rec(head, func2); + }SECTION("pre_iter1") { + pre_iter1(head, func2); + CHECK_THAT(res, Equals(last)); + }SECTION("pre_iter2") { + pre_iter2(head, func2); + CHECK_THAT(res, Equals(last)); + }SECTION("pre_iter3") { + pre_iter3(head, func2); + CHECK_THAT(res, Equals(last)); + } + INFO("pre order finish"); + SECTION("in_rec") { + count++; + in_rec(head, func2); + }SECTION("in_iter") { + in_iter(head, func2); + CHECK_THAT(res, Equals(last)); + }SECTION("in_iter2") { + in_iter2(head, func2); + CHECK_THAT(res, Equals(last)); + } + INFO("in order finish"); + SECTION("post_iter") { + count++; + post_iter(head, func2); + }SECTION("post_rec") { + post_rec(head, func2); + CHECK_THAT(res, Equals(last)); + } + std::copy(std::begin(res), std::end(res), std::begin(last)); + std::copy(std::begin(res), std::end(res), std::ostream_iterator{std::cout, " "}); + std::for_each(std::begin(numvec), std::end(numvec), [](TreeNode *node) { delete node; }); + cout << name[count] << end; + res.clear(); +} \ No newline at end of file diff --git a/includes/CS203_redirect.hpp b/includes/CS203_redirect.hpp index ab060cd0..9d2ea25c 100644 --- a/includes/CS203_redirect.hpp +++ b/includes/CS203_redirect.hpp @@ -48,13 +48,18 @@ class CS203_redirect { std::ifstream file_in = std::ifstream(); std::ofstream file_out = std::ofstream(); public: + static string file_paths; + // default path1 is input and path2 is output - explicit CS203_redirect(const string &path1, const string &path2 = "") { + explicit CS203_redirect(string path1, string path2 = "") { + string old_path2 = path2; + path1 = file_paths + path1; + path2 = file_paths + path2; this->strmin_buf = std::cin.rdbuf(); this->strmout_buf = std::cout.rdbuf(); this->file_in.open(path1); std::cin.rdbuf(file_in.rdbuf()); - if (!path2.empty()) { + if (!old_path2.empty()) { this->file_out.open(path2); std::cout.rdbuf(file_out.rdbuf()); } @@ -73,7 +78,15 @@ class CS203_redirect { std::cin.rdbuf(strmin_buf); std::cout.flush(); } - }; +#ifndef CS203_DSAA_TEST_MACRO +#define CS203_DSAA_TEST_MACRO +/* if in this macro, it means it do not belong to ./test/lab_${number}/${x}_test.cpp + so, it need define file_paths although it maybe do not need that + to avoid file_paths do not be define before use. +*/ +string CS203_redirect::file_paths = ""; +#endif //CS203_DSAA_TEST_MACRO + #endif //CS203_DSAA_TEMPLATE_INCLUDES_CS203_REDIRECT_H diff --git a/includes/TreeNode.hpp b/includes/TreeNode.hpp new file mode 100644 index 00000000..385b8ebc --- /dev/null +++ b/includes/TreeNode.hpp @@ -0,0 +1,63 @@ +/* + * @Github: https://github.com/Certseeds/CS203_DSAA_template + * @Organization: SUSTech + * @Author: nanoseeds + * @Date: 2020-07-30 10:34:16 + * @LastEditors: nanoseeds + * @LastEditTime: 2020-07-30 11:01:19 + */ +#ifndef CS203_DSAA_TEMPLATE_INCLUDES_TREENODE_H +#define CS203_DSAA_TEMPLATE_INCLUDES_TREENODE_H + +#include + +using std::vector; +constexpr int No = -100000; + +class TreeNode { + public: + int32_t val; + TreeNode *left; + TreeNode *right; + + explicit TreeNode(int x = 0) : val(x), left(nullptr), right(nullptr){}; + + TreeNode(int x, TreeNode *le, TreeNode *rig) : val(x), left(le), right(rig){}; + + TreeNode(const TreeNode &obj) { + this->val = obj.val; + this->left = obj.left; + this->right = obj.right; + } + TreeNode &operator=(const TreeNode &timer) = delete; + + TreeNode(TreeNode &&timer) = delete; + + TreeNode &operator=(TreeNode &&mat) = delete; + + ~TreeNode() { + this->val = 0; + this->left = nullptr; + this->right = nullptr; + } + + static vector numToTree(const vector &nums) { + vector will_return(nums.size(), nullptr); + for (int i = 0; i < static_cast(nums.size()); i++) { + will_return[i] = (nums[i] != No) ? new TreeNode(nums[i]) : nullptr; + } + TreeNode::organize(will_return); + return will_return; + } + static void organize(vector Tree) { + int32_t tree_size = Tree.size(); + for (int i = 0; i < tree_size; i++) { + if (Tree[i] != nullptr) { + Tree[i]->left = (2 * i + 1 < tree_size) ? Tree[2 * i + 1] : nullptr; + Tree[i]->right = (2 * i + 2 < tree_size) ? Tree[2 * i + 2] : nullptr; + } + } + } +}; + +#endif //CS203_DSAA_TEMPLATE_INCLUDES_TREENODE_H \ No newline at end of file diff --git a/includes/public.hpp b/includes/public.hpp index 007acbd0..44e3ed8e 100644 --- a/includes/public.hpp +++ b/includes/public.hpp @@ -4,7 +4,7 @@ * @Author: nanoseeds * @Date: 2020-07-15 21:44:06 * @LastEditors: nanoseeds - * @LastEditTime: 2020-07-23 23:40:10 + * @LastEditTime: 2020-07-30 11:27:06 * @LICENSE: MIT */ /* @@ -50,7 +50,6 @@ using std::ios; using std::cin; using std::cout; CS203_timer timer{}; - static int faster_streams = []() { srand(time(0)); // use time to init the random seed @@ -63,15 +62,18 @@ static int faster_streams = []() { return 0; }(); -bool compareFiles(const std::string &p1, const std::string &p2) { +bool compareFiles(std::string p1, std::string p2) { + p1 = CS203_redirect::file_paths + p1; + p2 = CS203_redirect::file_paths + p2; // get from https://stackoverflow.com/questions/6163611/compare-two-files std::ifstream f1(p1, std::ifstream::binary | std::ifstream::ate); std::ifstream f2(p2, std::ifstream::binary | std::ifstream::ate); if (f1.fail() || f2.fail()) { return false; //file problem } - std::cout << f1.tellg() << " " << f2.tellg() << " \n"; if (f1.tellg() != f2.tellg()) { + std::cout << p1 << " not match " << p2 << '\n'; + std::cout << f1.tellg() << " " << f2.tellg() << '\n'; return false; //size mismatch } //seek back to beginning and use std::equal to compare contents diff --git a/script/cpp_template.txt b/script/cpp_template.txt index f6c978bf..c6dbc1ee 100644 --- a/script/cpp_template.txt +++ b/script/cpp_template.txt @@ -30,42 +30,42 @@ using std::unordered_set; using std::priority_queue; const std::string end = "\n"; +using input_type = tuple; +using output_type = int32_t; -using input_data = tuple; -using result_data = int32_t; +inline input_type read(); -inline input_data read(); +output_type cal(input_type data); -result_data cal(input_data &&data); - -void output(const result_data &data); +void output(const output_type &data); #ifndef CS203_DSAA_TEST_MACRO #define CS203_DSAA_TEST_MACRO -int main() {{ - auto result_data = cal(read()); - output(result_data); +int main() { + auto input_data = read(); + auto output_data = cal(input_data); + output(output_data); return 0; -}} +} #endif // !CS203_DSAA_TEST_MACRO -inline input_data read() {{ - int32_t a{{0}}; - int32_t b{{0}}; +inline input_type read() { + int32_t a{0}; + int32_t b{0}; std::cin >> a >> b; return std::make_tuple(a, b); -}} +} -result_data cal(input_data &&data) {{ - int32_t a{{0}}; - int32_t b{{0}}; +output_type cal(input_type data) { + int32_t a{0}; + int32_t b{0}; tie(a, b) = data; int32_t c = a + b; return c; -}} +} -void output(const result_data &data) {{ +void output(const output_type &data) { std::cout << data << end; -}} \ No newline at end of file +} \ No newline at end of file diff --git a/script/cpp_test_template.txt b/script/cpp_test_template.txt index 4e251d98..45557ef7 100644 --- a/script/cpp_test_template.txt +++ b/script/cpp_test_template.txt @@ -16,11 +16,11 @@ using std::vector; using Catch::Matchers::Equals; using Catch::Matchers::UnorderedEquals; using Catch::Matchers::Contains; -const string test_file_path = "./../test/lab_{0}/lab_{0}_{1}_data/"; +string CS203_redirect::file_paths = "./../test/lab_{0}/lab_{0}_{1}_data/"; TEST_CASE("test case 1", "[test {0} {1}]") {{ - auto output = cal(std::make_tuple(114, 514)); - CHECK(output == 628); + auto output_data = cal(std::make_tuple(114, 514)); + CHECK(output_data == 628); CHECK(1 + 2 == 3); vector vec{{2, 7, 11, 15}}; SECTION("CHECK_THAT 1") {{ diff --git a/script/file_template.py b/script/file_template.py index 2a48fd57..8ade3560 100644 --- a/script/file_template.py +++ b/script/file_template.py @@ -6,7 +6,7 @@ @Author: nanoseeds @Date: 2020-07-15 21:47:09 @LastEditors: nanoseeds -@LastEditTime: 2020-07-27 15:32:22 +@LastEditTime: 2020-07-31 22:51:49 @LICENSE: MIT ''' ''' @@ -85,7 +85,9 @@ def try_mkdir(lab_number: str) -> None: test_code_template = read_file('cpp_test_template.txt') labs: List[str] = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15'] - problem_order: List[chr] = ['A', 'B', 'C','D', 'E', 'F', 'G', 'H', 'I', 'J'] + problem_order: List[str] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] + # labs: List[str] = ['01'] + # problem_order: List[str] = ['A'] for i in labs: try_mkdir(i) # 准备文件夹 for j in problem_order: diff --git a/script/test.sh b/script/test.sh new file mode 100755 index 00000000..698b3b4f --- /dev/null +++ b/script/test.sh @@ -0,0 +1,48 @@ +#!/bin/bash +set -eoux pipefail +### +# @Github: https://github.com/Certseeds/CS203_DSAA_template +# @Organization: SUSTech +# @Author: nanoseeds +# @Date: 2020-07-27 21:48:41 +# @LastEditors: nanoseeds +# @LastEditTime: 2020-07-30 18:19:45 +### +function cmake_make() { + mkdir ./cmake_build_path + cd ./cmake_build_path + cmake .. + make + # now at ./../cmake_build_path +} +function main() { + prefix="basic_algorithms" + ./CS203_DSAA_template_test + cd ./algorithm + { + cd ./sort + ./${prefix}_sort + cd .. + } + { + cd ./binary_search + ./${prefix}_binary_search + cd .. + } + { + cd ./tree + ./${prefix}_tree + cd .. + } + cd .. + # now at ./../cmake_build_path +} +function finish() { + rm -rf "${scratch}" +} +# now in ./ +cd .. +scratch="$(pwd)"/cmake_build_path +trap finish EXIT +cmake_make +main diff --git a/source/lab_00/lab_00_A.cpp b/source/lab_00/lab_00_A.cpp index d1292b9c..39ad6916 100644 --- a/source/lab_00/lab_00_A.cpp +++ b/source/lab_00/lab_00_A.cpp @@ -42,34 +42,35 @@ using std::tuple; using std::vector; const std::string end = "\n"; -using input_data = tuple; -using result_data = int32_t; +using input_type = tuple; +using output_type = int32_t; -inline input_data read(); +inline input_type read(); -result_data cal(input_data &&data); +output_type cal(input_type data); -void output(const result_data &data); +void output(const output_type &data); #ifndef CS203_DSAA_TEST_MACRO #define CS203_DSAA_TEST_MACRO int main() { - auto result_data = cal(read()); - output(result_data); + auto input_data = read(); + auto output_data = cal(input_data); + output(output_data); return 0; } #endif // !CS203_DSAA_TEST_MACRO -inline input_data read() { +inline input_type read() { int32_t a{0}; int32_t b{0}; std::cin >> a >> b; return std::make_tuple(a, b); } -result_data cal(input_data &&data) { +output_type cal(input_type data) { int32_t a{0}; int32_t b{0}; tie(a, b) = data; @@ -77,6 +78,6 @@ result_data cal(input_data &&data) { return c; } -void output(const result_data &data) { +void output(const output_type &data) { std::cout << data << end; } \ No newline at end of file diff --git a/source/lab_00/lab_00_B.cpp b/source/lab_00/lab_00_B.cpp index 4f1ed3a3..84de8ae8 100644 --- a/source/lab_00/lab_00_B.cpp +++ b/source/lab_00/lab_00_B.cpp @@ -45,43 +45,43 @@ using std::vector; const std::string end = "\n"; using num_type = uint64_t; -using input_format = vector; -using output_format = vector; +using input_type = vector; +using output_type = vector; -inline input_format read(); +inline input_type read(); -output_format cal_warpper(const input_format &input); +output_type cal_warpper(const input_type &input); num_type cal(num_type data); num_type brute_force(num_type data); -void output(const output_format &data); +void output(const output_type &data); #ifndef CS203_DSAA_TEST_MACRO #define CS203_DSAA_TEST_MACRO int main() { - input_format input_data = read(); - output_format result_data = cal_warpper(input_data); - output(result_data); + input_type input_data = read(); + output_type output_data = cal_warpper(input_data); + output(output_data); return 0; } #endif // !CS203_DSAA_TEST_MACRO -inline input_format read() { +inline input_type read() { int32_t number{0}; std::cin >> number; - input_format will_return(number, 0); + input_type will_return(number, 0); for (auto &item : will_return) { std::cin >> item; } return will_return; } -output_format cal_warpper(const input_format &input) { - output_format will_return(input.size(), 0); +output_type cal_warpper(const input_type &input) { + output_type will_return(input.size(), 0); for (size_t i = 0; i < input.size(); ++i) { will_return[i] = cal(input[i]); } diff --git a/source/lab_00/lab_00_C.cpp b/source/lab_00/lab_00_C.cpp index 22cfc36a..2ac2daef 100644 --- a/source/lab_00/lab_00_C.cpp +++ b/source/lab_00/lab_00_C.cpp @@ -77,27 +77,28 @@ enum class Color { black = 1 }; -using input_data = vector>; -using result_data = bool; +using input_type = vector>; +using output_type = bool; -inline input_data read(); +inline input_type read(); -result_data isBipartite(const input_data &&data); +output_type isBipartite(const input_type &data); -void output(const result_data &data); +void output(const output_type &data); #ifndef CS203_DSAA_TEST_MACRO #define CS203_DSAA_TEST_MACRO int main() { - auto result_data = isBipartite(read()); - output(result_data); + auto input_data = read(); + auto output_data = isBipartite(input_data); + output(output_data); return 0; } #endif // !CS203_DSAA_TEST_MACRO -inline input_data read() { +inline input_type read() { int32_t N{0}; std::cin >> N; vector> will_return(N); @@ -112,7 +113,7 @@ inline input_data read() { return will_return; } -result_data isBipartite(const input_data &&graph) { +output_type isBipartite(const input_type &graph) { int32_t node_number = graph.size(); vector color_vec(node_number, Color::uncolor); for (int32_t i = 0; i < node_number; i++) { @@ -138,7 +139,7 @@ result_data isBipartite(const input_data &&graph) { return true; } -void output(const result_data &data) { +void output(const output_type &data) { if (data) { std::cout << "\"PÖSSiBLE\"" << end; } else { diff --git a/source/lab_00/lab_00_D.cpp b/source/lab_00/lab_00_D.cpp index dac66882..346ded57 100644 --- a/source/lab_00/lab_00_D.cpp +++ b/source/lab_00/lab_00_D.cpp @@ -53,35 +53,35 @@ const vector> spis{ {0, "| |/"}, {0, "+---+"}}; -using input_data = vector>; -using result_data = vector>; +using input_type = vector>; +using output_type = vector>; -inline input_data read(); +inline input_type read(); -//result_data cal(input_data &&data); -void cal(input_data &&hi); +void cal(input_type &hi); -void output(const result_data &map, int high, int wide); +void output(const output_type &map, int high, int wide); -void print(result_data &map, int a, int b); +void print(output_type &map, int a, int b); #ifndef CS203_DSAA_TEST_MACRO #define CS203_DSAA_TEST_MACRO int main() { - cal(read()); - // auto result_data = cal(read()); - // output(result_data); + auto input_data = read(); + cal(input_data); + // auto output_type = cal(read()); + // output(output_type); return 0; } #endif // !CS203_DSAA_TEST_MACRO -inline input_data read() { +inline input_type read() { int32_t m{0}; int32_t n{0}; std::cin >> m >> n; - input_data mat(m + 1, vector(n + 1, -0x3f3f)); + input_type mat(m + 1, vector(n + 1, -0x3f3f)); for (int k = 1; k <= m; ++k) { for (int i = 1; i <= n; ++i) { std::cin >> mat[k][i]; @@ -90,7 +90,7 @@ inline input_data read() { return mat; } -void print(result_data &map, int a, int b) { +void print(output_type &map, int a, int b) { for (int i = 0; i < static_cast(spis.size()); i++) { for (int j = 0; j < static_cast(spis[i].second.size()); j++) { map[a - 1 + i][b + j + spis[i].first] = spis[i].second[j]; @@ -99,7 +99,7 @@ void print(result_data &map, int a, int b) { return; } -void cal(input_data &&hi) { +void cal(input_type &hi) { vector> out(302, vector(301, '.')); int m = static_cast(hi.size()) - 1; int n = static_cast(hi.front().size()) - 1; @@ -122,7 +122,7 @@ void cal(input_data &&hi) { output(out, h, wide); } -void output(const result_data &map, int high, int wide) { +void output(const output_type &map, int high, int wide) { for (int i = 0; i < high; i++) { for (int j = 1; j <= wide; j++) { cout << map[i][j]; diff --git a/test/lab_00/lab_00_A_test.cpp b/test/lab_00/lab_00_A_test.cpp index 9e50a3cc..80a952cc 100644 --- a/test/lab_00/lab_00_A_test.cpp +++ b/test/lab_00/lab_00_A_test.cpp @@ -1,11 +1,3 @@ -/* - * @Github: https://github.com/Certseeds/CS203_DSAA_template - * @Organization: SUSTech - * @Author: nanoseeds - * @Date: 2020-07-15 22:13:18 - * @LastEditors: nanoseeds - * @LastEditTime: 2020-07-17 23:26:32 - */ /** * @Github: https://github.com/Certseeds/CS203_DSAA_template * @Organization: SUSTech @@ -54,6 +46,7 @@ using std::cout; using std::tie; using std::tuple; using std::vector; +string CS203_redirect::file_paths = "./../test/lab_00/lab_00_A_data/"; TEST_CASE("test case 1", "[test 00 A]") { auto output1 = cal(std::make_tuple(0, 0)); diff --git a/test/lab_00/lab_00_B_test.cpp b/test/lab_00/lab_00_B_test.cpp index 02ffdd2f..d125bd30 100644 --- a/test/lab_00/lab_00_B_test.cpp +++ b/test/lab_00/lab_00_B_test.cpp @@ -49,19 +49,20 @@ using std::vector; using Catch::Matchers::Equals; using Catch::Matchers::UnorderedEquals; using Catch::Matchers::Contains; +string CS203_redirect::file_paths = "./../test/lab_00/lab_00_B_data/"; TEST_CASE("test case 1", "[test 00 B]") { - auto vec1 = vector{8, 1, 2, 3, 4, 5, 6, 7, 1100000}; + auto vec1 = vector{8, 1, 2, 3, 4, 5, 6, 7, 1100000}; for (const auto &i:vec1) { CHECK(cal(i) == brute_force(i)); } } TEST_CASE("test case 2", "[test 00 B]") { - auto vec1 = vector{1, 2, 3, 4, 5, 6, 7, 1100000}; + auto vec1 = vector{1, 2, 3, 4, 5, 6, 7, 1100000}; auto result = cal_warpper(vec1); - auto what_we_want = vector{1, 4, 10, 20, 35, 56, 84, 221833938333700000}; - CHECK_THAT(result, Equals({1, 4, 10, 20, 35, 56, 84, 221833938333700000})); + auto what_we_want = vector{1, 4, 10, 20, 35, 56, 84, 221833938333700000}; + CHECK_THAT(result, Equals({1, 4, 10, 20, 35, 56, 84, 221833938333700000})); CHECK_THAT(result, Equals(what_we_want)); } diff --git a/test/lab_00/lab_00_C_test.cpp b/test/lab_00/lab_00_C_test.cpp index 60a8963e..8751026a 100644 --- a/test/lab_00/lab_00_C_test.cpp +++ b/test/lab_00/lab_00_C_test.cpp @@ -46,70 +46,69 @@ using std::vector; using Catch::Matchers::Equals; using Catch::Matchers::UnorderedEquals; using Catch::Matchers::Contains; -const string test_file_path = "./../test/lab_00/lab_00_C_data/"; +string CS203_redirect::file_paths = "./../test/lab_00/lab_00_C_data/"; TEST_CASE("test case 1", "[test 00 C]") { - CS203_redirect cr{test_file_path + "01.data.in", ""}; + CS203_redirect cr{"01.data.in", ""}; // 重定向开始,开始run - auto result_data = isBipartite(read()); + auto output_data = isBipartite(read()); // 重定向结束 - CHECK_FALSE(result_data); + CHECK_FALSE(output_data); } TEST_CASE("test case 2", "[test 00 C]") { - CS203_redirect cr{test_file_path + "02.data.in"}; - auto result_data = isBipartite(read()); - CHECK(result_data); + CS203_redirect cr{"02.data.in"}; + auto output_data = isBipartite(read()); + CHECK(output_data); } TEST_CASE("test case 3", "[test 00 C]") { - CS203_redirect cr{test_file_path + "03.data.in"}; - auto result_data = isBipartite(read()); - CHECK(result_data); + CS203_redirect cr{"03.data.in"}; + auto output_data = isBipartite(read()); + CHECK(output_data); } TEST_CASE("test case 4", "[test 00 C]") { - CS203_redirect cr{test_file_path + "04.data.in"}; - auto result_data = isBipartite(read()); - CHECK_FALSE(result_data); + CS203_redirect cr{"04.data.in"}; + auto output_data = isBipartite(read()); + CHECK_FALSE(output_data); } + TEST_CASE("test case 5", "[test 00 C]") { - CS203_redirect cr{test_file_path + "05.data.in"}; - auto result_data = isBipartite(read()); - CHECK_FALSE(result_data); + CS203_redirect cr{"05.data.in"}; + auto output_data = isBipartite(read()); + CHECK_FALSE(output_data); } TEST_CASE("test case in loop", "[test 00 C]") { const vector strs{ - test_file_path + "01.data.in", - test_file_path + "02.data.in", - test_file_path + "03.data.in", - test_file_path + "04.data.in", - test_file_path + "05.data.in" + "01.data.in", "02.data.in", + "03.data.in", "04.data.in", + "05.data.in" }; const vector result{false, true, true, false, false}; for (int i = 0; i < 5; ++i) { CS203_redirect cr{strs[i]}; - auto result_data = isBipartite(read()); - CHECK(result_data == result[i]); + auto output_data = isBipartite(read()); + CHECK(output_data == result[i]); } } TEST_CASE("test case with tuple", "[test 00 C]") { - const vector> input_result{ - {test_file_path + "01.data.in", false}, - {test_file_path + "02.data.in", true}, - {test_file_path + "03.data.in", true}, - {test_file_path + "04.data.in", false}, - {test_file_path + "05.data.in", false} + const vector> input_result{ + {"01.data.in", false}, + {"02.data.in", true}, + {"03.data.in", true}, + {"04.data.in", false}, + {"05.data.in", false} }; for (const auto &tup : input_result) { string path; - result_data result; + output_type result; tie(path, result) = tup; CS203_redirect cr{path}; - auto result_data = isBipartite(read()); - CHECK(result_data == result); + auto output_data = isBipartite(read()); + CHECK(output_data == result); } } diff --git a/test/lab_00/lab_00_D_test.cpp b/test/lab_00/lab_00_D_test.cpp index 602617e8..40136201 100644 --- a/test/lab_00/lab_00_D_test.cpp +++ b/test/lab_00/lab_00_D_test.cpp @@ -48,21 +48,23 @@ using std::vector; using Catch::Matchers::Equals; using Catch::Matchers::UnorderedEquals; using Catch::Matchers::Contains; -const string test_file_path = "./../test/lab_00/lab_00_D_data/"; +string CS203_redirect::file_paths = "./../test/lab_00/lab_00_D_data/"; TEST_CASE("test case 1", "[test 00 D]") { - CS203_redirect cr{test_file_path + "01.data.in"}; + CS203_redirect cr{"01.data.in"}; // 重定向开始,开始run - cal(read()); + auto input_data = read(); + cal(input_data); // 重定向结束 } -TEST_CASE("test case 2", "[test 00 C]") { +TEST_CASE("test case 2", "[test 00 D]") { SECTION("do") { - CS203_redirect cr{test_file_path + "01.data.in", test_file_path + "01.test.out"}; - cal(read()); + CS203_redirect cr{"01.data.in", "01.test.out"}; + auto input_data = read(); + cal(input_data); }SECTION("compare files") { - CHECK(compareFiles(test_file_path + "01.test.out", test_file_path + "01.data.out")); + CHECK(compareFiles("01.test.out", "01.data.out")); } }