From d222df7dc17167540f2e674b517ef955c5a4e329 Mon Sep 17 00:00:00 2001 From: David Leal Date: Tue, 30 May 2023 10:30:13 -0600 Subject: [PATCH 01/11] [feat/docs]: improve the binary to decimal algorithm (#1263) --- conversions/binary_to_decimal.c | 85 ++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 27 deletions(-) diff --git a/conversions/binary_to_decimal.c b/conversions/binary_to_decimal.c index 3fa5773d..41721e5d 100644 --- a/conversions/binary_to_decimal.c +++ b/conversions/binary_to_decimal.c @@ -1,37 +1,68 @@ /** -* Modified 24/05/2023, Indrranil Pawar -* -* C program that converts a binary number to its decimal equivalent. + * @brief Converts a number from [Binary to Decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal). + * @details + * + * Binary to decimal conversion is a process to convert a number + * having a binary representation to its equivalent decimal representation. + * + * The base of both number systems is different. + * Binary number system is base 2 number system while decimal number system is base 10 number system. + * The numbers used in binary number system are 0 and 1 while decimal number system has numbers from 0 to 9. + * The conversion of binary number to decimal number is done by multiplying + * each digit of the binary number, starting from the rightmost digit, with the power of 2 and adding the result. + * + * @author [Anup Kumar Pawar](https://github.com/AnupKumarPanwar) + * @author [David Leal](https://github.com/Panquesito7) */ -#include +#include /// for IO operations +#include /// for assert +#include /// for pow +#include /// for uint64_t -int main() -{ - int binary_number, decimal_number = 0, temp = 1; +/** + * @brief Converts the given binary number + * to its equivalent decimal number/value. + * @param number The binary number to be converted + * @returns The decimal equivalent of the binary number +*/ +int convert_to_decimal(uint64_t number) { + int decimal_number = 0, i = 0; - // Input the binary number - printf("Enter any binary number: "); - scanf("%d", &binary_number); - - // Convert binary to decimal - while (binary_number > 0) - { - // Extract the rightmost digit of the binary number - int digit = binary_number % 10; - - // Multiply the rightmost digit with the corresponding power of 2 and add to the decimal number - decimal_number += digit * temp; - - // Remove the rightmost digit from the binary number - binary_number /= 10; - - // Increase the power of 2 for the next digit - temp *= 2; + while (number > 0) { + decimal_number += (number % 10) * pow(2, i); + number = number / 10; + i++; } - // Output the decimal equivalent - printf("Decimal equivalent: %d\n", decimal_number); + return decimal_number; +} +/** + * @brief Self-test implementations + * @returns void +*/ +static void tests() { + assert(convert_to_decimal(111) == 7); + assert(convert_to_decimal(101) == 5); + assert(convert_to_decimal(1010) == 10); + assert(convert_to_decimal(1101) == 13); + assert(convert_to_decimal(100001) == 33); + assert(convert_to_decimal(10101001) == 169); + assert(convert_to_decimal(111010) == 58); + assert(convert_to_decimal(100000000) == 256); + assert(convert_to_decimal(10000000000) == 1024); + assert(convert_to_decimal(101110111) == 375); + + printf("All tests have successfully passed!\n"); +} + +/** + * @brief Main function + * @returns 0 on exit +*/ +int main() +{ + tests(); // run self-test implementations return 0; } From 05ff277ebfd5fb8d21481349456454cac0600a6b Mon Sep 17 00:00:00 2001 From: Samuel Pires <98485367+disnoca@users.noreply.github.com> Date: Fri, 2 Jun 2023 17:43:21 +0100 Subject: [PATCH 02/11] feat: add Secant Method (#1255) * feat: add secant method * Apply suggestions from code review Co-authored-by: Sharon "Cass" Cassidy * fixed indentation * added more tests * better clarification for the tolerance parameter * removed redundant comments * chore: apply suggestions from code review --------- Co-authored-by: Sharon "Cass" Cassidy Co-authored-by: scpires Co-authored-by: David Leal --- numerical_methods/secant_method.c | 80 +++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 numerical_methods/secant_method.c diff --git a/numerical_methods/secant_method.c b/numerical_methods/secant_method.c new file mode 100644 index 00000000..94628538 --- /dev/null +++ b/numerical_methods/secant_method.c @@ -0,0 +1,80 @@ +/** + * @file + * @brief [Secant Method](https://en.wikipedia.org/wiki/Secant_method) implementation. Find a + * continuous function's root by using a succession of roots of secant lines to + * approximate it, starting from the given points' secant line. + * @author [Samuel Pires](https://github.com/disnoca) + */ + +#include /// for assert +#include /// for fabs +#include /// for io operations + +#define TOLERANCE 0.0001 // root approximation result tolerance +#define NMAX 100 // maximum number of iterations + +/** + * @brief Continuous function for which we want to find the root + * @param x Real input variable + * @returns The evaluation result of the function using the input value + */ +double func(double x) +{ + return x * x - 3.; // x^2 = 3 - solution is sqrt(3) +} + +/** + * @brief Root-finding method for a continuous function given two points + * @param x0 One of the starting secant points + * @param x1 One of the starting secant points + * @param tolerance Determines how accurate the returned value is. The returned + * value will be within `tolerance` of the actual root + * @returns `root of the function` if secant method succeed within the + * maximum number of iterations + * @returns `-1` if secant method fails + */ +double secant_method(double x0, double x1, double tolerance) +{ + int n = 1; // step counter + + while (n++ < NMAX) + { + // calculate secant line root + double x2 = x1 - func(x1) * (x1 - x0) / (func(x1) - func(x0)); + + // update values + x0 = x1; + x1 = x2; + + // return value if it meets tolerance + if (fabs(x1 - x0) < tolerance) + return x2; + } + + return -1; // method failed (maximum number of steps exceeded) +} + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() +{ + // compares root values found by the secant method within the tolerance + assert(secant_method(0.2, 0.5, TOLERANCE) - sqrt(3) < TOLERANCE); + assert(fabs(secant_method(-2, -5, TOLERANCE)) - sqrt(3) < TOLERANCE); + assert(secant_method(-3, 2, TOLERANCE) - sqrt(3) < TOLERANCE); + assert(fabs(secant_method(1, -1.5, TOLERANCE)) - sqrt(3) < TOLERANCE); + + printf("All tests have successfully passed!\n"); +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() +{ + test(); // run self-test implementations + return 0; +} From 01bc982b9a0439c492c53c40b0daa45b031dbc92 Mon Sep 17 00:00:00 2001 From: David Leal Date: Wed, 7 Jun 2023 21:55:20 -0600 Subject: [PATCH 03/11] feat: label when the build fails (#1254) * feat: label when the build fails * updating DIRECTORY.md --------- Co-authored-by: Taj Co-authored-by: github-actions[bot] --- .github/workflows/awesome_workflow.yml | 18 ++++++++++++++++-- DIRECTORY.md | 1 + 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.github/workflows/awesome_workflow.yml b/.github/workflows/awesome_workflow.yml index 50a15fc5..968aff2d 100644 --- a/.github/workflows/awesome_workflow.yml +++ b/.github/workflows/awesome_workflow.yml @@ -92,6 +92,8 @@ jobs: build: name: Compile checks runs-on: ${{ matrix.os }} + permissions: + pull-requests: write needs: [MainSequence] strategy: matrix: @@ -100,5 +102,17 @@ jobs: - uses: actions/checkout@v3 with: submodules: true - - run: cmake -B ./build -S . - - run: cmake --build build + - run: | + cmake -B ./build -S . + cmake --build build + - name: Label on PR fail + uses: actions/github-script@v6 + if: ${{ failure() && matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request' }} + with: + script: | + github.rest.issues.addLabels({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels: ['Autochecks are failing'] + }) diff --git a/DIRECTORY.md b/DIRECTORY.md index 8eb1c38a..fc4a3c47 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -238,6 +238,7 @@ * [Qr Decomposition](https://github.com/TheAlgorithms/C/blob/HEAD/numerical_methods/qr_decomposition.c) * [Qr Eigen Values](https://github.com/TheAlgorithms/C/blob/HEAD/numerical_methods/qr_eigen_values.c) * [Realtime Stats](https://github.com/TheAlgorithms/C/blob/HEAD/numerical_methods/realtime_stats.c) + * [Secant Method](https://github.com/TheAlgorithms/C/blob/HEAD/numerical_methods/secant_method.c) * [Simpsons 1 3Rd Rule](https://github.com/TheAlgorithms/C/blob/HEAD/numerical_methods/simpsons_1_3rd_rule.c) * [Variance](https://github.com/TheAlgorithms/C/blob/HEAD/numerical_methods/variance.c) From e278f5d74fe0ad4037f3e65272f5ab1c3026a7b2 Mon Sep 17 00:00:00 2001 From: Sahil Kandhare Date: Thu, 8 Jun 2023 20:26:26 +0530 Subject: [PATCH 04/11] feat: add Dynamic Stack implementation (#1261) * Create dynamic_stack.c In this implementation, functions such as PUSH, POP, PEEK, show_capacity, isempty, and stack_size are coded to implement dynamic stack. * Update dynamic_stack.c Worked on Suggested Changes. * Update dynamic_stack.c Worked on suggested changes. * Update dynamic_stack.c * Update: Used Int type that are OS-independent --------- Co-authored-by: David Leal --- data_structures/stack/dynamic_stack.c | 250 ++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 data_structures/stack/dynamic_stack.c diff --git a/data_structures/stack/dynamic_stack.c b/data_structures/stack/dynamic_stack.c new file mode 100644 index 00000000..482896ea --- /dev/null +++ b/data_structures/stack/dynamic_stack.c @@ -0,0 +1,250 @@ +/** + * @file + * + * @brief + * Dynamic [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)), + * just like Dynamic Array, is a stack data structure whose the length or + * capacity (maximum number of elements that can be stored) increases or + * decreases in real time based on the operations (like insertion or deletion) + * performed on it. + * + * In this implementation, functions such as PUSH, POP, PEEK, show_capacity, + * isempty, and stack_size are coded to implement dynamic stack. + * + * @author [SahilK-027](https://github.com/SahilK-027) + * + */ +#include /// to verify assumptions made by the program and print a diagnostic message if this assumption is false. +#include /// to provide a set of integer types with universally consistent definitions that are operating system-independent +#include /// for IO operations +#include /// for including functions involving memory allocation such as `malloc` +/** + * @brief DArrayStack Structure of stack. + */ +typedef struct DArrayStack +{ + int capacity, top; ///< to store capacity and top of the stack + int *arrPtr; ///< array pointer +} DArrayStack; + +/** + * @brief Create a Stack object + * + * @param cap Capacity of stack + * @return DArrayStack* Newly created stack object pointer + */ +DArrayStack *create_stack(int cap) +{ + DArrayStack *ptr; + ptr = (DArrayStack *)malloc(sizeof(DArrayStack)); + ptr->capacity = cap; + ptr->top = -1; + ptr->arrPtr = (int *)malloc(sizeof(int) * cap); + printf("\nStack of capacity %d is successfully created.\n", ptr->capacity); + return (ptr); +} + +/** + * @brief As this is stack implementation using dynamic array this function will + * expand the size of the stack by twice as soon as the stack is full. + * + * @param ptr Stack pointer + * @param cap Capacity of stack + * @return DArrayStack*: Modified stack + */ +DArrayStack *double_array(DArrayStack *ptr, int cap) +{ + int newCap = 2 * cap; + int *temp; + temp = (int *)malloc(sizeof(int) * newCap); + for (int i = 0; i < (ptr->top) + 1; i++) + { + temp[i] = ptr->arrPtr[i]; + } + free(ptr->arrPtr); + ptr->arrPtr = temp; + ptr->capacity = newCap; + return ptr; +} + +/** + * @brief As this is stack implementation using dynamic array this function will + * shrink the size of stack by twice as soon as the stack's capacity and size + * has significant difference. + * + * @param ptr Stack pointer + * @param cap Capacity of stack + * @return DArrayStack*: Modified stack + */ +DArrayStack *shrink_array(DArrayStack *ptr, int cap) +{ + int newCap = cap / 2; + int *temp; + temp = (int *)malloc(sizeof(int) * newCap); + for (int i = 0; i < (ptr->top) + 1; i++) + { + temp[i] = ptr->arrPtr[i]; + } + free(ptr->arrPtr); + ptr->arrPtr = temp; + ptr->capacity = newCap; + return ptr; +} + +/** + * @brief The push function pushes the element onto the stack. + * + * @param ptr Stack pointer + * @param data Value to be pushed onto stack + * @return int Position of top pointer + */ +int push(DArrayStack *ptr, int data) +{ + if (ptr->top == (ptr->capacity) - 1) + { + ptr = double_array(ptr, ptr->capacity); + ptr->top++; + ptr->arrPtr[ptr->top] = data; + } + else + { + ptr->top++; + ptr->arrPtr[ptr->top] = data; + } + printf("Successfully pushed : %d\n", data); + return ptr->top; +} + +/** + * @brief The pop function to pop an element from the stack. + * + * @param ptr Stack pointer + * @return int Popped value + */ +int pop(DArrayStack *ptr) +{ + if (ptr->top == -1) + { + printf("Stack is empty UNDERFLOW \n"); + return -1; + } + int ele = ptr->arrPtr[ptr->top]; + ptr->arrPtr[ptr->top] = 0; + ptr->top = (ptr->top - 1); + if ((ptr->capacity) % 2 == 0) + { + if (ptr->top <= (ptr->capacity / 2) - 1) + { + ptr = shrink_array(ptr, ptr->capacity); + } + } + printf("Successfully popped: %d\n", ele); + return ele; +} + +/** + * @brief To retrieve or fetch the first element of the Stack or the element + * present at the top of the Stack. + * + * @param ptr Stack pointer + * @return int Top of the stack + */ +int peek(DArrayStack *ptr) +{ + if (ptr->top == -1) + { + printf("Stack is empty UNDERFLOW \n"); + return -1; + } + return ptr->arrPtr[ptr->top]; +} + +/** + * @brief To display the current capacity of the stack. + * + * @param ptr Stack pointer + * @return int Current capacity of the stack + */ +int show_capacity(DArrayStack *ptr) { return ptr->capacity; } + +/** + * @brief The function is used to check whether the stack is empty or not and + * return true or false accordingly. + * + * @param ptr Stack pointer + * @return int returns 1 -> true OR returns 0 -> false + */ +int isempty(DArrayStack *ptr) +{ + if (ptr->top == -1) + { + return 1; + } + return 0; +} + +/** + * @brief Used to get the size of the Stack or the number of elements present in + * the Stack. + * + * @param ptr Stack pointer + * @return int size of stack + */ +int stack_size(DArrayStack *ptr) { return ptr->top + 1; } + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() +{ + DArrayStack *NewStack; + int capacity = 1; + NewStack = create_stack(capacity); + uint64_t arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + + printf("\nTesting Empty stack: "); + assert(stack_size(NewStack) == 0); + assert(isempty(NewStack) == 1); + printf("Size of an empty stack is %d\n", stack_size(NewStack)); + + printf("\nTesting PUSH operation:\n"); + for (int i = 0; i < 12; ++i) + { + int topVal = push(NewStack, arr[i]); + printf("Size: %d, Capacity: %d\n\n", stack_size(NewStack), + show_capacity(NewStack)); + assert(topVal == i); + assert(peek(NewStack) == arr[i]); + assert(stack_size(NewStack) == i + 1); + assert(isempty(NewStack) == 0); + } + + printf("\nTesting POP operation:\n"); + for (int i = 11; i > -1; --i) + { + peek(NewStack); + assert(peek(NewStack) == arr[i]); + int ele = pop(NewStack); + assert(ele == arr[i]); + assert(stack_size(NewStack) == i); + } + + printf("\nTesting Empty stack size: "); + assert(stack_size(NewStack) == 0); + assert(isempty(NewStack) == 1); + printf("Size of an empty stack is %d\n", stack_size(NewStack)); + + printf("\nTesting POP operation on empty stack: "); + assert(pop(NewStack) == -1); +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() +{ + test(); // run self-test implementations + return 0; +} From b1a8da69a87cd45e9db0cc340730e4a5e3b36201 Mon Sep 17 00:00:00 2001 From: David Leal Date: Fri, 9 Jun 2023 10:51:37 -0600 Subject: [PATCH 05/11] fix: missing `;` in `matrix_chain_order.c` --- dynamic_programming/matrix_chain_order.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/matrix_chain_order.c b/dynamic_programming/matrix_chain_order.c index 76c29bf1..d0ee8cac 100644 --- a/dynamic_programming/matrix_chain_order.c +++ b/dynamic_programming/matrix_chain_order.c @@ -55,7 +55,7 @@ int matrixChainOrder(int l,const int *p, int *s) { void printSolution(int l,int *s,int i,int j) { if(i == j) { printf("A%d",i); - return + return; } putchar('('); printSolution(l,s,i,s[i * l + j]); From 2e8374e5c14d7106442833a20428dc2843982436 Mon Sep 17 00:00:00 2001 From: David Leal Date: Tue, 13 Jun 2023 10:13:34 -0600 Subject: [PATCH 06/11] chore: improve the LeetCode directory writer (#1276) * chore: free-dependency LeetCode directory writer * updating DIRECTORY.md --------- Co-authored-by: github-actions[bot] --- .../workflows/leetcode_directory_writer.yml | 23 +++++++++++-------- DIRECTORY.md | 1 + 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.github/workflows/leetcode_directory_writer.yml b/.github/workflows/leetcode_directory_writer.yml index e85c879b..e6d666ce 100644 --- a/.github/workflows/leetcode_directory_writer.yml +++ b/.github/workflows/leetcode_directory_writer.yml @@ -7,6 +7,7 @@ on: - "leetcode/src/**.c" jobs: build: + if: github.repository == 'TheAlgorithms/C' # We only need this to run in our repository. runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -21,17 +22,21 @@ jobs: - name: Write LeetCode DIRECTORY.md run: | python3 scripts/leetcode_directory_md.py 2>&1 | tee leetcode/DIRECTORY.md - - name: Commit and push changes - uses: stefanzweifel/git-auto-commit-action@v4 - id: commit-push - with: - commit_message: "docs: updating `leetcode/DIRECTORY.md`" - branch: "leetcode-directory-${{ github.sha }}" - create_branch: true - - name: Creating and merging the PR + - name: Setup Git configurations shell: bash - if: steps.commit-push.outputs.changes_detected == 'true' run: | + git config --global user.name github-actions[bot] + git config --global user.email 'github-actions@users.noreply.github.com' + - name: Committing changes + shell: bash + run: | + git checkout -b leetcode-directory-${{ github.sha }} + git commit -m "docs: updating `leetcode/DIRECTORY.md` + git push origin leetcode-directory-${{ github.sha }}:leetcode-directory-${{ github.sha }} + - name: Creating the pull request + shell: bash + run: | + if [[ `git status --porcelain` ]]; then gh pr create --base ${GITHUB_REF##*/} --head leetcode-directory-${{ github.sha }} --title 'docs: updating `leetcode/DIRECTORY.md`' --body 'Updated LeetCode directory (see the diff. for changes).' env: GH_TOKEN: ${{ github.token }} diff --git a/DIRECTORY.md b/DIRECTORY.md index fc4a3c47..1493a480 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -107,6 +107,7 @@ * [Queue](https://github.com/TheAlgorithms/C/blob/HEAD/data_structures/queue/queue.c) * [Stack](https://github.com/TheAlgorithms/C/blob/HEAD/data_structures/stack.c) * Stack + * [Dynamic Stack](https://github.com/TheAlgorithms/C/blob/HEAD/data_structures/stack/dynamic_stack.c) * [Main](https://github.com/TheAlgorithms/C/blob/HEAD/data_structures/stack/main.c) * [Parenthesis](https://github.com/TheAlgorithms/C/blob/HEAD/data_structures/stack/parenthesis.c) * [Stack](https://github.com/TheAlgorithms/C/blob/HEAD/data_structures/stack/stack.c) From b6b01a360549b5bd131d171639204aee7b524fd8 Mon Sep 17 00:00:00 2001 From: David Leal Date: Tue, 13 Jun 2023 10:45:46 -0600 Subject: [PATCH 07/11] feat: add `dynamic_programming` to CMake lists (#1266) * feat: add `dynamic_programming` to CMake lists * updating DIRECTORY.md * chore: fix minor doc. issues and indentation --------- Co-authored-by: github-actions[bot] --- CMakeLists.txt | 1 + dynamic_programming/CMakeLists.txt | 18 +++++++++ dynamic_programming/lcs.c | 64 ++++++++++++++++++------------ 3 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 dynamic_programming/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index eb925dc9..29fb9d20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,7 @@ add_subdirectory(process_scheduling_algorithms) add_subdirectory(numerical_methods) add_subdirectory(math) add_subdirectory(cipher) +add_subdirectory(dynamic_programming) ## Configure Doxygen documentation system cmake_policy(SET CMP0054 NEW) diff --git a/dynamic_programming/CMakeLists.txt b/dynamic_programming/CMakeLists.txt new file mode 100644 index 00000000..a65bbb7d --- /dev/null +++ b/dynamic_programming/CMakeLists.txt @@ -0,0 +1,18 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. The RELATIVE flag makes it easier to extract an executable's name +# automatically. + +file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c ) +foreach( testsourcefile ${APP_SOURCES} ) + string( REPLACE ".c" "" testname ${testsourcefile} ) # File type. Example: `.c` + add_executable( ${testname} ${testsourcefile} ) + + if(OpenMP_C_FOUND) + target_link_libraries(${testname} OpenMP::OpenMP_C) + endif() + if(MATH_LIBRARY) + target_link_libraries(${testname} ${MATH_LIBRARY}) + endif() + install(TARGETS ${testname} DESTINATION "bin/dynamic_programming") # Folder name. Do NOT include `<>` + +endforeach( testsourcefile ${APP_SOURCES} ) diff --git a/dynamic_programming/lcs.c b/dynamic_programming/lcs.c index 5637a72d..bf360b73 100644 --- a/dynamic_programming/lcs.c +++ b/dynamic_programming/lcs.c @@ -1,12 +1,15 @@ /** * @file - * @brief [Longest Common Subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) algorithm + * @brief [Longest Common + * Subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) + * algorithm * @details * From Wikipedia: The longest common subsequence (LCS) problem is the problem - * of finding the longest subsequence common to all sequences in a set of sequences - * (often just two sequences). + * of finding the longest subsequence common to all sequences in a set of + * sequences (often just two sequences). * @author [Kurtz](https://github.com/itskurtz) */ + #include /* for io operations */ #include /* for memory management & exit */ #include /* for string manipulation & ooperations */ @@ -15,13 +18,13 @@ enum {LEFT, UP, DIAG}; /** - * @breif Computes LCS between s1 and s2 using a dynamic-programming approach - * @param1 s1 first null-terminated string - * @param2 s2 second null-terminated string - * @param3 l1 length of s1 - * @param4 l2 length of s2 - * @param5 L matrix of size l1 x l2 - * @param6 B matrix of size l1 x l2 + * @brief Computes LCS between s1 and s2 using a dynamic-programming approach + * @param s1 first null-terminated string + * @param s2 second null-terminated string + * @param l1 length of s1 + * @param l2 length of s2 + * @param L matrix of size l1 x l2 + * @param B matrix of size l1 x l2 * @returns void */ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) { @@ -31,8 +34,8 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) { /* loop over the simbols in my sequences save the directions according to the LCS */ - for (i = 1; i <= l1; ++i) - for (j = 1; j <= l2; ++j) + for (i = 1; i <= l1; ++i) { + for (j = 1; j <= l2; ++j) { if (s1[i-1] == s2[j-1]) { L[i][j] = 1 + L[i-1][j-1]; B[i][j] = DIAG; @@ -44,16 +47,18 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) { else { L[i][j] = L[i-1][j]; B[i][j] = UP; - } + } + } + } } /** - * @breif Builds the LCS according to B using a traceback approach - * @param1 s1 first null-terminated string - * @param2 l1 length of s1 - * @param3 l2 length of s2 - * @param4 L matrix of size l1 x l2 - * @param5 B matrix of size l1 x l2 + * @brief Builds the LCS according to B using a traceback approach + * @param s1 first null-terminated string + * @param l1 length of s1 + * @param l2 length of s2 + * @param L matrix of size l1 x l2 + * @param B matrix of size l1 x l2 * @returns lcs longest common subsequence */ char *lcsbuild(const char *s1, int l1, int l2, int **L, int **B) { @@ -76,13 +81,18 @@ char *lcsbuild(const char *s1, int l1, int l2, int **L, int **B) { i = i - 1; j = j - 1; } - else if (B[i][j] == LEFT) - j = j - 1; - else - i = i - 1; + else if (B[i][j] == LEFT) + { + j = j - 1; + } + else + { + i = i - 1; + } } return lcs; } + /** * @brief Self-test implementations * @returns void @@ -132,9 +142,11 @@ static void test() { printf("LCS len:%3d\n", L[l1][l2]); printf("LCS: %s\n", lcs); - free(lcs); - for (j = 0; j <= l1; j++) - free(L[j]), free(B[j]); + free(lcs); + for (j = 0; j <= l1; j++) + { + free(L[j]), free(B[j]); + } free(L); free(B); From 8a3ff966e74f61b6ce75e174424a6939fb0451a7 Mon Sep 17 00:00:00 2001 From: David Leal Date: Wed, 14 Jun 2023 10:18:38 -0600 Subject: [PATCH 08/11] chore: run LeetCode directory writer on `main` only --- .github/workflows/leetcode_directory_writer.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/leetcode_directory_writer.yml b/.github/workflows/leetcode_directory_writer.yml index e6d666ce..8758df4f 100644 --- a/.github/workflows/leetcode_directory_writer.yml +++ b/.github/workflows/leetcode_directory_writer.yml @@ -5,6 +5,8 @@ on: push: paths: - "leetcode/src/**.c" + branches: + - main jobs: build: if: github.repository == 'TheAlgorithms/C' # We only need this to run in our repository. @@ -37,6 +39,7 @@ jobs: shell: bash run: | if [[ `git status --porcelain` ]]; then - gh pr create --base ${GITHUB_REF##*/} --head leetcode-directory-${{ github.sha }} --title 'docs: updating `leetcode/DIRECTORY.md`' --body 'Updated LeetCode directory (see the diff. for changes).' + gh pr create --base ${GITHUB_REF##*/} --head leetcode-directory-${{ github.sha }} --title 'docs: updating `leetcode/DIRECTORY.md`' --body 'Updated LeetCode directory (see the diff. for changes).' + fi env: GH_TOKEN: ${{ github.token }} From 8a1a4972a58ea279162626cc66dc6b796a0423e5 Mon Sep 17 00:00:00 2001 From: Ethan Fredsti Date: Tue, 20 Jun 2023 14:07:09 -0700 Subject: [PATCH 09/11] fix: change list length in `data_structures/list/list.c` (#1265) I changed the return value of n in List_length to reflect the number of items inside the list, so a newly initialized list will return a length of 0. To prevent items in List_toArray from being cut off, I addeone back to n at the beginning of the List_toArray function. --- data_structures/list/list.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/list/list.c b/data_structures/list/list.c index 9731acf0..762ffdfa 100644 --- a/data_structures/list/list.c +++ b/data_structures/list/list.c @@ -30,13 +30,13 @@ int List_length(L list) { int n; for (n = 0; list; list = list->next) n++; - return n; + return n - 1; } /* Convert list to array */ void **List_toArray(L list) { - int i, n = List_length(list); + int i, n = List_length(list) + 1; void **array = (void **)malloc((n + 1) * sizeof(*array)); for (i = 0; i < n; i++) From f4ee5743afc648cfbd65cc6add1b7d7f983bd8c4 Mon Sep 17 00:00:00 2001 From: David Leal Date: Fri, 23 Jun 2023 12:04:53 -0600 Subject: [PATCH 10/11] fix: use correct branch name --- .github/workflows/leetcode_directory_writer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/leetcode_directory_writer.yml b/.github/workflows/leetcode_directory_writer.yml index 8758df4f..a1189165 100644 --- a/.github/workflows/leetcode_directory_writer.yml +++ b/.github/workflows/leetcode_directory_writer.yml @@ -6,7 +6,7 @@ on: paths: - "leetcode/src/**.c" branches: - - main + - master jobs: build: if: github.repository == 'TheAlgorithms/C' # We only need this to run in our repository. From db69d0a539d2248c61ce2849dd12f9487d998d64 Mon Sep 17 00:00:00 2001 From: hemanth8819 <119241921+hemanth8819@users.noreply.github.com> Date: Wed, 28 Jun 2023 01:19:43 +0530 Subject: [PATCH 11/11] feat: add LeetCode problem 69 (#1259) * feat: add LeetCode problem 69 Here is the code for the problem 69 of leetcode as there are many ways to do it we programmers need to find the most optimal way to solve a problem statement so i used binary-search approach inorder to solve it. All suggestions are accepted!!! * Update 69.c I have updated the solution according to the suggestions. * Update 69.c * Update 69.c --- leetcode/src/69.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 leetcode/src/69.c diff --git a/leetcode/src/69.c b/leetcode/src/69.c new file mode 100644 index 00000000..63c0d025 --- /dev/null +++ b/leetcode/src/69.c @@ -0,0 +1,23 @@ +//using the binary search method is one of the efficient ones for this problem statement. +int mySqrt(int x){ +int start=0; + int end=x; + long long int ans=0; + while(start <= end){ + long long int mid=(start+end)/2; + long long int val=mid*mid; + if( val == x){ + return mid; + } +//if mid is less than the square root of the number(x) store the value of mid in ans. + if( val < x){ + ans = mid; + start = mid+1; + } +//if mid is greater than the square root of the number(x) then ssign the value mid-1 to end. + if( val > x){ + end = mid-1; + } + } + return ans; +}