From 9621751a4d843c1dc274ffead54f55e212dba2ba Mon Sep 17 00:00:00 2001 From: Divlo Date: Mon, 8 Nov 2021 16:56:46 +0100 Subject: [PATCH] feat(solutions): add `heap-algorithm/c/function` --- .../solutions/c/function/README.md | 3 ++ .../solutions/c/function/character.c | 10 +++++ .../solutions/c/function/character.h | 13 ++++++ .../solutions/c/function/input.c | 19 ++++++++ .../solutions/c/function/input.h | 11 +++++ .../solutions/c/function/solution.c | 45 +++++++++++++++++++ 6 files changed, 101 insertions(+) create mode 100644 challenges/heap-algorithm/solutions/c/function/README.md create mode 100644 challenges/heap-algorithm/solutions/c/function/character.c create mode 100644 challenges/heap-algorithm/solutions/c/function/character.h create mode 100644 challenges/heap-algorithm/solutions/c/function/input.c create mode 100644 challenges/heap-algorithm/solutions/c/function/input.h create mode 100644 challenges/heap-algorithm/solutions/c/function/solution.c diff --git a/challenges/heap-algorithm/solutions/c/function/README.md b/challenges/heap-algorithm/solutions/c/function/README.md new file mode 100644 index 0000000..1a28ad6 --- /dev/null +++ b/challenges/heap-algorithm/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# heap-algorithm/c/function + +Created by [@Divlo](https://github.com/Divlo) on 8 November 2021. diff --git a/challenges/heap-algorithm/solutions/c/function/character.c b/challenges/heap-algorithm/solutions/c/function/character.c new file mode 100644 index 0000000..49d4f00 --- /dev/null +++ b/challenges/heap-algorithm/solutions/c/function/character.c @@ -0,0 +1,10 @@ +#include "character.h" + +#include +#include + +void character_append(char* string, char character) { + size_t length = strlen(string); + string[length] = character; + string[length + 1] = '\0'; +} diff --git a/challenges/heap-algorithm/solutions/c/function/character.h b/challenges/heap-algorithm/solutions/c/function/character.h new file mode 100644 index 0000000..0fab88f --- /dev/null +++ b/challenges/heap-algorithm/solutions/c/function/character.h @@ -0,0 +1,13 @@ +#ifndef __CHARACTER__ +#define __CHARACTER__ + +/** + * @brief Append a character to a string, assuming string points to an array + * with enough space. + * + * @param string + * @param character + */ +void character_append(char* string, char character); + +#endif diff --git a/challenges/heap-algorithm/solutions/c/function/input.c b/challenges/heap-algorithm/solutions/c/function/input.c new file mode 100644 index 0000000..c07bb6d --- /dev/null +++ b/challenges/heap-algorithm/solutions/c/function/input.c @@ -0,0 +1,19 @@ +#include "input.h" + +#include +#include + +#include "character.h" + +char* input() { + char character; + size_t length = 1; + char* string = malloc(length * sizeof(char)); + *string = '\0'; + while ((character = getchar()) != '\n' && character != EOF) { + length++; + string = realloc(string, length * sizeof(char)); + character_append(string, character); + } + return string; +} diff --git a/challenges/heap-algorithm/solutions/c/function/input.h b/challenges/heap-algorithm/solutions/c/function/input.h new file mode 100644 index 0000000..d432398 --- /dev/null +++ b/challenges/heap-algorithm/solutions/c/function/input.h @@ -0,0 +1,11 @@ +#ifndef __INPUT__ +#define __INPUT__ + +/** + * @brief Read a line from stdin. + * + * @return char* + */ +char* input(); + +#endif diff --git a/challenges/heap-algorithm/solutions/c/function/solution.c b/challenges/heap-algorithm/solutions/c/function/solution.c new file mode 100644 index 0000000..8d5bafb --- /dev/null +++ b/challenges/heap-algorithm/solutions/c/function/solution.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +#include "input.h" + +char *swap(char *string, size_t index_from, size_t index_target) { + size_t string_length = strlen(string); + char *result = malloc(sizeof(char *) * (string_length)); + for (size_t index = 0; index < string_length; index++) { + if (index == index_from) { + result[index] = string[index_target]; + } else if (index == index_target) { + result[index] = string[index_from]; + } else { + result[index] = string[index]; + } + } + return result; +} + +void heap_algorithm(unsigned long number_of_elements_to_operate, char *string) { + if (number_of_elements_to_operate == 1) { + printf("%s\n", string); + } else { + heap_algorithm(number_of_elements_to_operate - 1, string); + for (size_t index = 0; index < number_of_elements_to_operate - 1; index++) { + bool is_even = number_of_elements_to_operate % 2 == 0; + if (!is_even) { + string = swap(string, index, number_of_elements_to_operate - 1); + } else { + string = swap(string, 0, number_of_elements_to_operate - 1); + } + heap_algorithm(number_of_elements_to_operate - 1, string); + } + } +} + +int main() { + char *string = input(); + size_t string_length = strlen(string); + heap_algorithm(string_length, string); + return EXIT_SUCCESS; +}