From e8d3811f129a7253136a6704ffbf02bc7c895caa Mon Sep 17 00:00:00 2001 From: Aybars Nazlica Date: Wed, 1 Feb 2023 04:24:50 +0900 Subject: [PATCH] feat: add hamming distance (#1200) * feat: add hamming distance * Update misc/hamming_distance.c Co-authored-by: David Leal * Update misc/hamming_distance.c Co-authored-by: David Leal * updating DIRECTORY.md * Add curly braces to the while loop * Fix character comparison * Add a one-line description for library/header * Update misc/hamming_distance.c Co-authored-by: Taj * Fix function names in test --------- Co-authored-by: David Leal Co-authored-by: github-actions[bot] Co-authored-by: Taj --- DIRECTORY.md | 3 ++ misc/hamming_distance.c | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 misc/hamming_distance.c diff --git a/DIRECTORY.md b/DIRECTORY.md index aa6a378e..3fbbde13 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -188,6 +188,7 @@ * [12](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/12.c) * [1207](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1207.c) * [121](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/121.c) + * [124](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/124.c) * [125](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/125.c) * [1283](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1283.c) * [13](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/13.c) @@ -263,6 +264,7 @@ * [461](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/461.c) * [476](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/476.c) * [485](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/485.c) + * [50](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/50.c) * [509](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/509.c) * [520](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/520.c) * [53](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/53.c) @@ -335,6 +337,7 @@ ## Misc * [Demonetization](https://github.com/TheAlgorithms/C/blob/HEAD/misc/demonetization.c) + * [Hamming Distance](https://github.com/TheAlgorithms/C/blob/HEAD/misc/hamming_distance.c) * [Lexicographic Permutations](https://github.com/TheAlgorithms/C/blob/HEAD/misc/lexicographic_permutations.c) * [Longest Subsequence](https://github.com/TheAlgorithms/C/blob/HEAD/misc/longest_subsequence.c) * [Mirror](https://github.com/TheAlgorithms/C/blob/HEAD/misc/mirror.c) diff --git a/misc/hamming_distance.c b/misc/hamming_distance.c new file mode 100644 index 00000000..e479bf14 --- /dev/null +++ b/misc/hamming_distance.c @@ -0,0 +1,62 @@ +/** + * @file + * @brief [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) + * algorithm implementation. + * @details + * In information theory, the Hamming distance between two strings of + * equal length is the number of positions at which the corresponding symbols + * are different. + * @author [Aybars Nazlica](https://github.com/aybarsnazlica) + */ + +#include /// for assert +#include /// for IO operations + +/** + * @brief Function to calculate the Hamming distance between two strings + * @param param1 string 1 + * @param param2 string 2 + * @returns Hamming distance + */ +int hamming_distance(char* str1, char* str2) +{ + int i = 0, distance = 0; + + while (str1[i] != '\0') + { + if (str1[i] != str2[i]) + { + distance++; + } + i++; + } + + return distance; +} + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() +{ + char str1[] = "karolin"; + char str2[] = "kathrin"; + + assert(hamming_distance(str1, str2) == 3); + + char str3[] = "00000"; + char str4[] = "11111"; + + assert(hamming_distance(str3, str4) == 5); + printf("All tests have successfully passed!\n"); +} +/** + * @brief Main function + * @returns 0 on exit + */ +int main() +{ + test(); // run self-test implementations + return 0; +}