-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsecutive_groups.rs
55 lines (50 loc) · 1.65 KB
/
consecutive_groups.rs
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Consecutive groups problem is a problem in which we have to find the number
* of groups needs to be created from a list of numbers. We have to create a new
* group if there is a gap between 2 numbers.
*
* - example 1: [1,2,4,5,8,9,10]
* Here, this group should be grouped into [1,2], [4,5], [7,8,9]
* here, there is 3 skipped so we need to create a new group for 4,5 similarly,
* 6 and 7 are skipped so we need to create another group for 8,9,10. Hence,
* the total number of groups to be created is 3.
*
* - example 2: [1,4,11,3,12,2,14,13]
* here we can see consecutive numbers [1,2,3,4] and [11,12,13,14], so the
* total number of groups to be created is 2.
*
*/
use common::input; // common library for this repository
fn find_groups(mut list: Vec<usize>) -> usize {
let mut sets = 1;
list.sort();
for (index, item) in list.iter().enumerate() {
if index < list.len() - 1 && list[index + 1] - item != 1 {
sets += 1;
}
}
sets
}
fn main() {
// ! input() is a common library function, not included in std
let value = input("enter numbers separated by space");
println!("{}", value);
let int_list: Vec<usize> = value
.split(" ")
.map(|v| v.trim().parse::<usize>().unwrap())
.collect();
println!("given: {:?}", int_list);
println!("total groups: {}", find_groups(int_list));
}
#[cfg(test)]
mod test {
use crate::find_groups;
#[test]
fn correct() {
assert_eq!(find_groups(vec![1, 2, 4, 5, 6, 8, 9]), 3);
}
#[test]
fn correct_with_unordered() {
assert_eq!(find_groups(vec![1, 14, 2, 12, 3, 9, 15, 10, 8, 13]), 3)
}
}