Skip to content

Commit 996b556

Browse files
Merge branch 'master' into bug_in_ncr_modulo_p_patch
2 parents 01791bc + 813175a commit 996b556

File tree

6 files changed

+195
-88
lines changed

6 files changed

+195
-88
lines changed

.github/pull_request_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ Contributors guide: https://github.com/TheAlgorithms/C-Plus-Plus/CONTRIBUTING.md
1818
- [ ] Search previous suggestions before making a new one, as yours may be a duplicate.
1919
- [ ] I acknowledge that all my contributions will be made under the project's license.
2020

21-
Notes: <!-- Please add a one-line description for developers or pull request viewers -->
21+
Notes: <!-- Please add a one-line description for developers or pull request viewers -->

.github/workflows/awesome_workflow.yml

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,8 @@ jobs:
3838
# be able to catch any errors for other platforms.
3939
run: cmake -B build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
4040
- name: Lint modified files
41-
shell: python
42-
run: |
43-
import os
44-
import subprocess
45-
import sys
46-
47-
print("Python {}.{}.{}".format(*sys.version_info)) # Python 3.8
48-
with open("git_diff.txt") as in_file:
49-
modified_files = sorted(in_file.read().splitlines())
50-
print("{} files were modified.".format(len(modified_files)))
51-
52-
cpp_exts = tuple(".c .c++ .cc .cpp .cu .cuh .cxx .h .h++ .hh .hpp .hxx".split())
53-
cpp_files = [file for file in modified_files if file.lower().endswith(cpp_exts)]
54-
print(f"{len(cpp_files)} C++ files were modified.")
55-
if not cpp_files:
56-
sys.exit(0)
57-
58-
subprocess.run(["clang-tidy", "--fix", "-p=build", "--extra-arg=-std=c++11", *cpp_files, "--"],
59-
check=True, text=True, stderr=subprocess.STDOUT)
60-
61-
subprocess.run(["clang-format", "-i", "-style=file", *cpp_files],
62-
check=True, text=True, stderr=subprocess.STDOUT)
63-
64-
upper_files = [file for file in cpp_files if file != file.lower()]
65-
if upper_files:
66-
print(f"{len(upper_files)} files contain uppercase characters:")
67-
print("\n".join(upper_files) + "\n")
68-
69-
space_files = [file for file in cpp_files if " " in file or "-" in file]
70-
if space_files:
71-
print(f"{len(space_files)} files contain space or dash characters:")
72-
print("\n".join(space_files) + "\n")
73-
74-
nodir_files = [file for file in cpp_files if file.count(os.sep) != 1]
75-
if nodir_files:
76-
print(f"{len(nodir_files)} files are not in one and only one directory:")
77-
print("\n".join(nodir_files) + "\n")
78-
79-
bad_files = len(upper_files + space_files + nodir_files)
80-
if bad_files:
81-
sys.exit(bad_files)
41+
shell: bash
42+
run: python3 scripts/file_linter.py
8243
- name: Commit and push changes
8344
run: |
8445
git diff DIRECTORY.md

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ a.out
3535
*.app
3636

3737
build/
38+
git_diff.txt
Lines changed: 150 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,159 @@
1-
#include <iosrteam>
2-
using namespace std;
1+
/**
2+
* @file
3+
* @brief contains the definition of the function ::longest_common_string_length
4+
* @details
5+
* the function ::longest_common_string_length computes the length
6+
* of the longest common string which can be created of two input strings
7+
* by removing characters from them
8+
*
9+
* @author [Nikhil Arora](https://github.com/nikhilarora068)
10+
* @author [Piotr Idzik](https://github.com/vil02)
11+
*/
312

4-
int max(int a, int b) { return (a > b) ? a : b; }
13+
#include <cassert> /// for assert
14+
#include <iostream> /// for std::cout
15+
#include <string> /// for std::string
16+
#include <utility> /// for std::move
17+
#include <vector> /// for std::vector
518

