From 9ae0cadb72cada476af330a06e4516cdc25e6a54 Mon Sep 17 00:00:00 2001 From: Divlo Date: Thu, 30 Sep 2021 18:56:27 +0200 Subject: [PATCH] feat(solutions): add `find-outlier-number/c/function` --- .../solutions/c/function/README.md | 3 ++ .../solutions/c/function/solution.c | 47 +++++++++++++++++++ .../solutions/typescript/function/README.md | 3 -- .../solutions/typescript/function/solution.ts | 35 -------------- 4 files changed, 50 insertions(+), 38 deletions(-) create mode 100644 challenges/find-outlier-number/solutions/c/function/README.md create mode 100644 challenges/find-outlier-number/solutions/c/function/solution.c delete mode 100644 challenges/find-outlier-number/solutions/typescript/function/README.md delete mode 100644 challenges/find-outlier-number/solutions/typescript/function/solution.ts diff --git a/challenges/find-outlier-number/solutions/c/function/README.md b/challenges/find-outlier-number/solutions/c/function/README.md new file mode 100644 index 0000000..6a98651 --- /dev/null +++ b/challenges/find-outlier-number/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# find-outlier-number/c/function + +Created by [@Divlo](https://github.com/Divlo) on 30 September 2021. diff --git a/challenges/find-outlier-number/solutions/c/function/solution.c b/challenges/find-outlier-number/solutions/c/function/solution.c new file mode 100644 index 0000000..ff289ba --- /dev/null +++ b/challenges/find-outlier-number/solutions/c/function/solution.c @@ -0,0 +1,47 @@ +#include +#include +#include + +bool is_odd(int number) { + return number % 2 == 1; +} + +int find_outlier_number(int* numbers, size_t length) { + int outlier_number; + int* odd_numbers = malloc(length * sizeof(int)); + int* even_numbers = malloc(length * sizeof(int)); + size_t odd_length = 0; + size_t even_length = 0; + for (size_t index = 0; index < length; index++) { + if (is_odd(numbers[index])) { + odd_numbers[odd_length] = numbers[index]; + odd_length++; + } else { + even_numbers[even_length] = numbers[index]; + even_length++; + } + } + if (odd_length == 1) { + outlier_number = odd_numbers[0]; + } else { + outlier_number = even_numbers[0]; + } + free(odd_numbers); + free(even_numbers); + return outlier_number; +} + +int main() { + size_t length = 0; + int number; + int* numbers = malloc(sizeof(int)); + do { + scanf("%d", &number); + numbers = realloc(numbers, sizeof(int) * (length + 1)); + numbers[length] = number; + length++; + } while (fgetc(stdin) != EOF); + printf("%d\n", find_outlier_number(numbers, length)); + free(numbers); + return EXIT_SUCCESS; +} diff --git a/challenges/find-outlier-number/solutions/typescript/function/README.md b/challenges/find-outlier-number/solutions/typescript/function/README.md deleted file mode 100644 index 2fc4b51..0000000 --- a/challenges/find-outlier-number/solutions/typescript/function/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# find-outlier-number/typescript/function - -Created by [@Divlo](https://github.com/Divlo) on 6 June 2021. diff --git a/challenges/find-outlier-number/solutions/typescript/function/solution.ts b/challenges/find-outlier-number/solutions/typescript/function/solution.ts deleted file mode 100644 index d002f17..0000000 --- a/challenges/find-outlier-number/solutions/typescript/function/solution.ts +++ /dev/null @@ -1,35 +0,0 @@ -import readline from 'node:readline' - -const input: string[] = [] -const readlineInterface = readline.createInterface({ - input: process.stdin, - output: process.stdout -}) -readlineInterface.on('line', (value) => { - input.push(value) -}) -readlineInterface.on('close', solution) - -interface NumberObject { - value: number - index: number -} - -const isOdd = (number: number): boolean => { - return number % 2 !== 0 -} - -function solution() { - const numbers = input.map((value) => Number(value)) - const oddNumbers: NumberObject[] = [] - const evenNumbers: NumberObject[] = [] - numbers.forEach((number, index) => { - const numberObject: NumberObject = { value: number, index } - return isOdd(number) - ? oddNumbers.push(numberObject) - : evenNumbers.push(numberObject) - }) - const isValueThatDiffersFromOthers = - oddNumbers.length === 1 ? oddNumbers[0] : evenNumbers[0] - console.log(isValueThatDiffersFromOthers.value) -}