rustgym/leetcode/
_1271_hexspeak.rs1struct Solution;
2
3#[allow(clippy::wrong_self_convention)]
4impl Solution {
5 fn to_hexspeak(num: String) -> String {
6 let x: i64 = num.parse::<i64>().unwrap();
7 let s = format!("{:X}", x);
8 let mut res: Vec<char> = vec![];
9 for c in s.chars() {
10 match c {
11 '0' => res.push('O'),
12 '1' => res.push('I'),
13 c @ 'A'..='F' => res.push(c),
14 _ => return "ERROR".to_string(),
15 }
16 }
17 res.iter().collect()
18 }
19}
20
21#[test]
22fn test() {
23 let num = "257".to_string();
24 let res = "IOI".to_string();
25 assert_eq!(Solution::to_hexspeak(num), res);
26 let num = "3".to_string();
27 let res = "ERROR".to_string();
28 assert_eq!(Solution::to_hexspeak(num), res);
29 let num = "619879596177".to_string();
30 let res = "ERROR".to_string();
31 assert_eq!(Solution::to_hexspeak(num), res);
32}