From 6d30c3defeed55543c6d2b44cab1cb9eb1598274 Mon Sep 17 00:00:00 2001 From: Divlo Date: Tue, 30 Nov 2021 16:42:54 +0100 Subject: [PATCH] feat(solutions): add `look-and-say-sequence-conway/c/function` --- .../solutions/c/function/README.md | 3 ++ .../solutions/c/function/character.c | 17 +++++++++++ .../solutions/c/function/character.h | 22 +++++++++++++++ .../solutions/c/function/input.c | 19 +++++++++++++ .../solutions/c/function/input.h | 11 ++++++++ .../solutions/c/function/solution.c | 28 +++++++++++++++++++ .../solutions/cs/function/Solution.cs | 2 +- 7 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 challenges/look-and-say-sequence-conway/solutions/c/function/README.md create mode 100644 challenges/look-and-say-sequence-conway/solutions/c/function/character.c create mode 100644 challenges/look-and-say-sequence-conway/solutions/c/function/character.h create mode 100644 challenges/look-and-say-sequence-conway/solutions/c/function/input.c create mode 100644 challenges/look-and-say-sequence-conway/solutions/c/function/input.h create mode 100644 challenges/look-and-say-sequence-conway/solutions/c/function/solution.c diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/README.md b/challenges/look-and-say-sequence-conway/solutions/c/function/README.md new file mode 100644 index 0000000..157c49f --- /dev/null +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# look-and-say-sequence-conway/c/function + +Created by [@Divlo](https://github.com/Divlo) on 30 November 2021. diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/character.c b/challenges/look-and-say-sequence-conway/solutions/c/function/character.c new file mode 100644 index 0000000..d0fdd2f --- /dev/null +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/character.c @@ -0,0 +1,17 @@ +#include "character.h" + +#include +#include + +void character_append(char* string, char character) { + size_t length = strlen(string); + string[length] = character; + string[length + 1] = '\0'; +} + +void character_append_many(char* string, char* characters) { + size_t characters_length = strlen(characters); + for (size_t index = 0; index < characters_length; index++) { + character_append(string, characters[index]); + } +} diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/character.h b/challenges/look-and-say-sequence-conway/solutions/c/function/character.h new file mode 100644 index 0000000..70bf124 --- /dev/null +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/character.h @@ -0,0 +1,22 @@ +#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); + +/** + * @brief Append many characters to a string, assuming string points to an array + * with enough space. + * + * @param string + * @param characters + */ +void character_append_many(char* string, char* characters); + +#endif diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/input.c b/challenges/look-and-say-sequence-conway/solutions/c/function/input.c new file mode 100644 index 0000000..c07bb6d --- /dev/null +++ b/challenges/look-and-say-sequence-conway/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/look-and-say-sequence-conway/solutions/c/function/input.h b/challenges/look-and-say-sequence-conway/solutions/c/function/input.h new file mode 100644 index 0000000..d432398 --- /dev/null +++ b/challenges/look-and-say-sequence-conway/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/look-and-say-sequence-conway/solutions/c/function/solution.c b/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c new file mode 100644 index 0000000..640f6d7 --- /dev/null +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c @@ -0,0 +1,28 @@ +#include +#include +#include + +#include "character.h" +#include "input.h" + +int main() { + char* string = input(); + size_t string_length = strlen(string); + char* result = malloc(sizeof(char*) * (string_length + 1)); + for (size_t index = 0; index < string_length; index++) { + size_t number_of_appearances = 0; + char value_to_search = string[index]; + size_t iteration = index; + while (iteration < string_length && string[iteration] == value_to_search) { + number_of_appearances++; + iteration++; + } + char* number_of_appearances_string = malloc(sizeof(char*) * (string_length + 1)); + snprintf(number_of_appearances_string, sizeof(result), "%zu", number_of_appearances); + character_append_many(result, number_of_appearances_string); + character_append(result, value_to_search); + index += number_of_appearances - 1; + } + printf("%s\n", result); + return EXIT_SUCCESS; +} diff --git a/challenges/look-and-say-sequence-conway/solutions/cs/function/Solution.cs b/challenges/look-and-say-sequence-conway/solutions/cs/function/Solution.cs index 0139b3d..19c80da 100644 --- a/challenges/look-and-say-sequence-conway/solutions/cs/function/Solution.cs +++ b/challenges/look-and-say-sequence-conway/solutions/cs/function/Solution.cs @@ -18,7 +18,7 @@ namespace Solution numberOfAppearances += 1; iteration++; } - result = result + numberOfAppearances.ToString() + line[index]; + result = result + numberOfAppearances.ToString() + valueToSearch; index += numberOfAppearances - 1; } Console.WriteLine(result);