From 48b7773b37fe13792eed229e12aa54bec7d1ce5a Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 16:34:53 -0500 Subject: [PATCH 01/13] fix: Various LGTM fixes --- data_structures/stack_using_array.cpp | 14 +++++++------- data_structures/stack_using_linked_list.cpp | 14 +++++++------- math/fibonacci_fast.cpp | 12 ++++++------ others/paranthesis_matching.cpp | 10 +++++----- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/data_structures/stack_using_array.cpp b/data_structures/stack_using_array.cpp index 36be9eb39..ab2d7d7dd 100644 --- a/data_structures/stack_using_array.cpp +++ b/data_structures/stack_using_array.cpp @@ -1,31 +1,31 @@ #include int *stack; -int top = 0, stack_size; +int top_var = 0, stack_size; void push(int x) { - if (top == stack_size) { + if (top_var == stack_size) { std::cout << "\nOverflow"; } else { - stack[top++] = x; + stack[top_var++] = x; } } void pop() { - if (top == 0) { + if (top_var == 0) { std::cout << "\nUnderflow"; } else { - std::cout << "\n" << stack[--top] << " deleted"; + std::cout << "\n" << stack[--top_var] << " deleted"; } } void show() { - for (int i = 0; i < top; i++) { + for (int i = 0; i < top_var; i++) { std::cout << stack[i] << "\n"; } } -void topmost() { std::cout << "\nTopmost element: " << stack[top - 1]; } +void topmost() { std::cout << "\nTopmost element: " << stack[top_var - 1]; } int main() { std::cout << "\nEnter stack_size of stack : "; std::cin >> stack_size; diff --git a/data_structures/stack_using_linked_list.cpp b/data_structures/stack_using_linked_list.cpp index ae53fe95a..05f0d3d6a 100644 --- a/data_structures/stack_using_linked_list.cpp +++ b/data_structures/stack_using_linked_list.cpp @@ -6,28 +6,28 @@ struct node { node *next; }; -node *top; +node *top_var; void push(int x) { node *n = new node; n->val = x; - n->next = top; - top = n; + n->next = top_var; + top_var = n; } void pop() { - if (top == NULL) { + if (top_var == NULL) { cout << "\nUnderflow"; } else { - node *t = top; + node *t = top_var; cout << "\n" << t->val << " deleted"; - top = top->next; + top_var = top_var->next; delete t; } } void show() { - node *t = top; + node *t = top_var; while (t != NULL) { cout << t->val << "\n"; t = t->next; diff --git a/math/fibonacci_fast.cpp b/math/fibonacci_fast.cpp index 8fdb20058..80e9108ea 100644 --- a/math/fibonacci_fast.cpp +++ b/math/fibonacci_fast.cpp @@ -24,23 +24,23 @@ const uint64_t MAX = 93; /** Array of computed fibonacci numbers */ -uint64_t f[MAX] = {0}; +uint64_t numbers[MAX] = {0}; /** Algorithm */ uint64_t fib(uint64_t n) { if (n == 0) return 0; if (n == 1 || n == 2) - return (f[n] = 1); + return (numbers[n] = 1); - if (f[n]) - return f[n]; + if (numbers[n]) + return numbers[n]; uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; - f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) + numbers[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) : (2 * fib(k - 1) + fib(k)) * fib(k); - return f[n]; + return numbers[n]; } /** Main function */ diff --git a/others/paranthesis_matching.cpp b/others/paranthesis_matching.cpp index 2a6358d94..0a1a9e474 100644 --- a/others/paranthesis_matching.cpp +++ b/others/paranthesis_matching.cpp @@ -20,13 +20,13 @@ char stack[MAX]; //! pointer to track stack index -int top = -1; +int top_var = -1; //! push byte to stack variable -void push(char ch) { stack[++top] = ch; } +void push(char ch) { stack[++top_var] = ch; } //! pop a byte out of stack variable -char pop() { return stack[top--]; } +char pop() { return stack[top_var--]; } //! @}-------------- end stack ----------- @@ -56,7 +56,7 @@ int main() { while (valid == 1 && i < exp.length()) { if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<') { push(exp[i]); - } else if (top >= 0 && stack[top] == opening(exp[i])) { + } else if (top_var >= 0 && stack[top_var] == opening(exp[i])) { pop(); } else { valid = 0; @@ -65,7 +65,7 @@ int main() { } // makes sure the stack is empty after processsing (above) - if (valid == 1 && top == -1) { + if (valid == 1 && top_var == -1) { std::cout << "\nCorrect Expression"; } else { std::cout << "\nWrong Expression"; From cdacbf1998fd060c08897c687f8f695ff27559fa Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 16:37:46 -0500 Subject: [PATCH 02/13] fix: Remove namespace --- data_structures/stack_using_linked_list.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/data_structures/stack_using_linked_list.cpp b/data_structures/stack_using_linked_list.cpp index 05f0d3d6a..3dcf03f8a 100644 --- a/data_structures/stack_using_linked_list.cpp +++ b/data_structures/stack_using_linked_list.cpp @@ -1,5 +1,4 @@ #include -using namespace std; struct node { int val; @@ -17,10 +16,10 @@ void push(int x) { void pop() { if (top_var == NULL) { - cout << "\nUnderflow"; + std::cout << "\nUnderflow"; } else { node *t = top_var; - cout << "\n" << t->val << " deleted"; + std::cout << "\n" << t->val << " deleted"; top_var = top_var->next; delete t; } @@ -29,7 +28,7 @@ void pop() { void show() { node *t = top_var; while (t != NULL) { - cout << t->val << "\n"; + std::cout << t->val << "\n"; t = t->next; } } @@ -37,14 +36,14 @@ void show() { int main() { int ch, x; do { - cout << "\n1. Push"; - cout << "\n2. Pop"; - cout << "\n3. Print"; - cout << "\nEnter Your Choice : "; - cin >> ch; + std::cout << "\n1. Push"; + std::cout << "\n2. Pop"; + std::cout << "\n3. Print"; + std::cout << "\nEnter Your Choice : "; + std::cin >> ch; if (ch == 1) { - cout << "\nInsert : "; - cin >> x; + std::cout << "\nInsert : "; + std::cin >> x; push(x); } else if (ch == 2) { pop(); From a6c3667f308cc27c3b97beafe0123885d38114a2 Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 17:38:17 -0500 Subject: [PATCH 03/13] fix: Release allocated memory --- data_structures/stack_using_array.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/stack_using_array.cpp b/data_structures/stack_using_array.cpp index ab2d7d7dd..6702f2d59 100644 --- a/data_structures/stack_using_array.cpp +++ b/data_structures/stack_using_array.cpp @@ -51,5 +51,7 @@ int main() { } } while (ch != 0); + delete stack; + return 0; } From 957a05bd0cd7e21017e13f57be3998ae1cdd7b73 Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 17:39:29 -0500 Subject: [PATCH 04/13] fix: Convert global variables to local --- math/fibonacci_fast.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/math/fibonacci_fast.cpp b/math/fibonacci_fast.cpp index 80e9108ea..dba8b9dc0 100644 --- a/math/fibonacci_fast.cpp +++ b/math/fibonacci_fast.cpp @@ -19,15 +19,15 @@ #include #include -/** maximum number that can be computed - The result after 93 cannot be stored - * in a `uint64_t` data type. */ -const uint64_t MAX = 93; - -/** Array of computed fibonacci numbers */ -uint64_t numbers[MAX] = {0}; - /** Algorithm */ uint64_t fib(uint64_t n) { + // maximum number that can be computed - The result after 93 cannot be stored + // in a `uint64_t` data type. + static const uint64_t MAX = 93; + + // Array of computed fibonacci numbers */ + static uint64_t numbers[MAX] = {0}; + if (n == 0) return 0; if (n == 1 || n == 2) From cea644bdc31710e1544623934e8ca8646fc9a97d Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 17:52:04 -0500 Subject: [PATCH 05/13] fix: Use delete[] operator --- data_structures/stack_using_array.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stack_using_array.cpp b/data_structures/stack_using_array.cpp index 6702f2d59..b78e08e37 100644 --- a/data_structures/stack_using_array.cpp +++ b/data_structures/stack_using_array.cpp @@ -51,7 +51,7 @@ int main() { } } while (ch != 0); - delete stack; + delete[] stack; return 0; } From da18b9049898dc061e3cb2e5b73f173a90bf19d4 Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 17:53:20 -0500 Subject: [PATCH 06/13] fix: Use #define --- math/fibonacci_fast.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/math/fibonacci_fast.cpp b/math/fibonacci_fast.cpp index dba8b9dc0..fa81f9561 100644 --- a/math/fibonacci_fast.cpp +++ b/math/fibonacci_fast.cpp @@ -19,12 +19,15 @@ #include #include +/** + * maximum number that can be computed - The result after 93 cannot be stored + * in a `uint64_t` data type. + */ + +#define MAX 93 + /** Algorithm */ uint64_t fib(uint64_t n) { - // maximum number that can be computed - The result after 93 cannot be stored - // in a `uint64_t` data type. - static const uint64_t MAX = 93; - // Array of computed fibonacci numbers */ static uint64_t numbers[MAX] = {0}; From 01c52789111c8853db3812a36b139e4670431eed Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 18:22:21 -0500 Subject: [PATCH 07/13] fix: fibonacci_fast.cpp fixes --- math/fibonacci_fast.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/math/fibonacci_fast.cpp b/math/fibonacci_fast.cpp index fa81f9561..be68bab70 100644 --- a/math/fibonacci_fast.cpp +++ b/math/fibonacci_fast.cpp @@ -28,22 +28,20 @@ /** Algorithm */ uint64_t fib(uint64_t n) { - // Array of computed fibonacci numbers */ - static uint64_t numbers[MAX] = {0}; + static uint64_t f1 = 1, f2 = 1; // using static keyword will retain the values of f1 and f2 for the next function call. - if (n == 0) + if (n <= 2) + return f2; + if (n >= 93) { + std::err << "Cannot compute for n>93 due to limit of 64-bit integers\n"; return 0; - if (n == 1 || n == 2) - return (numbers[n] = 1); + } - if (numbers[n]) - return numbers[n]; + uint64_t temp = f2; // we do not need temp to be static + f2 += f1; + f1 = temp; - uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; - - numbers[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) - : (2 * fib(k - 1) + fib(k)) * fib(k); - return numbers[n]; + return f2; } /** Main function */ From 52b9b0bb98ad730822fc07834e2f06afeaebe8f4 Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 18:23:37 -0500 Subject: [PATCH 08/13] docs: Change variable name --- data_structures/stack_using_array.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data_structures/stack_using_array.cpp b/data_structures/stack_using_array.cpp index b78e08e37..22b397ba8 100644 --- a/data_structures/stack_using_array.cpp +++ b/data_structures/stack_using_array.cpp @@ -1,31 +1,31 @@ #include int *stack; -int top_var = 0, stack_size; +int stack_idx = 0, stack_size; void push(int x) { - if (top_var == stack_size) { + if (stack_idx == stack_size) { std::cout << "\nOverflow"; } else { - stack[top_var++] = x; + stack[stack_idx++] = x; } } void pop() { - if (top_var == 0) { + if (stack_idx == 0) { std::cout << "\nUnderflow"; } else { - std::cout << "\n" << stack[--top_var] << " deleted"; + std::cout << "\n" << stack[--stack_idx] << " deleted"; } } void show() { - for (int i = 0; i < top_var; i++) { + for (int i = 0; i < stack_idx; i++) { std::cout << stack[i] << "\n"; } } -void topmost() { std::cout << "\nTopmost element: " << stack[top_var - 1]; } +void topmost() { std::cout << "\nTopmost element: " << stack[stack_idx - 1]; } int main() { std::cout << "\nEnter stack_size of stack : "; std::cin >> stack_size; From f05aadf3b8fddae08331fa127f7e75a6ab44e37a Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 18:26:47 -0500 Subject: [PATCH 09/13] fix: Wrong function name --- math/fibonacci_fast.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/fibonacci_fast.cpp b/math/fibonacci_fast.cpp index be68bab70..e7582df73 100644 --- a/math/fibonacci_fast.cpp +++ b/math/fibonacci_fast.cpp @@ -33,7 +33,7 @@ uint64_t fib(uint64_t n) { if (n <= 2) return f2; if (n >= 93) { - std::err << "Cannot compute for n>93 due to limit of 64-bit integers\n"; + std::cerr << "Cannot compute for n>93 due to limit of 64-bit integers\n"; return 0; } From b6fdaa63eb5f14c1a8d18aef476707baccd72ee6 Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 18:45:56 -0500 Subject: [PATCH 10/13] docs: Change variable names --- data_structures/stack_using_linked_list.cpp | 14 +++++++------- others/paranthesis_matching.cpp | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/data_structures/stack_using_linked_list.cpp b/data_structures/stack_using_linked_list.cpp index 3dcf03f8a..315b4e3b9 100644 --- a/data_structures/stack_using_linked_list.cpp +++ b/data_structures/stack_using_linked_list.cpp @@ -5,28 +5,28 @@ struct node { node *next; }; -node *top_var; +node *stack_idx; void push(int x) { node *n = new node; n->val = x; - n->next = top_var; - top_var = n; + n->next = stack_idx; + stack_idx = n; } void pop() { - if (top_var == NULL) { + if (stack_idx == NULL) { std::cout << "\nUnderflow"; } else { - node *t = top_var; + node *t = stack_idx; std::cout << "\n" << t->val << " deleted"; - top_var = top_var->next; + stack_idx = stack_idx->next; delete t; } } void show() { - node *t = top_var; + node *t = stack_idx; while (t != NULL) { std::cout << t->val << "\n"; t = t->next; diff --git a/others/paranthesis_matching.cpp b/others/paranthesis_matching.cpp index 0a1a9e474..d9c52c813 100644 --- a/others/paranthesis_matching.cpp +++ b/others/paranthesis_matching.cpp @@ -20,13 +20,13 @@ char stack[MAX]; //! pointer to track stack index -int top_var = -1; +int stack_idx = -1; //! push byte to stack variable -void push(char ch) { stack[++top_var] = ch; } +void push(char ch) { stack[++stack_idx] = ch; } //! pop a byte out of stack variable -char pop() { return stack[top_var--]; } +char pop() { return stack[stack_idx--]; } //! @}-------------- end stack ----------- @@ -56,7 +56,7 @@ int main() { while (valid == 1 && i < exp.length()) { if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<') { push(exp[i]); - } else if (top_var >= 0 && stack[top_var] == opening(exp[i])) { + } else if (stack_idx >= 0 && stack[stack_idx] == opening(exp[i])) { pop(); } else { valid = 0; @@ -65,7 +65,7 @@ int main() { } // makes sure the stack is empty after processsing (above) - if (valid == 1 && top_var == -1) { + if (valid == 1 && stack_idx == -1) { std::cout << "\nCorrect Expression"; } else { std::cout << "\nWrong Expression"; From e0fa86816aee2c41d21a2716c0ef913c5ad1ec3e Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Tue, 23 Jun 2020 18:50:14 -0500 Subject: [PATCH 11/13] fix: Variable name --- data_structures/stack_using_linked_list.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data_structures/stack_using_linked_list.cpp b/data_structures/stack_using_linked_list.cpp index 315b4e3b9..3dcf03f8a 100644 --- a/data_structures/stack_using_linked_list.cpp +++ b/data_structures/stack_using_linked_list.cpp @@ -5,28 +5,28 @@ struct node { node *next; }; -node *stack_idx; +node *top_var; void push(int x) { node *n = new node; n->val = x; - n->next = stack_idx; - stack_idx = n; + n->next = top_var; + top_var = n; } void pop() { - if (stack_idx == NULL) { + if (top_var == NULL) { std::cout << "\nUnderflow"; } else { - node *t = stack_idx; + node *t = top_var; std::cout << "\n" << t->val << " deleted"; - stack_idx = stack_idx->next; + top_var = top_var->next; delete t; } } void show() { - node *t = stack_idx; + node *t = top_var; while (t != NULL) { std::cout << t->val << "\n"; t = t->next; From c7ff9d66f1e7c18b43e51d183f1299b25293ac55 Mon Sep 17 00:00:00 2001 From: Panquesito7 Date: Wed, 24 Jun 2020 12:12:30 -0500 Subject: [PATCH 12/13] feat: Add exit option --- data_structures/stack_using_array.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/stack_using_array.cpp b/data_structures/stack_using_array.cpp index 22b397ba8..0c0813d5e 100644 --- a/data_structures/stack_using_array.cpp +++ b/data_structures/stack_using_array.cpp @@ -32,6 +32,7 @@ int main() { stack = new int[stack_size]; int ch, x; do { + std::cout << "\n0. Exit"; std::cout << "\n1. Push"; std::cout << "\n2. Pop"; std::cout << "\n3. Print"; From 4c6b3b86c1f191339770039b8c7f377a5a18fc4e Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 24 Jun 2020 17:14:57 +0000 Subject: [PATCH 13/13] formatting source-code for c7ff9d66f1e7c18b43e51d183f1299b25293ac55 --- math/fibonacci_fast.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/math/fibonacci_fast.cpp b/math/fibonacci_fast.cpp index e7582df73..0948276a0 100644 --- a/math/fibonacci_fast.cpp +++ b/math/fibonacci_fast.cpp @@ -19,7 +19,7 @@ #include #include -/** +/** * maximum number that can be computed - The result after 93 cannot be stored * in a `uint64_t` data type. */ @@ -28,16 +28,19 @@ /** Algorithm */ uint64_t fib(uint64_t n) { - static uint64_t f1 = 1, f2 = 1; // using static keyword will retain the values of f1 and f2 for the next function call. - + static uint64_t f1 = 1, + f2 = 1; // using static keyword will retain the values of + // f1 and f2 for the next function call. + if (n <= 2) return f2; if (n >= 93) { - std::cerr << "Cannot compute for n>93 due to limit of 64-bit integers\n"; + std::cerr + << "Cannot compute for n>93 due to limit of 64-bit integers\n"; return 0; } - uint64_t temp = f2; // we do not need temp to be static + uint64_t temp = f2; // we do not need temp to be static f2 += f1; f1 = temp;