Strings
Given two UTF-8 strings, calculate the sum of each string's characters and return true
if they match.
are_utf8_sums_equal("Anna", "Nana") == true
"Anna" == 65 + 110 + 110 + 97 == 382
"Nana" == 78 + 97 + 110 + 97 == 382
are_utf8_sums_equal("Sebastian", "Patricia") == false
"Sebastian" == 83 + 101 + 98 + 97 + 115 + 116 + 105 + 97 + 110 == 922
"Patricia" == 80 + 97 + 116 + 114 + 105 + 99 + 105 + 97 == 813
are_utf8_sums_equal("わたしのごく", "わたしはごぎ") == true
"わたしのごく" == 12431 + 12383 + 12375 + 12398 + 12372 + 12367 == 74326
"わたしはごぎ" == 12431 + 12383 + 12375 + 12399 + 12372 + 12366 == 74326
fn are_utf8_sums_equal(a: &str, b: &str) -> bool { utf8_sum(a) == utf8_sum(b) } fn utf8_sum(string: &str) -> u32 { string.chars().map(u32::from).sum() }
fn verify_sum(name_one: &str, name_two: &str) -> bool {sum(name_one) == sum(name_two)- fn are_utf8_sums_equal(a: &str, b: &str) -> bool {
- utf8_sum(a) == utf8_sum(b)
- }
fn sum(name: &str) -> u32 {name.chars().filter(|ch| ch.is_ascii_alphabetic()).map(u32::from).sum()}- fn utf8_sum(string: &str) -> u32 {
- string.chars().map(u32::from).sum()
- }
#[cfg(test)] mod test { use super::*; #[test] fn roman_alphabet() { assert!(!are_utf8_sums_equal("Sebastian", "Patricia")); assert!(are_utf8_sums_equal("Anna", "Nana")); assert!(are_utf8_sums_equal("John Doe", "Doe John")); assert!(are_utf8_sums_equal("Bob", "bOb")); } #[test] fn empty_string() { assert!(are_utf8_sums_equal("", "")); } #[test] fn whitespace() { assert!(are_utf8_sums_equal(" ", " ")); assert!(are_utf8_sums_equal("\n", "\n")); assert!(are_utf8_sums_equal("\r\r\r\r\r\r\r\r\r", "\t\t\t\t\t\t\t\t\t\t\t\t\t")); assert!(!are_utf8_sums_equal(" ", "\n")); assert!(!are_utf8_sums_equal(" ", "\t")); assert!(!are_utf8_sums_equal("\r", "\n")); } #[test] fn hiragana() { assert!(are_utf8_sums_equal("わたしのごく", "わたしはごぎ")); } }
- #[cfg(test)]
- mod test {
- use super::*;
- #[test]
fn test() {assert!(!verify_sum("Sebastian", "Patricia"));assert!(verify_sum("Anna", "Nana"));assert!(verify_sum("",""));assert!(verify_sum("John Doe", "Doe John"));assert!(verify_sum("Bob", "bOb"));- fn roman_alphabet() {
- assert!(!are_utf8_sums_equal("Sebastian", "Patricia"));
- assert!(are_utf8_sums_equal("Anna", "Nana"));
- assert!(are_utf8_sums_equal("John Doe", "Doe John"));
- assert!(are_utf8_sums_equal("Bob", "bOb"));
- }
- #[test]
- fn empty_string() {
- assert!(are_utf8_sums_equal("", ""));
- }
- #[test]
- fn whitespace() {
- assert!(are_utf8_sums_equal(" ", " "));
- assert!(are_utf8_sums_equal("\n", "\n"));
- assert!(are_utf8_sums_equal("\r\r\r\r\r\r\r\r\r", "\t\t\t\t\t\t\t\t\t\t\t\t\t"));
- assert!(!are_utf8_sums_equal(" ", "\n"));
- assert!(!are_utf8_sums_equal(" ", "\t"));
- assert!(!are_utf8_sums_equal("\r", "\n"));
- }
- #[test]
- fn hiragana() {
- assert!(are_utf8_sums_equal("わたしのごく", "わたしはごぎ"));
- }
- }