Created
September 25, 2018 06:30
-
-
Save Sam-Gram/ea62bec0281c0e3094f3a75262cd9c94 to your computer and use it in GitHub Desktop.
String distance in rust, first stab.
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
/** | |
GLWT(Good Luck With That) Public License | |
Copyright (c) Everyone, except Author | |
The author has absolutely no clue what the code in this project does. | |
It might just work or not, there is no third option. | |
Everyone is permitted to copy, distribute, modify, merge, sell, publish, | |
sublicense or whatever they want with this software but at their OWN RISK. | |
GOOD LUCK WITH THAT PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION, AND MODIFICATION | |
0. You just DO WHATEVER YOU WANT TO as long as you NEVER LEAVE A | |
TRACE TO TRACK THE AUTHOR of the original product to blame for or hold | |
responsible. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
Good luck. | |
*/ | |
use std::env; | |
fn min(a:i32,b:i32,c:i32) -> i32 { | |
if a <= b && a <= c { | |
a | |
} else if b <= c{ | |
b | |
} else { | |
c | |
} | |
} | |
fn string_distance(a: &String,b: &String) -> i32 { | |
let a_len = a.len(); | |
let b_len = b.len(); | |
let a:Vec<char> = a.chars().collect(); | |
let b:Vec<char> = b.chars().collect(); | |
let mut edit_tracker: Vec<Vec<i32>> = vec![vec![0;b_len]; a_len]; | |
for i in 0..a_len { | |
for j in 0..b_len { | |
if i == 0 { | |
edit_tracker[i][j] = j as i32; | |
} else if j == 0 { | |
edit_tracker[i][j] = i as i32; | |
} else if a[i-1] == b[j-1] { | |
edit_tracker[i][j] = edit_tracker[i-1][j-1]; | |
} else { | |
edit_tracker[i][j] = 1 + min(edit_tracker[i][j-1],edit_tracker[i-1][j],edit_tracker[i-1][j-1]); | |
} | |
} | |
} | |
edit_tracker[a_len-1][b_len-1] | |
} | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
println!("{}",string_distance(&args[1], &args[2])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment