aboutsummaryrefslogtreecommitdiff
path: root/challenge-344/benjamin-andre/rust/ch-2.rs
blob: 740ee45d31b7211b85eb86cca00f80405b86c13e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit

fn array_formation(source: &[&[i32]], target: &[i32]) -> bool {
    let mut pos = 0;
    while pos < target.len() {
        let chunk = source
            .iter()
            .find(|&&s| s.len() <= target.len() - pos && s == &target[pos..pos + s.len()]);
        match chunk {
            Some(c) => pos += c.len(),
            None => return false,
        }
    }
    true
}

#[test]
fn example() {
    assert_eq!(array_formation(&[&[2, 3], &[1], &[4]], &[1, 2, 3, 4]), true);
    assert_eq!(array_formation(&[&[1, 3], &[2, 4]], &[1, 2, 3, 4]), false);
    assert_eq!(
        array_formation(&[&[9, 1], &[5, 8], &[2]], &[5, 8, 2, 9, 1]),
        true
    );
    assert_eq!(array_formation(&[&[1], &[3]], &[1, 2, 3]), false);
    assert_eq!(array_formation(&[&[7, 4, 6]], &[7, 4, 6]), true);
}