Created
November 29, 2021 18:33
-
-
Save zoeisnowooze/2d4de576560cd1f340956aca424a94b0 to your computer and use it in GitHub Desktop.
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
#![allow(unused)] | |
extern crate itertools; | |
use itertools::Itertools; | |
fn phone_letter(number: &str) -> Vec<String> { | |
number | |
.chars() | |
.filter_map(|digit| match digit { | |
'2' => Some(vec!['a', 'b', 'c']), | |
'3' => Some(vec!['d', 'e', 'f']), | |
'4' => Some(vec!['g', 'h', 'i']), | |
'5' => Some(vec!['j', 'k', 'l']), | |
'6' => Some(vec!['m', 'n', 'o']), | |
'7' => Some(vec!['p', 'q', 'r', 's']), | |
'8' => Some(vec!['t', 'u', 'v']), | |
'9' => Some(vec!['w', 'x', 'y', 'z']), | |
_ => None, | |
}) | |
.multi_cartesian_product() | |
.map(|chars| chars.iter().collect::<String>()) | |
.collect() | |
} | |
#[test] | |
fn single_digit() { | |
assert_eq!(phone_letter("9"), vec!["w", "x", "y", "z"]); | |
} | |
#[test] | |
fn multiple_digits() { | |
assert_eq!( | |
phone_letter("23"), | |
vec!["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment