C: simplify hamming/binary search
This commit is contained in:
@@ -4,8 +4,7 @@ const int *binary_search(int v, const int *a, size_t size)
|
||||
{
|
||||
size_t lo, hi, i;
|
||||
|
||||
/* quickly exclude invalid/trivial results :
|
||||
* NULL or empty array, value out of bounds
|
||||
/* early exclude invalid/trivial result: NULL/empty array, value off bounds
|
||||
*/
|
||||
if (!size || !a || v < *a || v > *(a+size-1))
|
||||
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
|
||||
*/
|
||||
#ifdef UNIT_TEST
|
||||
#include <stdio.h>
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int arg=1, *res;
|
||||
|
@@ -12,6 +12,7 @@ const int *binary_search(int value, const int *arr, size_t length);
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#ifdef TESTALL
|
||||
#undef TEST_IGNORE
|
||||
#define TEST_IGNORE() {}
|
||||
|
@@ -1,14 +1,5 @@
|
||||
#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
|
||||
* about DNA sequence, not ASCII chars sequence :-)
|
||||
* exercism test needs only:
|
||||
@@ -16,22 +7,25 @@
|
||||
*/
|
||||
#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;
|
||||
const char *l=lhs, *r=rhs;
|
||||
|
||||
if (!l || !r)
|
||||
return -1;
|
||||
for (; V(l) && V(r); ++l, ++r) {
|
||||
for (; V(l) && V(r); ++l, ++r)
|
||||
if (*l != *r)
|
||||
res++;
|
||||
}
|
||||
return *r || *l? -1: res;
|
||||
return *l || *r? -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)
|
||||
{
|
||||
if (ac==3) {
|
||||
|
@@ -3,9 +3,8 @@
|
||||
|
||||
int compute(const char *lhs, const char *rhs);
|
||||
|
||||
/* Note: For explanation on section below, see 'GNUfilename' included in
|
||||
* link below :
|
||||
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
|
||||
/* See GNUmakefile below for explanation
|
||||
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
|
||||
*/
|
||||
#ifdef TESTALL
|
||||
#undef TEST_IGNORE
|
||||
|
Reference in New Issue
Block a user