From ae98925062d5a392a73df4a1456df3a4a3be9b18 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 8 Mar 2013 20:14:51 -0600 Subject: factor common behavior out into a library --- rosalind/str.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rosalind/str.rs (limited to 'rosalind/str.rs') diff --git a/rosalind/str.rs b/rosalind/str.rs new file mode 100644 index 0000000..0fac1cd --- /dev/null +++ b/rosalind/str.rs @@ -0,0 +1,21 @@ +use str = core::str; + +/* really feels like there should be a more efficient way to do this */ +fn reverse(s: &str) -> ~str { + let mut r = ~""; + str::reserve(&mut r, str::len(s)); + for str::each_char(s) |ch| { + str::unshift_char(&mut r, ch) + } + r +} + +pure fn hamming(string1: ~str, string2: ~str) -> int { + let mut hamming = 0; + for str::each_chari(string1) |i, ch| { + if ch != str::char_at(string2, i) { + hamming += 1; + } + } + hamming +} -- cgit v1.2.3