-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_3.rs
90 lines (83 loc) · 1.56 KB
/
task_3.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::utils::{
input::{get_numeric_input, invalid_input},
lcm::Lcm,
Error,
};
const MIN: u32 = 10;
const MAX: u32 = 100;
/// # HOMEWORK 1 | TASK 3
///
/// ## Input
///
/// 1. Three numbers
///
/// ### Constaints
///
/// 1. Each number should be in the inclusive range of: `[10;100]`.
///
/// ### Output
///
/// The [LCM](<https://en.wikipedia.org/wiki/Least_common_multiple>) of the three numbers.
///
/// ### Sample
///
/// #### Input Format
///
/// ```txt
/// 10
/// 20
/// 30
/// ```
///
/// #### Output Format
///
/// ```txt
/// 60
/// ```
///
/// ### Error
///
/// If the input is invalid, return:
///
/// ```rust
/// String::from("Invalid input data!");
/// ```
///
/// ## Test cases
///
/// ```rust
/// use hackerrank::hw1::task_3::Solution;
///
/// assert_eq!(
/// Solution::main([10, 20, 30]),
/// String::from("60")
/// );
/// assert_eq!(
/// Solution::main([9, 10, 20]),
/// String::from("Invalid input data!")
/// );
/// ```
pub struct Solution;
impl Solution {
pub fn main(user_input: [u32; 3]) -> String {
if user_input.iter().any(|x| *x < MIN || *x > MAX) {
return invalid_input();
}
format!(
"{}",
user_input
.iter()
.skip(1)
.fold(user_input[0], |acc, x| acc.lcm(*x))
)
}
}
pub fn main() -> Result<(), Error> {
let user_input = [
get_numeric_input::<u32>()?,
get_numeric_input::<u32>()?,
get_numeric_input::<u32>()?,
];
println!("{}", Solution::main(user_input));
Ok(())
}