Created
September 12, 2019 15:22
-
-
Save andrewmatte/fd505e8e83a5b1be16a3db0e4469c3bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
void substring(const char [], char[], int, int); | |
float fuzzy_string(const char* a, const char* b) { | |
int reward = 0; | |
int penalty = 0; | |
int len_a = 0; | |
while (a[len_a] != '\0') len_a++; | |
int len_b = 0; | |
char sub[64]; | |
while (b[len_b] != '\0') len_b++; | |
for (int start = 0; start < len_a; start++) { | |
for (int length = 0; length < len_a - 1; length++) { | |
substring(a, sub, start, length); | |
if(strstr(b, sub) != NULL) { | |
reward += length * 2; | |
} else { | |
penalty += 1; | |
} | |
} | |
} | |
for (int start = 0; start < len_b; start++) { | |
for (int length = 0; length < len_b - 1; length++) { | |
substring(b, sub, start, length); | |
if(strstr(a, sub) != NULL) { | |
reward += length * 2; | |
} else { | |
penalty += 1; | |
} | |
} | |
} | |
float total = reward + penalty; | |
return (float) reward / total; | |
} | |
void substring(const char s[], char sub[], int p, int l) { | |
int c = 0; | |
while (c < l) { | |
sub[c] = s[p+c-1]; | |
c++; | |
} | |
sub[c] = '\0'; | |
} | |
int main() { | |
float x = fuzzy_string("andrew", "mandrew"); | |
printf("%f\n", x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment