C: simplify hamming/binary search

This commit is contained in:
2021-08-20 16:41:13 +02:00
parent 01fcdc8566
commit 6c5e61efe9
4 changed files with 13 additions and 21 deletions

View File

@@ -4,8 +4,7 @@ const int *binary_search(int v, const int *a, size_t size)
{ {
size_t lo, hi, i; size_t lo, hi, i;
/* quickly exclude invalid/trivial results : /* early exclude invalid/trivial result: NULL/empty array, value off bounds
* NULL or empty array, value out of bounds
*/ */
if (!size || !a || v < *a || v > *(a+size-1)) if (!size || !a || v < *a || v > *(a+size-1))
return NULL; return NULL;
@@ -22,7 +21,6 @@ const int *binary_search(int v, const int *a, size_t size)
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile * https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
*/ */
#ifdef UNIT_TEST #ifdef UNIT_TEST
#include <stdio.h>
int main(int ac, char **av) int main(int ac, char **av)
{ {
int arg=1, *res; int arg=1, *res;

View File

@@ -12,6 +12,7 @@ const int *binary_search(int value, const int *arr, size_t length);
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#endif #endif
#ifdef TESTALL #ifdef TESTALL
#undef TEST_IGNORE #undef TEST_IGNORE
#define TEST_IGNORE() {} #define TEST_IGNORE() {}

View File

@@ -1,14 +1,5 @@
#include "hamming.h" #include "hamming.h"
/* Note: For explanation on section below, see 'GNUfilename' included in
* link below :
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
*/
#if defined UNIT_TEST || defined DEBUG
#include <stdio.h>
#include <stdlib.h>
#endif
/* test does not include invalid input, but it should, as the subject is /* test does not include invalid input, but it should, as the subject is
* about DNA sequence, not ASCII chars sequence :-) * about DNA sequence, not ASCII chars sequence :-)
* exercism test needs only: * exercism test needs only:
@@ -16,22 +7,25 @@
*/ */
#define V(p) (*(p)=='A' || *(p)=='C' || *(p)=='G' || *(p)=='T') #define V(p) (*(p)=='A' || *(p)=='C' || *(p)=='G' || *(p)=='T')
int compute(const char *lhs, const char *rhs) int compute(const char *l, const char *r)
{ {
int res=0; int res=0;
const char *l=lhs, *r=rhs;
if (!l || !r) if (!l || !r)
return -1; return -1;
for (; V(l) && V(r); ++l, ++r) { for (; V(l) && V(r); ++l, ++r)
if (*l != *r) if (*l != *r)
res++; res++;
} return *l || *r? -1: res;
return *r || *l? -1: res;
} }
/* See GNUmakefile below for explanation
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
*/
#if defined UNIT_TEST
#include <stdio.h>
#include <stdlib.h>
#ifdef UNIT_TEST
int main(int ac, char **av) int main(int ac, char **av)
{ {
if (ac==3) { if (ac==3) {

View File

@@ -3,9 +3,8 @@
int compute(const char *lhs, const char *rhs); int compute(const char *lhs, const char *rhs);
/* Note: For explanation on section below, see 'GNUfilename' included in /* See GNUmakefile below for explanation
* link below : * https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
*/ */
#ifdef TESTALL #ifdef TESTALL
#undef TEST_IGNORE #undef TEST_IGNORE