loading...

re: Daily Challenge #50 - Number Neighbor VIEW POST

FULL DISCUSSION
 

I liked the "any number can differ by 1" approach more - it's a but more work :)

Rust:

pub fn number_neighbors(number: u32) -> Vec<u32> {
    let len = number.to_string().len() as u32;
    (0..len)
        .map(|delta| 10u32.pow(delta))
        .flat_map(|delta| vec![number - delta, number + delta])
        .collect()
}

And a test (I usually omit them from my submissions, but I think they have a place here):

#[test]                                                                   
fn test_number_neighbors() {                                              
    assert_eq!(vec![221, 223, 212, 232, 122, 322], number_neighbors(222));
}                                                                         
code of conduct - report abuse