Created
September 22, 2019 23:58
-
-
Save andrewmatte/b1f2e83796cbbefcf2bc31f2f725c9e9 to your computer and use it in GitHub Desktop.
same old fuzzy string algo, shiny to rust implementation
This file contains 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
use std::env; | |
//"./fuzzy andrew andy" should yield 0.91588783 | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
let arg1 = &args[1]; | |
let arg2 = &args[2]; | |
println!("{}", fuzzy(arg1.to_string(), arg2.to_string())); | |
} | |
fn fuzzy(a: String, b: String) -> f32 { | |
let mut reward = 0; | |
let mut penalty = 0; | |
for i in 0..a.len() { | |
for j in i..a.len() { | |
if b.contains(&a[i..j]) { | |
reward += j * 2; | |
} else { | |
penalty += 1; | |
} | |
} | |
} | |
for i in 0..b.len() { | |
for j in i..b.len() { | |
if a.contains(&b[i..j]) { | |
reward += j * 2; | |
} else { | |
penalty += 1; | |
} | |
} | |
} | |
let reward = reward as f32; | |
let penalty = penalty as f32; | |
return reward/(reward + penalty); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment