Phone Number

1. Readme

电话号码

整理用户输入的电话号码,以便他们可以发送短信.

这个**北美编号计划(NANP)**是北美洲、加拿大或百慕大群岛等许多国家使用的电话号码系统。所有 NANP 国家共享相同的国际国家代码:1

NANP 数字是十位数字,由三位编号——区域划分代码组成,俗称地区代码其次是一个七位数的本地号码。本地号码的前三位数字表示交换码,剩余是唯一的四位数字,这是用户号码

格式通常表示为

(NXX)-NXX-XXXX

这里的N是从 2 到 9 的任何数字,而X是从 0 到 9 的任何数字.

您的任务是通过删除标点符号和国家代码(1),整理不同格式的电话号码。

例如,输入

  • +1 (613)-995-0253
  • 613-995-0253
  • 1 613 995 0253
  • 613.995.0253

都应该产出

6139950253

**注:**因为这个练习只涉及 NANP 国家使用的电话号码,只有 1 被认为是有效的国家代码。

资源

JumpstartLab 的事件管理器http://tutorials.jumpstartlab.com/projects/eventmanager.html

2. 开始你的表演

pub fn number(user_number: &str) -> Option<String> {
   unimplemented!(
       "Given the number entered by user '{}', convert it into SMS-friendly format. If the entered number is not a valid NANP number, return None.",
       user_number
   );
}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
fn to_some_string(s: &str) -> Option<String> {
   Some(s.to_string())
}

#[test]
fn test_cleans_the_number() {
   assert_eq!(number("(223) 456-7890"), to_some_string("2234567890"));
}

#[test]
//#[ignore]
fn test_cleans_numbers_with_dots() {
   assert_eq!(number("223.456.7890"), to_some_string("2234567890"));
}

#[test]
//#[ignore]
fn test_cleans_numbers_with_multiple_spaces() {
   assert_eq!(number("223 456   7890   "), to_some_string("2234567890"));
}

#[test]
//#[ignore]
fn test_invalid_when_9_digits() {
   assert_eq!(number("123456789"), None);
}

#[test]
//#[ignore]
fn test_invalid_when_11_digits_does_not_start_with_a_1() {
   assert_eq!(number("22234567890"), None);
}

#[test]
//#[ignore]
fn test_valid_when_11_digits_and_starting_with_1() {
   assert_eq!(number("12234567890"), to_some_string("2234567890"));
}

#[test]
//#[ignore]
fn test_valid_when_11_digits_and_starting_with_1_even_with_punctuation() {
   assert_eq!(number("+1 (223) 456-7890"), to_some_string("2234567890"));
}

#[test]
//#[ignore]
fn test_invalid_when_more_than_11_digits() {
   assert_eq!(number("321234567890"), None);
}

#[test]
//#[ignore]
fn test_invalid_with_letters() {
   assert_eq!(number("123-abc-7890"), None);
}

#[test]
//#[ignore]
fn test_invalid_with_punctuations() {
   assert_eq!(number("123-@:!-7890"), None);
}

#[test]
//#[ignore]
fn test_invalid_if_area_code_does_not_start_with_2_9() {
   assert_eq!(number("(123) 456-7890"), None);
}

#[test]
//#[ignore]
fn test_invalid_if_exchange_code_does_not_start_with_2_9() {
   assert_eq!(number("(223) 056-7890"), None);
}

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
pub fn number(s: &str) -> Option<String> {
   let digits: String = s.chars().filter(|&c| c.is_digit(10)).collect();
   match digits.len() {
       10 => match (digits.chars().nth(0), digits.chars().nth(3)) {
           (Some('0'), _) => None,
           (Some('1'), _) => None,
           (_, Some('0')) => None,
           (_, Some('1')) => None,
           _ => Some(digits),
       },
       11 => match digits.chars().nth(0) {
           Some('1') => Some(digits[1..].to_string()),
           _ => None,
       },
       _ => None,
   }
}

#}



填充/相关