6-
int main() {
7-
char str1[] = "DEFBCD";
8-
char str2[] = "ABDEFJ";
9-
int i, j, k;
10-
int n = strlen(str1) + 1;
11-
int m = strlen(str2) + 1;
12-
// cout<<n<<" "<<m<<"\n";
13-
int a[m][n];
14-
15-
for (i = 0; i < m; i++) {
16-
for (j = 0; j < n; j++) {
17-
if (i == 0 || j == 0)
18-
a[i][j] = 0;
19-
20-
else if (str1[i - 1] == str2[j - 1])
21-
a[i][j] = a[i - 1][j - 1] + 1;
22-
23-
else
24-
a[i][j] = 0;
25-
}
26-
}
19+
/**
20+
* @brief computes the length of the longest common string created from input
21+
* strings
22+
* @details has O(str_a.size()*str_b.size()) time and memory complexity
23+
* @param string_a first input string
24+
* @param string_b second input string
25+
* @returns the length of the longest common string which can be strated from
26+
* str_a and str_b
27+
*/
28+
std::size_t longest_common_string_length(const std::string& string_a,
29+
const std::string& string_b) {
30+
const auto size_a = string_a.size();
31+
const auto size_b = string_b.size();
32+
std::vector<std::vector<std::size_t>> sub_sols(
33+
size_a + 1, std::vector<std::size_t>(size_b + 1, 0));
2734

28-
/*for(i=0;i<m;i++)
29-
{
30-
for(j=0;j<n;j++)
31-
cout<<a[i][j]<<" ";
32-
cout<<"\n";
33-
}*/
34-
35-
int ma = -1;
36-
int indi, indj;
37-
for (i = 0; i < m; i++) {
38-
for (j = 0; j < n; j++) {
39-
if (a[i][j] > ma) {
40-
ma = a[i][j];
41-
indi = i;
42-
indj = j;
35+
const auto limit = static_cast<std::size_t>(-1);
36+
for (std::size_t pos_a = size_a - 1; pos_a != limit; --pos_a) {
37+
for (std::size_t pos_b = size_b - 1; pos_b != limit; --pos_b) {
38+
if (string_a[pos_a] == string_b[pos_b]) {
39+
sub_sols[pos_a][pos_b] = 1 + sub_sols[pos_a + 1][pos_b + 1];
40+
} else {
41+
sub_sols[pos_a][pos_b] = std::max(sub_sols[pos_a + 1][pos_b],
42+
sub_sols[pos_a][pos_b + 1]);
4343
}
4444
}
4545
}
4646

47-
cout << str1 << "\n";
48-
cout << str2 << "\n";
47+
return sub_sols[0][0];
48+
}
49+
50+
/**
51+
* @brief represents single example inputs and expected output of the function
52+
* ::longest_common_string_length
53+
*/
54+
struct TestCase {
55+
const std::string string_a;
56+
const std::string string_b;
57+
const std::size_t common_string_len;
58+
59+
TestCase(std::string string_a, std::string string_b,
60+
const std::size_t in_common_string_len)
61+
: string_a(std::move(string_a)),
62+
string_b(std::move(string_b)),
63+
common_string_len(in_common_string_len) {}
64+
};
4965

50-
cout << "longest string size = " << ma /*<<" "<<indi<<" "<<indj*/ << "\n";
51-
for (i = indi - 3; i < indi; i++) cout << str1[i];
52-
cout << "\n";
66+
/**
67+
* @return example data used in the tests of ::longest_common_string_length
68+
*/
69+
std::vector<TestCase> get_test_cases() {
70+
return {TestCase("", "", 0),
71+
TestCase("ab", "ab", 2),
72+
TestCase("ab", "ba", 1),
73+
TestCase("", "xyz", 0),
74+
TestCase("abcde", "ace", 3),
75+
TestCase("BADANA", "ANADA", 3),
76+
TestCase("BADANA", "CANADAS", 3),
77+
TestCase("a1a234a5aaaa6", "A1AAAA234AAA56AAAAA", 6),
78+
TestCase("123x", "123", 3),
79+
TestCase("12x3x", "123", 3),
80+
TestCase("1x2x3x", "123", 3),
81+
TestCase("x1x2x3x", "123", 3),
82+
TestCase("x12x3x", "123", 3)};
83+
}
84+
85+
/**
86+
* @brief checks the function ::longest_common_string_length agains example data
87+
* @param test_cases list of test cases
88+
* @tparam type representing a list of test cases
89+
*/
90+
template <typename TestCases>
91+
static void test_longest_common_string_length(const TestCases& test_cases) {
92+
for (const auto& cur_tc : test_cases) {
93+
assert(longest_common_string_length(cur_tc.string_a, cur_tc.string_b) ==
94+
cur_tc.common_string_len);
95+
}
96+
}
97+
98+
/**
99+
* @brief checks if the function ::longest_common_string_length returns the same
100+
* result when its argument are flipped
101+
* @param test_cases list of test cases
102+
* @tparam type representing a list of test cases
103+
*/
104+
template <typename TestCases>
105+
static void test_longest_common_string_length_is_symmetric(
106+
const TestCases& test_cases) {
107+
for (const auto& cur_tc : test_cases) {
108+
assert(longest_common_string_length(cur_tc.string_b, cur_tc.string_a) ==
109+
cur_tc.common_string_len);
110+
}
111+
}
112+
113+
/**
114+
* @brief reverses a given string
115+
* @param in_str input string
116+
* @return the string in which the characters appear in the reversed order as in
117+
* in_str
118+
*/
119+
std::string reverse_str(const std::string& in_str) {
120+
return {in_str.rbegin(), in_str.rend()};
121+
}
122+
123+
/**
124+
* @brief checks if the function ::longest_common_string_length returns the same
125+
* result when its inputs are reversed
126+
* @param test_cases list of test cases
127+
* @tparam type representing a list of test cases
128+
*/
129+
template <typename TestCases>
130+
static void test_longest_common_string_length_for_reversed_inputs(
131+
const TestCases& test_cases) {
132+
for (const auto& cur_tc : test_cases) {
133+
assert(longest_common_string_length(reverse_str(cur_tc.string_a),
134+
reverse_str(cur_tc.string_b)) ==
135+
cur_tc.common_string_len);
136+
}
137+
}
138+
139+
/**
140+
* @brief runs all tests for ::longest_common_string_length funcion
141+
*/
142+
static void tests() {
143+
const auto test_cases = get_test_cases();
144+
assert(test_cases.size() > 0);
145+
test_longest_common_string_length(test_cases);
146+
test_longest_common_string_length_is_symmetric(test_cases);
147+
test_longest_common_string_length_for_reversed_inputs(test_cases);
148+
149+
std::cout << "All tests have successfully passed!\n";
150+
}
151+
152+
/**
153+
* @brief Main function
154+
* @returns 0 on exit
155+
*/
156+
int main() {
157+
tests();
158+
return 0;
53159
}

graphics/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ if(OpenGL_FOUND)
66
include(ExternalProject)
77
ExternalProject_Add (
88
FREEGLUT-PRJ
9-
URL https://github.com/FreeGLUTProject/freeglut/releases/download/v3.2.2/freeglut-3.2.2.tar.gz
10-
URL_MD5 485c1976165315fc42c0b0a1802816d9
9+
URL https://github.com/FreeGLUTProject/freeglut/releases/download/v3.4.0/freeglut-3.4.0.tar.gz
10+
URL_MD5 f1621464e6525d0368976870cab8f418
1111
CMAKE_GENERATOR ${CMAKE_GENERATOR}
1212
CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET}
1313
CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM}

scripts/file_linter.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
print("Python {}.{}.{}".format(*sys.version_info)) # Python 3.8
6+
with open("git_diff.txt") as in_file:
7+
modified_files = sorted(in_file.read().splitlines())
8+
print("{} files were modified.".format(len(modified_files)))
9+
10+
cpp_exts = tuple(".c .c++ .cc .cpp .cu .cuh .cxx .h .h++ .hh .hpp .hxx".split())
11+
cpp_files = [file for file in modified_files if file.lower().endswith(cpp_exts)]
12+
print(f"{len(cpp_files)} C++ files were modified.")
13+
if not cpp_files:
14+
sys.exit(0)
15+
16+
subprocess.run(["clang-tidy", "--fix", "-p=build", "--extra-arg=-std=c++11", *cpp_files, "--"],
17+
check=True, text=True, stderr=subprocess.STDOUT)
18+
19+
subprocess.run(["clang-format", "-i", "-style=file", *cpp_files],
20+
check=True, text=True, stderr=subprocess.STDOUT)
21+
22+
upper_files = [file for file in cpp_files if file != file.lower()]
23+
if upper_files:
24+
print(f"{len(upper_files)} files contain uppercase characters:")
25+
print("\n".join(upper_files) + "\n")
26+
27+
space_files = [file for file in cpp_files if " " in file or "-" in file]
28+
if space_files:
29+
print(f"{len(space_files)} files contain space or dash characters:")
30+
print("\n".join(space_files) + "\n")
31+
32+
nodir_files = [file for file in cpp_files if file.count(os.sep) != 1]
33+
if nodir_files:
34+
print(f"{len(nodir_files)} files are not in one and only one directory:")
35+
print("\n".join(nodir_files) + "\n")
36+
37+
bad_files = len(upper_files + space_files + nodir_files)
38+
if bad_files:
39+
sys.exit(bad_files)

0 commit comments

Comments
 (0)