l8kyu/
arrays.rs

1//!
2//! Modules categorized by Codewars labels - 8kyu *** Arrays ***
3//!
4
5use shared::kata::*;
6
7/// You are given two sorted arrays that contain only integers.
8/// These arrays may be sorted in either ascending or descending order.
9/// Your task is to merge them into a single array, ensuring that:
10/// The resulting array is sorted in ascending order. Any duplicate values
11/// are removed, so each integer appears only once. If both input arrays are
12/// empty, return an empty array. No input validation is needed, as both
13/// arrays are guaranteed to contain zero or more integers.
14/// # Example
15/// ``` [1, 2, 3, 4, 5], [6, 7, 8, 9, 10] ``` <br>
16/// returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
17pub fn merge_arrays(arr1: &[i32], arr2: &[i32]) -> Vec<i32> {
18    let kata = Kata {
19        level: Level::L8kyu,
20        tags: vec![Tag::Arrays, Tag::Fundamentals],
21        description: String::from("Merge two sorted arrays into one"),
22    };
23
24    println!(
25        "Level: {:?}, Tags: {:?}, Description: {}",
26        kata.level, kata.tags, kata.description
27    );
28
29    // let mut vec1 = arr1.to_vec();
30    // let mut vec2 = arr2.to_vec();
31
32    // if !vec1.is_sorted() {
33    //     vec1.reverse();
34    // }
35    // if !vec2.is_sorted() {
36    //     vec2.reverse();
37    // }
38
39    // let mut merged = vec1;
40    // merged.extend(vec2);
41
42    // merged.sort();
43    // merged.dedup();
44
45    // Found better solution on cw
46
47    let mut merged = [arr1, arr2].concat();
48    merged.sort();
49    merged.dedup();
50
51    merged
52}
53
54/// Your task is to find the first element of an array that is not consecutive.
55/// If the whole array is consecutive then return null.
56/// The array will always have at least 2 elements and all elements will be numbers.
57/// The numbers will also all be unique and in ascending order. The numbers could
58/// be positive or negative and the first non-consecutive could be either too!
59/// # Example
60/// ``` [1,2,3,4,6,7,8] ``` <br>
61/// returns 6
62pub fn first_non_consecutive(arr: &Vec<i32>) -> Option<i32> {
63    let kata = Kata {
64        level: Level::L8kyu,
65        tags: vec![Tag::Arrays, Tag::Fundamentals],
66        description: String::from("Find the first non-consecutive number"),
67    };
68
69    println!(
70        "Level: {:?}, Tags: {:?}, Description: {}",
71        kata.level, kata.tags, kata.description
72    );
73
74    for i in 1..arr.len() {
75        if arr[i] - arr[i - 1] != 1 {
76            return Some(arr[i]);
77        }
78    }
79    None
80}
81
82/// Complete the square sum function so that it squares each number passed into it
83/// and then sums the results together.
84/// # Example
85/// ``` square_sum(vec![5, 3, 4]); ``` <br>
86/// returns 50
87pub fn square_sum(vec: Vec<i32>) -> i32 {
88    let kata = Kata {
89        level: Level::L8kyu,
90        tags: vec![Tag::Arrays, Tag::Lists, Tag::Fundamentals],
91        description: String::from("Square(n) Sum"),
92    };
93
94    println!(
95        "Level: {:?}, Tags: {:?}, Description: {}",
96        kata.level, kata.tags, kata.description
97    );
98
99    vec.iter().map(|&x| x * x).sum()
100}