Ad
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
Code
Diff
  • 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()
    • }