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

@@ -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) {

View File

@@ -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