Skip to content

Commit d6cc187

Browse files
committed
Add problem 3024: Type of Triangle
1 parent 2bf8bd2 commit d6cc187

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,7 @@ pub mod problem_3014_minimum_number_of_pushes_to_type_word_i;
20512051
pub mod problem_3016_minimum_number_of_pushes_to_type_word_ii;
20522052
pub mod problem_3019_number_of_changing_keys;
20532053
pub mod problem_3021_alice_and_bob_playing_flower_game;
2054+
pub mod problem_3024_type_of_triangle;
20542055

20552056
#[cfg(test)]
20562057
mod test_utilities;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::mem;
6+
7+
impl Solution {
8+
pub fn triangle_type(nums: Vec<i32>) -> String {
9+
const BUFFER: &str = "isoscelescalenequilateral";
10+
11+
let isosceles = &BUFFER[..9];
12+
let scalene = &BUFFER[8..15];
13+
let equilateral = &BUFFER[14..];
14+
let [mut a, mut b, mut c] = <[_; 3]>::map(nums.try_into().ok().unwrap(), i32::cast_unsigned);
15+
16+
'block: {
17+
let max = if b > a {
18+
if c > b { &mut c } else { &mut b }
19+
} else if c > a {
20+
&mut c
21+
} else {
22+
break 'block;
23+
};
24+
25+
mem::swap(&mut a, max);
26+
}
27+
28+
String::from(if b + c > a {
29+
if a == b {
30+
if a == c { equilateral } else { isosceles }
31+
} else if a == c || b == c {
32+
isosceles
33+
} else {
34+
scalene
35+
}
36+
} else {
37+
"none"
38+
})
39+
}
40+
}
41+
42+
// ------------------------------------------------------ snip ------------------------------------------------------ //
43+
44+
impl super::Solution for Solution {
45+
fn triangle_type(digits: Vec<i32>) -> String {
46+
Self::triangle_type(digits)
47+
}
48+
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
#[test]
53+
fn test_solution() {
54+
super::super::tests::run::<super::Solution>();
55+
}
56+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub mod mathematical;
2+
3+
pub trait Solution {
4+
fn triangle_type(digits: Vec<i32>) -> String;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
(&[3, 3, 3] as &[_], "equilateral"),
14+
(&[3, 4, 5], "scalene"),
15+
(&[9, 4, 9], "isosceles"),
16+
(&[5, 3, 8], "none"),
17+
];
18+
19+
for (digits, expected) in test_cases {
20+
assert_eq!(S::triangle_type(digits.to_vec()), expected);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)