algo/
algo.rs

1use rs_algo::compare::{LCSubsequence, LCSubstring};
2use rs_algo::math;
3use rs_algo::search::binary;
4use rs_algo::sort::*;
5
6// test
7fn main() {
8    let mut a = vec![
9        117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 32, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 32, 1, 3,
10        99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 32, 1, 3, 99, 10, 7, 2, 11, -5,
11        4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
12    ];
13    let mut b = vec![
14        117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 817, 1, 3,
15        99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5,
16        4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
17    ];
18    let mut c = vec![
19        117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 817, 1, 3,
20        99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5,
21        4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
22    ];
23    let mut d = vec![
24        117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 817, 1, 3,
25        99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5,
26        4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
27    ];
28
29    // Get a sorted array without changing the original
30    let sorted_bubble = bubble::sort(&a);
31    let sorted_insertion = insertion::sort(&b);
32    let sorted_merge = merge::sort(&c);
33    let sorted_quick = quick::sort(&d);
34
35    // This will sort the vector passed in, changing the original vector order
36    merge::sort_mut(&mut a);
37    quick::sort_mut(&mut b);
38    insertion::sort_mut(&mut c);
39    bubble::sort_mut(&mut d);
40
41    // get a new longest common sequence object
42    let sequence = LCSubsequence::new_subsequence("leighxxxft".to_string(), "right".to_string());
43    assert_eq!(sequence.subsequence_len, 4);
44    assert_eq!(sequence.get_longest_subsequence(), Some("ight".to_string()));
45
46    // get a new longest common substring
47    let substring = LCSubstring::new_substring(
48        "!!!!Hello WorldXXXXX".to_string(),
49        "XXX   Hello World@cvcvcvc".to_string(),
50    );
51    assert_eq!(substring.substring_len, 11);
52    assert_eq!(
53        substring.get_longest_substring(),
54        Some("Hello World".to_string())
55    );
56
57    // search our array. Binary search needs the array to already be sorted
58    match binary::search(817, &a) {
59        Some(value) => println!("our array has value {}", value),
60        None => println!("our array dosen't have value 817"),
61    }
62
63    match binary::index_of(817, &a) {
64        Some(index) => println!("index of 817 is {}", index),
65        None => println!("no index of 817, guess it's not in there"),
66    }
67
68    // common math functions
69    let divisor = math::gcd(30, 21);
70    assert_eq!(Ok(3), divisor);
71
72    let factor = math::factors(9124);
73    assert_eq!(Some(vec![2, 4, 2281, 4562]), factor);
74}