From 1433ef22106c140121ecaba64cc2116fd0c39aa9 Mon Sep 17 00:00:00 2001 From: Rp-sushil Date: Thu, 20 Aug 2020 17:21:30 +0530 Subject: [PATCH 01/19] [BUG] #804 search/median_search.cpp FIXED --- search/median_search.cpp | 158 +++++++++++++++++++++++---------------- 1 file changed, 95 insertions(+), 63 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index 7379cad26..3190afa2d 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -1,78 +1,110 @@ /** * \file * \brief [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm - * \warning This core is erroneous and gives invorrect answers. Tested using * cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) * \ingroup median search - * \{ */ -#include + #include +#include #include +using namespace std; -/** - * @todo add documentation - */ -template -void comp(X x, std::vector *s1, std::vector *s2, - std::vector *s3) { - if (s1->size() >= x && s1->size() + s2->size() < x) { - std::cout << (*s2)[0] << " is the " << x + 1 << "th element from front"; - } else if (s1->size() > x) { - std::sort(s1->begin(), s1->end()); - std::cout << (*s1)[x] << " is the " << x + 1 << "th element from front"; - } else if (s1->size() + s2->size() <= x && s3->size() > x) { - std::sort(s3->begin(), s3->end()); - std::cout << (*s3)[x - s1->size() - s2->size()] << " is the " << x + 1 - << "th element from front"; - } else { - std::cout << x + 1 << " is invalid location"; - } +/* Assume that all the elements of A are distinct + def median_of_medians(A, i): + + #divide A into sublists of len 5 + sublists = [A[j:j+5] for j in range(0, len(A), 5)] + medians = [sorted(sublist)[len(sublist)/2] for sublist in sublists] + if len(medians) <= 5: + pivot = sorted(medians)[len(medians)/2] + else: + #the pivot is the median of the medians + pivot = median_of_medians(medians, len(medians)/2) + + #partitioning step + low = [j for j in A if j < pivot] + high = [j for j in A if j > pivot] + + k = len(low) + if i < k: + return median_of_medians(low,i) + elif i > k: + return median_of_medians(high,i-k-1) + else: #pivot = k + return pivot +*/ + +/* + * Here are some example lists you can use to see how the algorithm works + * A = [1,2,3,4,5,1000,8,9,99] (Contain Unique Elements) + * B = [1,2,3,4,5,6] (Contains Unique Elements) + * print median_of_medians(A, 0) #should be 1 + * print median_of_medians(A,7) #should be 99 + * print median_of_medians(B,4) #should be 5 +*/ + +int median_of_medians(vector a, int idx){ + int pivot; + vector m; + int r = a.size(); + for(int i = 0; i < r; i += 5){ + sort(a.begin() + i, a.begin() + min(r, i + 5)); + int mid = (i + min(r, i + 5)) / 2; + m.push_back(a[mid]); + } + int sz = int(m.size()); + if(sz <= 5){ + sort(m.begin(), m.end()); + pivot = m[(sz- 1) / 2]; + } + else{ + pivot = median_of_medians(m, idx); + } + vector low; + vector high; + for(int i = 0; i < r; i++){ + if(a[i] < pivot) + low.push_back(a[i]); + else if(a[i] > pivot) + high.push_back(a[i]); + } + int k = int(low.size()); + if(idx < k) + return median_of_medians(low, idx); + else if(idx > k) + return median_of_medians(high, idx-k-1); + else + return pivot; } -#define MAX_NUM 20 ///< maximum number of values to sort from +/* Main function*/ -/** - * Main function - */ -int main() { - std::vector v{25, 21, 98, 100, 76, 22, 43, 60, 89, 87}; - std::vector s1; - std::vector s2; - std::vector s3; +int main() +{ + int n; + cout << "Enter Size of Array: "; + cin >> n; + vector a(n); + cout << "Enter Array: "; + for(int i = 0; i < n; i++) + cin >> a[i]; - // creates an array of random numbers - // for (int i = 0; i < MAX_NUM; i++) { - // int r = std::rand() % 1000; - // v.push_back(r); - // std::cout << r << " "; - // } - for (int r : v) std::cout << r << " "; + cout << "Median: "; + int x = median_of_medians(a, (n - 1) / 2); + if(n % 2 == 0){ + int y = median_of_medians(a, n / 2); + cout << (float(x) + float(y))/2.0; + } + else + cout << x; - int median = std::rand() % 1000; // initialize to a random numnber - - std::cout << "\nmedian=" << median << std::endl; - int avg1, avg2, avg3, sum1 = 0, sum2 = 0, sum3 = 0; - - for (int i = 0; i < v.size(); i++) { // iterate through all numbers - if (v.back() == v[median]) { - avg1 = sum1 + v.back(); - s2.push_back(v.back()); - } else if (v.back() < v[median]) { - avg2 = sum2 + v.back(); - s1.push_back(v.back()); - } else { - avg3 = sum3 + v.back(); - s3.push_back(v.back()); - } - v.pop_back(); - } - - int x; - std::cout << "enter the no. to be searched form begining:- "; - std::cin >> x; - comp(x - 1, &s1, &s2, &s3); - - return 0; + cout << "\nTo find i-th smallest element "; + cout << "\nEnter i: "; + int idx; + cin >> idx; + idx--; + cout << "i-th smallest element: " << median_of_medians(a, idx) << endl; + return 0; } -/// } + From 0d61b0494f01149fe78a3e3132e095392ed06cfd Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Fri, 21 Aug 2020 10:45:00 +0530 Subject: [PATCH 02/19] fixed clang-tidy warnings --- search/median_search.cpp | 68 +++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index 3190afa2d..c67ad170b 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -8,7 +8,6 @@ #include #include #include -using namespace std; /* Assume that all the elements of A are distinct def median_of_medians(A, i): @@ -44,67 +43,72 @@ using namespace std; * print median_of_medians(B,4) #should be 5 */ -int median_of_medians(vector a, int idx){ - int pivot; - vector m; +int median_of_medians(std::vector a, int idx){ + int pivot = 0; // initialized with zero + std::vector m; int r = a.size(); for(int i = 0; i < r; i += 5){ - sort(a.begin() + i, a.begin() + min(r, i + 5)); - int mid = (i + min(r, i + 5)) / 2; + std::sort(a.begin() + i, a.begin() + std::min(r, i + 5)); + int mid = (i + std::min(r, i + 5)) / 2; m.push_back(a[mid]); } int sz = int(m.size()); if(sz <= 5){ - sort(m.begin(), m.end()); + std::sort(m.begin(), m.end()); pivot = m[(sz- 1) / 2]; } else{ pivot = median_of_medians(m, idx); } - vector low; - vector high; + std::vector low; + std::vector high; for(int i = 0; i < r; i++){ - if(a[i] < pivot) + if(a[i] < pivot){ low.push_back(a[i]); - else if(a[i] > pivot) + } + else if(a[i] > pivot){ high.push_back(a[i]); + } } int k = int(low.size()); - if(idx < k) + if(idx < k){ return median_of_medians(low, idx); - else if(idx > k) + } + else if(idx > k){ return median_of_medians(high, idx-k-1); - else + } + else{ return pivot; + } } /* Main function*/ int main() { - int n; - cout << "Enter Size of Array: "; - cin >> n; - vector a(n); - cout << "Enter Array: "; - for(int i = 0; i < n; i++) - cin >> a[i]; - - cout << "Median: "; + int n = 0; + std::cout << "Enter Size of Array: "; + std::cin >> n; + std::vector a(n); + std::cout << "Enter Array: "; + for(int i = 0; i < n; i++){ + std::cin >> a[i]; + } + std::cout << "Median: "; // Median defination: https://en.wikipedia.org/wiki/Median int x = median_of_medians(a, (n - 1) / 2); if(n % 2 == 0){ int y = median_of_medians(a, n / 2); - cout << (float(x) + float(y))/2.0; + std::cout << (float(x) + float(y))/2.0; } - else - cout << x; - - cout << "\nTo find i-th smallest element "; - cout << "\nEnter i: "; - int idx; - cin >> idx; + else{ + std::cout << x; + } + std::cout << "\nTo find i-th smallest element "; + std::cout << "\nEnter i: "; + int idx = 0; + std::cin >> idx; idx--; - cout << "i-th smallest element: " << median_of_medians(a, idx) << endl; + std::cout << idx + 1<< "-th smallest element: " << median_of_medians(a, idx) << '\n'; return 0; } From 406ad387db413690247cc77ba6ba6d34cac186c6 Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Fri, 21 Aug 2020 12:08:06 +0530 Subject: [PATCH 03/19] Suggested changes has been made. --- search/median_search.cpp | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index c67ad170b..4ef73ac08 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -1,14 +1,20 @@ -/** - * \file - * \brief [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm - * cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) - * \ingroup median search - */ +// Given an array A[1,...,n] of n numbers and an index idx, idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A. #include #include #include +/** + * @namespace search + * @brief Search algorithms + */ +namespace search { +/** + * @namespace median_search + * @brief Functions for [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm + * @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) + */ +namespace median_search { /* Assume that all the elements of A are distinct def median_of_medians(A, i): @@ -43,8 +49,8 @@ * print median_of_medians(B,4) #should be 5 */ -int median_of_medians(std::vector a, int idx){ - int pivot = 0; // initialized with zero +int median_of_medians(std::vector a, int idx){ // Search the element in **a** whose index is **idx** and return element at index **idx** in **a** (a[idx]) + int pivot = 0; // initialized with zero std::vector m; int r = a.size(); for(int i = 0; i < r; i += 5){ @@ -81,9 +87,12 @@ int median_of_medians(std::vector a, int idx){ return pivot; } } +} // namespace median_search +} // namespace search -/* Main function*/ - +/** + * Main function + */ int main() { int n = 0; @@ -95,9 +104,9 @@ int main() std::cin >> a[i]; } std::cout << "Median: "; // Median defination: https://en.wikipedia.org/wiki/Median - int x = median_of_medians(a, (n - 1) / 2); + int x = search::median_search::median_of_medians(a, (n - 1) / 2); if(n % 2 == 0){ - int y = median_of_medians(a, n / 2); + int y = search::median_search::median_of_medians(a, n / 2); std::cout << (float(x) + float(y))/2.0; } else{ @@ -108,7 +117,7 @@ int main() int idx = 0; std::cin >> idx; idx--; - std::cout << idx + 1<< "-th smallest element: " << median_of_medians(a, idx) << '\n'; + std::cout << idx + 1<< "-th smallest element: " << search::median_search::median_of_medians(a, idx) << '\n'; return 0; } From b631eee388da277a66e781dd8e8bb9ebcb2f1d75 Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Fri, 21 Aug 2020 23:47:07 +0530 Subject: [PATCH 04/19] added a comment block like in #962. --- search/median_search.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index 4ef73ac08..0488a5188 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -1,5 +1,14 @@ -// Given an array A[1,...,n] of n numbers and an index idx, idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A. - +/** + * @file median_search.cpp + * @brief Implementation of [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm. + * @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) + * + * @details + * Given an array A[1,...,n] of n numbers and an index idx, idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A. + * \note this algorithm implements median search for only arrays which have distinct elements + * + * @author [Sushil Kumar](https://github.com/Rp-sushil) + */ #include #include #include @@ -12,7 +21,6 @@ namespace search { /** * @namespace median_search * @brief Functions for [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm - * @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) */ namespace median_search { /* Assume that all the elements of A are distinct From caed3ddadd2e2ff8d3a43062aab27a68efd6294b Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Sat, 22 Aug 2020 01:53:54 +0530 Subject: [PATCH 05/19] Changed has been made (#962 as a reference) --- search/median_search.cpp | 77 ++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index 0488a5188..28f74b17a 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -1,14 +1,43 @@ /** * @file median_search.cpp - * @brief Implementation of [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm. + * @brief Implementation of [Median search](https://en.wikipedia.org/wiki/Median_of_medians) algorithm. * @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) * * @details - * Given an array A[1,...,n] of n numbers and an index idx, idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A. + * Given an array A[1,...,n] of n numbers and an index idx, idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A. + * median_of_medians(A, i): + * #divide A into sublists of len 5 + * sublists = [A[j:j+5] for j in range(0, len(A), 5)] + * medians = [sorted(sublist)[len(sublist)/2] for sublist in sublists] + * if len(medians) <= 5: + * pivot = sorted(medians)[len(medians)/2] + * else: + * #the pivot is the median of the medians + * pivot = median_of_medians(medians, len(medians)/2) + * #partitioning step + * low = [j for j in A if j < pivot] + * high = [j for j in A if j > pivot] + * k = len(low) + * if i < k: + * return median_of_medians(low,i) + * elif i > k: + * return median_of_medians(high,i-k-1) + * else: #pivot = k + * return pivot + * * \note this algorithm implements median search for only arrays which have distinct elements - * + * + * Here are some example lists you can use to see how the algorithm works + * A = [1,2,3,4,5,1000,8,9,99] (Contain Unique Elements) + * B = [1,2,3,4,5,6] (Contains Unique Elements) + * print median_of_medians(A, 0) #should be 1 + * print median_of_medians(A,7) #should be 99 + * print median_of_medians(B,4) #should be 5 + * + * @author Unknown author * @author [Sushil Kumar](https://github.com/Rp-sushil) */ + #include #include #include @@ -23,42 +52,14 @@ namespace search { * @brief Functions for [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm */ namespace median_search { -/* Assume that all the elements of A are distinct - def median_of_medians(A, i): - - #divide A into sublists of len 5 - sublists = [A[j:j+5] for j in range(0, len(A), 5)] - medians = [sorted(sublist)[len(sublist)/2] for sublist in sublists] - if len(medians) <= 5: - pivot = sorted(medians)[len(medians)/2] - else: - #the pivot is the median of the medians - pivot = median_of_medians(medians, len(medians)/2) - - #partitioning step - low = [j for j in A if j < pivot] - high = [j for j in A if j > pivot] - - k = len(low) - if i < k: - return median_of_medians(low,i) - elif i > k: - return median_of_medians(high,i-k-1) - else: #pivot = k - return pivot -*/ - -/* - * Here are some example lists you can use to see how the algorithm works - * A = [1,2,3,4,5,1000,8,9,99] (Contain Unique Elements) - * B = [1,2,3,4,5,6] (Contains Unique Elements) - * print median_of_medians(A, 0) #should be 1 - * print median_of_medians(A,7) #should be 99 - * print median_of_medians(B,4) #should be 5 -*/ - -int median_of_medians(std::vector a, int idx){ // Search the element in **a** whose index is **idx** and return element at index **idx** in **a** (a[idx]) +/** +* This function Search the element in **a** whose index is **idx** and return element at index **idx** in **a** (a[idx]) +* @param A(list) and idx(index) of element which we want to search +* @return corresponding element which we want to search. +*/ +int median_of_medians(const std::vector& A, const int& idx) { int pivot = 0; // initialized with zero + std::vector a(A.begin(), A.end()); std::vector m; int r = a.size(); for(int i = 0; i < r; i += 5){ From 498db114f3e053dc215ca8871ca5d8b9ca49a0b3 Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Sat, 22 Aug 2020 02:22:47 +0530 Subject: [PATCH 06/19] updated median_search (#962 as reference) --- search/median_search.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index 28f74b17a..3cf1ea994 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -53,8 +53,9 @@ namespace search { */ namespace median_search { /** -* This function Search the element in **a** whose index is **idx** and return element at index **idx** in **a** (a[idx]) -* @param A(list) and idx(index) of element which we want to search +* This function search the element in an array for the given index. +* @param A an array +* @param idx the index * @return corresponding element which we want to search. */ int median_of_medians(const std::vector& A, const int& idx) { From 306de24e703f6fba5366d4dcf96f08a3211a09ad Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Sat, 22 Aug 2020 02:39:13 +0530 Subject: [PATCH 07/19] Suggested changes has been made. --- search/median_search.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index 3cf1ea994..f252e8430 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -54,9 +54,9 @@ namespace search { namespace median_search { /** * This function search the element in an array for the given index. -* @param A an array -* @param idx the index -* @return corresponding element which we want to search. +* @param A array where numbers are saved +* @param idx current index in array +* @returns corresponding element which we want to search. */ int median_of_medians(const std::vector& A, const int& idx) { int pivot = 0; // initialized with zero From 37b252697da4638bebc26531f7859314bfbc2ba7 Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Sun, 23 Aug 2020 00:29:00 +0530 Subject: [PATCH 08/19] test added --- search/median_search.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index f252e8430..08cb676c9 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -4,7 +4,7 @@ * @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) * * @details - * Given an array A[1,...,n] of n numbers and an index idx, idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A. + * Given an array A[1,...,n] of n numbers and an index idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A. * median_of_medians(A, i): * #divide A into sublists of len 5 * sublists = [A[j:j+5] for j in range(0, len(A), 5)] @@ -41,6 +41,7 @@ #include #include #include +#include /** * @namespace search @@ -100,11 +101,33 @@ int median_of_medians(const std::vector& A, const int& idx) { } // namespace median_search } // namespace search +/** + * Function to test above algorithm + */ +void test(){ + std::vector A{25,21,98,100,76,22,43,60,89,87}; + int i = 3; + assert(A[6] == search::median_search::median_of_medians(A, i)); // A[6] = 43, is the fourth smallest element. + std::cout << "test case:1 passed\n"; + + std::vector B{1,2,3,4,5,6}; + int j = 4; + assert(B[4] == search::median_search::median_of_medians(B, j)); // B[4] = 5, is the fifth smallest element. + std::cout << "test case:2 passed\n"; + + std::vector C{1,2,3,4,5,1000,8,9,99}; + int k = 3; + assert(C[3] == search::median_search::median_of_medians(C, k)); // C[3] = 4, is the fourth smallest element. + std::cout << "test case:3 passed\n"; + std::cout << "--All tests passed--\n"; +} + /** * Main function */ int main() { + test(); int n = 0; std::cout << "Enter Size of Array: "; std::cin >> n; From a1242904e1faad3c4391244477dd428f4f6b3f72 Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Sun, 23 Aug 2020 00:37:46 +0530 Subject: [PATCH 09/19] repeated constraints removed. --- search/median_search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index 08cb676c9..c20bcfeaa 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -4,7 +4,7 @@ * @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) * * @details - * Given an array A[1,...,n] of n numbers and an index idx, where 1≤idx≤ n, 1≤idx≤ n, find the i-th smallest element of A. + * Given an array A[1,...,n] of n numbers and an index idx, where 1≤idx≤ n, find the i-th smallest element of A. * median_of_medians(A, i): * #divide A into sublists of len 5 * sublists = [A[j:j+5] for j in range(0, len(A), 5)] From b50591dfcedd55ed86ba7c119a1da3ff986305de Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Sun, 23 Aug 2020 15:49:52 +0530 Subject: [PATCH 10/19] Suggested changes has been made. --- search/median_search.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/search/median_search.cpp b/search/median_search.cpp index c20bcfeaa..479de8107 100644 --- a/search/median_search.cpp +++ b/search/median_search.cpp @@ -4,7 +4,7 @@ * @cases from [here](https://brilliant.org/wiki/median-finding-algorithm/) * * @details - * Given an array A[1,...,n] of n numbers and an index idx, where 1≤idx≤ n, find the i-th smallest element of A. + * Given an array A[1,...,n] of n numbers and an index i, where 1 ≤ i ≤ n, find the i-th smallest element of A. * median_of_medians(A, i): * #divide A into sublists of len 5 * sublists = [A[j:j+5] for j in range(0, len(A), 5)] @@ -107,12 +107,12 @@ int median_of_medians(const std::vector& A, const int& idx) { void test(){ std::vector A{25,21,98,100,76,22,43,60,89,87}; int i = 3; - assert(A[6] == search::median_search::median_of_medians(A, i)); // A[6] = 43, is the fourth smallest element. + assert(A[6] == search::median_search::median_of_medians(A, i)); // A[6] = 43, is the fourth smallest element. std::cout << "test case:1 passed\n"; std::vector B{1,2,3,4,5,6}; int j = 4; - assert(B[4] == search::median_search::median_of_medians(B, j)); // B[4] = 5, is the fifth smallest element. + assert(B[4] == search::median_search::median_of_medians(B, j)); // B[4] = 5, is the fifth smallest element. std::cout << "test case:2 passed\n"; std::vector C{1,2,3,4,5,1000,8,9,99}; From 039022becf5539ad852e0ef4b10cd4ef61469883 Mon Sep 17 00:00:00 2001 From: Anushka Verma Date: Mon, 24 Aug 2020 01:19:39 +0530 Subject: [PATCH 11/19] fix typo: (rename file) fast_interger_input -> fast_integer_input --- others/{fast_interger_input.cpp => fast_integer_input.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename others/{fast_interger_input.cpp => fast_integer_input.cpp} (100%) diff --git a/others/fast_interger_input.cpp b/others/fast_integer_input.cpp similarity index 100% rename from others/fast_interger_input.cpp rename to others/fast_integer_input.cpp From 7a13a35dfd6d4563d5dab7db3547b5e8f131fc61 Mon Sep 17 00:00:00 2001 From: Anushka Verma Date: Mon, 24 Aug 2020 01:47:16 +0530 Subject: [PATCH 12/19] feat: add word break solution (backtracking) --- backtracking/word_break.cpp | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 backtracking/word_break.cpp diff --git a/backtracking/word_break.cpp b/backtracking/word_break.cpp new file mode 100644 index 000000000..b41a6aa77 --- /dev/null +++ b/backtracking/word_break.cpp @@ -0,0 +1,50 @@ +/** + * @file Word Break + * + * @details + * Given a valid sentence without any spaces between the words and + * a dictionary of valid English words, find all possible ways to + * break the sentence in individual dictionary words. + * + * @author [Anushka Verma](https://github.com/verma-anushka) + */ + +#include +using namespace std; + +/** + * Utility function + */ +void wordbreak(string s, vector& dict, int i, string ans) { + + string word=s.substr(0, i+1); + int ws=s.size(); + + // search for word in the dictionary + if(find(dict.begin(), dict.end(), word) != dict.end()) { + + if(i dict = { "mobile","samsung","sam","sung", + "man","mango","icecream","and", + "go","i","like","ice","cream" }; + string s = "ilikesamsung"; + wordbreak(s, dict, 0, ""); + return 0; +} + From 47bbc2a16e28ed92aab5269fc390de3ee16e3b16 Mon Sep 17 00:00:00 2001 From: Anushka Verma Date: Mon, 24 Aug 2020 01:54:17 +0530 Subject: [PATCH 13/19] remove word break --- backtracking/word_break.cpp | 50 ------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 backtracking/word_break.cpp diff --git a/backtracking/word_break.cpp b/backtracking/word_break.cpp deleted file mode 100644 index b41a6aa77..000000000 --- a/backtracking/word_break.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/** - * @file Word Break - * - * @details - * Given a valid sentence without any spaces between the words and - * a dictionary of valid English words, find all possible ways to - * break the sentence in individual dictionary words. - * - * @author [Anushka Verma](https://github.com/verma-anushka) - */ - -#include -using namespace std; - -/** - * Utility function - */ -void wordbreak(string s, vector& dict, int i, string ans) { - - string word=s.substr(0, i+1); - int ws=s.size(); - - // search for word in the dictionary - if(find(dict.begin(), dict.end(), word) != dict.end()) { - - if(i dict = { "mobile","samsung","sam","sung", - "man","mango","icecream","and", - "go","i","like","ice","cream" }; - string s = "ilikesamsung"; - wordbreak(s, dict, 0, ""); - return 0; -} - From 149fe0cd63294d91b572cf9f0156522a29f8d644 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 25 Aug 2020 12:23:25 +0000 Subject: [PATCH 14/19] updating DIRECTORY.md --- DIRECTORY.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 605c4a1fe..4d95ad3ac 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -113,7 +113,9 @@ * [Adaline Learning](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/machine_learning/adaline_learning.cpp) * [Kohonen Som Topology](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/machine_learning/kohonen_som_topology.cpp) * [Kohonen Som Trace](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/machine_learning/kohonen_som_trace.cpp) + * [Neural Network](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/machine_learning/neural_network.cpp) * [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/machine_learning/ordinary_least_squares_regressor.cpp) + * [Vector Ops](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/machine_learning/vector_ops.hpp) ## Math * [Armstrong Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/armstrong_number.cpp) @@ -182,7 +184,7 @@ * [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_binary.cpp) * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_hexadecimal.cpp) * [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_roman_numeral.cpp) - * [Fast Interger Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_interger_input.cpp) + * [Fast Integer Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_integer_input.cpp) * [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp) * [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp) * [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/palindrome_of_number.cpp) From de868c9faa2ac2191343c177549c03393a8f8a86 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Tue, 25 Aug 2020 19:20:32 -0400 Subject: [PATCH 15/19] [enhancement] guideline for multiple implementations (#1051) * guideline for multiple implementations * updating DIRECTORY.md * fixed spelling Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- CONTRIBUTING.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 35d875d43..07d620dea 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -23,7 +23,8 @@ We are very happy that you consider implementing algorithms and data structures - Please use the directory structure of the repository. - File extension for code should be *.h *.cpp. - Don't use **bits/stdc++.h** because this is quite Linux specific and slows down the compilation process. -- Avoid using **struct** and instead use the **class** keyword. +- Organize your code using **`struct`**, **`class`** and/or **`namespace`** keywords +- If an implementation of the algorithm already exists, please refer to the [file-name section below](#new-file-name-guidelines). - You can suggest reasonable changes to existing algorithms. - Strictly use snake_case (underscore_separated) in filenames. - If you have added or modified code, please make sure the code compiles before submitting. @@ -116,6 +117,7 @@ my_new_cpp_class.cpp is correct format ``` - It will be used to dynamically create a directory of files and implementation. - File name validation will run on docker to ensure the validity. +- If an implementation of the algorithm already exists and your version is different from that implemented, please use incremental numeric digit as a suffix. For example, if `median_search.cpp` already exists in the `search` folder and you are contributing a new implementation, the filename should be `median_search2.cpp` and for a third implementation, `median_search3.cpp`. #### New Directory guidelines - We recommend adding files to existing directories as much as possible. From 2882b7bec2aad61bd42912d2262696aebec3648c Mon Sep 17 00:00:00 2001 From: Filip Hlasek Date: Tue, 25 Aug 2020 16:56:49 -0700 Subject: [PATCH 16/19] fix: math/fibonacci linter warnings. (#1047) * fix: math/fibonacci linter warnings. * updating DIRECTORY.md * doxygen * unit64_t instead of unsigned int Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- math/fibonacci.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 493523b61..4e3c15de8 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -13,12 +13,15 @@ /** * Recursively compute sequences + * @param n input + * @returns n-th element of the Fbinacci's sequence */ -unsigned int fibonacci(unsigned int n) { +uint64_t fibonacci(uint64_t n) { /* If the input is 0 or 1 just return the same This will set the first 2 values of the sequence */ - if (n <= 1) + if (n <= 1) { return n; + } /* Add the last 2 values of the sequence to get next */ return fibonacci(n - 1) + fibonacci(n - 2); @@ -30,27 +33,27 @@ unsigned int fibonacci(unsigned int n) { * @returns `void` */ static void test() { - unsigned int test_case_1 = fibonacci(0); + uint64_t test_case_1 = fibonacci(0); assert(test_case_1 == 0); std::cout << "Passed Test 1!" << std::endl; - unsigned int test_case_2 = fibonacci(1); + uint64_t test_case_2 = fibonacci(1); assert(test_case_2 == 1); std::cout << "Passed Test 2!" << std::endl; - unsigned int test_case_3 = fibonacci(2); + uint64_t test_case_3 = fibonacci(2); assert(test_case_3 == 1); std::cout << "Passed Test 3!" << std::endl; - unsigned int test_case_4 = fibonacci(3); + uint64_t test_case_4 = fibonacci(3); assert(test_case_4 == 2); std::cout << "Passed Test 4!" << std::endl; - unsigned int test_case_5 = fibonacci(4); + uint64_t test_case_5 = fibonacci(4); assert(test_case_5 == 3); std::cout << "Passed Test 5!" << std::endl; - unsigned int test_case_6 = fibonacci(15); + uint64_t test_case_6 = fibonacci(15); assert(test_case_6 == 610); std::cout << "Passed Test 6!" << std::endl << std::endl; } @@ -58,7 +61,7 @@ static void test() { /// Main function int main() { test(); - int n; + int n = 0; std::cin >> n; assert(n >= 0); std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl; From 66dcc4c394c823f5ce1a964e050077899decd48c Mon Sep 17 00:00:00 2001 From: Lownish Rai Sookha <69192570+Lownish@users.noreply.github.com> Date: Wed, 26 Aug 2020 05:27:24 +0530 Subject: [PATCH 17/19] feat: add Pigeonhole algorithm (#1028) * feat: add Pigeonhole algorithm * Executed clang-format * Used pointers and vector * Corrected clang-tidy issues * Modified code structure * Apply suggestions from code review Suggested changes applied Co-authored-by: David Leal * Added missing parameter and documentation * Added delete function * Update pigeonhole_sort.cpp * Corrected delete function Co-authored-by: David Leal * Apply suggestions from code review Co-authored-by: David Leal * Apply suggestions from code review Co-authored-by: David Leal * Changed documentation regarding array size * clang-tidy * Apply suggestions from code review Co-authored-by: David Leal * assert moved to test function * Update pigeonhole_sort.cpp * Update sorting/pigeonhole_sort.cpp Co-authored-by: David Leal * Added test function and inbuilt min function * min and max to const variables * const int* to auto * const int* to auto * Apply suggestions from code review Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * Modified pigeonSort documentation * Corrected test functions Co-authored-by: lsurface Co-authored-by: David Leal Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> --- sorting/pigeonhole_sort.cpp | 133 ++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 sorting/pigeonhole_sort.cpp diff --git a/sorting/pigeonhole_sort.cpp b/sorting/pigeonhole_sort.cpp new file mode 100644 index 000000000..764365b86 --- /dev/null +++ b/sorting/pigeonhole_sort.cpp @@ -0,0 +1,133 @@ +/** + * @file + * @brief Implementation of [Pigeonhole Sort algorithm] + * (https://en.wikipedia.org/wiki/Pigeonhole_sort) + * @author [Lownish](https://github.com/Lownish) + * @details + * Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists + * of elements where the number of elements and the number of possible key + * values are approximately the same. It requires O(n + Range) time where n is + * number of elements in input array and ‘Range’ is number of possible values in + * array. + * + * The time Complexity of the algorithm is \f$O(n+N)\f$. + */ + +#include //for std::is_sorted +#include //for std::array +#include //for assert +#include //for io operations + +/** + * @namespace sorting + * @brief Sorting algorithms + */ +namespace sorting { + +/** + * Pigeonhole sorting of array of size n + * The function will sort the array through Pigeonhole algorithm and print + * @param arr unsorted array of elements + * @returns sorted array of elements + */ +template +std::array pigeonSort(std::array arr) { + // Finding min and max* + auto min = std::min_element(std::begin(arr), std::end(arr)); + auto max = std::max_element(std::begin(arr), std::end(arr)); + + // Range refers to the number of holes required + int range = *max - *min + 1; + int *hole = new int[range](); + + // Copying all array values to pigeonhole + for (int i = 0; i < N; i++) { + hole[arr[i] - *min] = arr[i]; + } + + // Deleting elements from list and storing to original array + int count = 0; + for (int i = 0; i < range; i++) { + while (hole[i] != '\0') { + arr[count] = hole[i]; + hole[i] = {}; + count++; + } + } + delete[] hole; + + return arr; +} +} // namespace sorting + +/** + * Test function 1 with unsorted array + * {8, 3, 2, 7, 4, 6, 8} + * @returns none + */ +static void test_1() { + const int n = 7; + std::array test_array = {8, 3, 2, 7, 4, 6, 8}; + + test_array = sorting::pigeonSort(test_array); + + assert(std::is_sorted(std::begin(test_array), std::end(test_array))); + + // Printing sorted array + for (int i = 0; i < n; i++) { + std::cout << test_array.at(i) << " "; + } + std::cout << "\nPassed\n"; +} + +/** + * Test function 2 with unsorted array + * {802, 630, 20, 745, 52, 300, 612, 932, 78, 187} + * @returns none + */ +static void test_2() { + const int n = 10; + std::array test_array = {802, 630, 20, 745, 52, + 300, 612, 932, 78, 187}; + + test_array = sorting::pigeonSort(test_array); + + assert(std::is_sorted(std::begin(test_array), std::end(test_array))); + + // Printing sorted array + for (int i = 0; i < n; i++) { + std::cout << test_array.at(i) << " "; + } + std::cout << "\nPassed\n"; +} + +/** + * Test function 1 with unsorted array + * {11,13,12,14} + * @returns none + */ +static void test_3() { + const int n = 4; + std::array test_array = {11, 13, 12, 14}; + + test_array = sorting::pigeonSort(test_array); + + assert(std::is_sorted(std::begin(test_array), std::end(test_array))); + + // Printing sorted array + for (int i = 0; i < n; i++) { + std::cout << test_array.at(i) << " "; + } + std::cout << "\nPassed\n"; +} + +/** + * Main function + */ +int main() { + test_1(); + test_2(); + test_3(); + + return 0; +} From 63333f38ccd81d82eb2b6a5ee6786bdbe5b83a04 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 25 Aug 2020 23:58:06 +0000 Subject: [PATCH 18/19] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 4d95ad3ac..178e88499 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -241,6 +241,7 @@ * [Non Recursive Merge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/non_recursive_merge_sort.cpp) * [Numeric String Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/numeric_string_sort.cpp) * [Odd Even Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/odd_even_sort.cpp) + * [Pigeonhole Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/pigeonhole_sort.cpp) * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort.cpp) * [Quick Sort 3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort_3.cpp) * [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/radix_sort.cpp) From c4f586b6ab269948210f455520fc6b48a8b1378d Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 26 Aug 2020 10:48:08 -0400 Subject: [PATCH 19/19] [bug fix] fix code formatting in CI (#1052) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: linter and spacing for is_graph_bipartite. * updating DIRECTORY.md * clang-tidy fixes for a49ec9b8d79e62be49587043cf482a9f705e3ed1 * use clang-12 * downgrade to clang11 * added clang-format confiug file * added explicit clang-format step in workflow * fix git command * commit format and lint together lint first and then format * corrected order * Revert "Merge branch 'is_graph_bipartite' into fix_clang" This reverts commit d4d406017502cc745a9a77b4d462dc7a4aea6522, reversing changes made to 2ccc3a364e4a6491e62ad836a237eb1603ddef6a. Co-authored-by: Filip Hlásek Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- .clang-format | 167 +++++++++++++++++++++++++ .github/workflows/awesome_workflow.yml | 16 +-- 2 files changed, 172 insertions(+), 11 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..2711cbb0b --- /dev/null +++ b/.clang-format @@ -0,0 +1,167 @@ +--- +Language: Cpp +AccessModifierOffset: -3 +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: true +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: true +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^' + Priority: 2 + SortPriority: 0 + - Regex: '^<.*\.h>' + Priority: 1 + SortPriority: 0 + - Regex: '^<.*' + Priority: 2 + SortPriority: 0 + - Regex: '.*' + Priority: 3 + SortPriority: 0 +IncludeIsMainRegex: '([-_](test|unittest))?$' +IncludeIsMainSourceRegex: '' +IndentCaseLabels: true +IndentGotoLabels: true +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Never +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +RawStringFormats: + - Language: Cpp + Delimiters: + - cc + - CC + - cpp + - Cpp + - CPP + - 'c++' + - 'C++' + CanonicalDelimiter: '' + BasedOnStyle: google + - Language: TextProto + Delimiters: + - pb + - PB + - proto + - PROTO + EnclosingFunctions: + - EqualsProto + - EquivToProto + - PARSE_PARTIAL_TEXT_PROTO + - PARSE_TEST_PROTO + - PARSE_TEXT_PROTO + - ParseTextOrDie + - ParseTextProtoOrDie + CanonicalDelimiter: '' + BasedOnStyle: google +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +Standard: Auto +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseCRLF: false +UseTab: Never +... + diff --git a/.github/workflows/awesome_workflow.yml b/.github/workflows/awesome_workflow.yml index 9cff1c48d..78ea62c7b 100644 --- a/.github/workflows/awesome_workflow.yml +++ b/.github/workflows/awesome_workflow.yml @@ -16,7 +16,7 @@ jobs: - name: requirements run: | sudo apt -qq -y update - sudo apt -qq install clang-tidy-10 + sudo apt -qq install clang-tidy-10 clang-format-10 # checks are passing with less errors when used with this version. # The default installs v6.0 which did not work out well in my tests - name: Setup Git Specs @@ -44,7 +44,7 @@ jobs: git "mv" "${fname}" ${new_fname} fi done - git commit -am "formatting filenames $GITHUB_SHA" || true + git commit -am "formatting filenames ${GITHUB_SHA::8}" || true - name: Update DIRECTORY.md shell: python @@ -124,15 +124,9 @@ jobs: subprocess.run(["clang-tidy-10", "--fix", "-p=build", "--extra-arg=-std=c++11", *cpp_files, "--"], check=True, text=True, stderr=subprocess.STDOUT) - # for cpp_file in cpp_files: - # subprocess.run(["clang-tidy-10", "--fix", "-p=build", cpp_file, "--"], - # check=True, text=True, stderr=subprocess.STDOUT) - # print("g++:") - # compile_exts = tuple(".c .c++ .cc .cpp .cu .cxx".split()) - # compile_files = [file for file in cpp_files if file.lower().endswith(compile_exts)] - # for cpp_file in cpp_files: - # subprocess.run(["g++", cpp_file], check=True, text=True) + subprocess.run(["clang-format-10", "-i", "-style=file", *cpp_files], + check=True, text=True, stderr=subprocess.STDOUT) upper_files = [file for file in cpp_files if file != file.lower()] if upper_files: @@ -155,7 +149,7 @@ jobs: - name: Commit and push changes run: | git diff DIRECTORY.md - git commit -am "clang-tidy fixes for $GITHUB_SHA" || true + git commit -am "clang-format and clang-tidy fixes for ${GITHUB_SHA::8}" || true git push --force origin HEAD:$GITHUB_REF || true build: