-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_1.rs
94 lines (87 loc) · 2.38 KB
/
task_1.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
91
92
93
94
use crate::utils::{
input::{get_input, get_numeric_input, invalid_input},
Error,
};
const MIN_CIPHER: u32 = 0;
const MAX_CIPHER: u32 = 25;
/// # HOMEWORK 4 | TASK 1
///
/// ## Input
///
/// 1. One str (the text to cipher) and 1 number (the cipher amount).
///
/// ### Constaints
///
/// 2. The cipher amount should be in the inclusive range of: `[0;25]`.
///
/// ### Output
///
/// The ciphered version of the the input text by the cipher amount.
///
/// ### Error
///
/// If the input is invalid, return:
///
/// ```rust
/// String::from("Invalid input data!");
/// ```
///
/// ## Test cases
///
/// ```rust
/// use hackerrank::hw4::task_1::Solution;
///
/// assert_eq!(
/// // lol
/// Solution::main("Declaring and Invoking Functions in C++!", 3),
/// String::from("Ghfodulqj dqg Lqyrnlqj Ixqfwlrqv lq F++!")
/// );
/// assert_eq!(
/// Solution::main("Discrete Mathematics and Programming", 3),
/// String::from("Glvfuhwh Pdwkhpdwlfv dqg Surjudpplqj")
/// );
/// assert_eq!(
/// Solution::main("Discrete Mathematics and Programming", 5),
/// String::from("Inxhwjyj Rfymjrfynhx fsi Uwtlwfrrnsl")
/// );
/// assert_eq!(
/// Solution::main("Reading and Printing 2D Arrays", 0),
/// String::from("Reading and Printing 2D Arrays")
/// );
/// assert_eq!(
/// Solution::main("Reading and Printing 2D Arrays", 26),
/// String::from("Invalid input data!")
/// );
/// assert_eq!(
/// Solution::main("Generate code!", 22),
/// String::from("Cajanwpa ykza!")
/// );
/// assert_eq!(
/// Solution::main("Exchanging Knowledge", 10),
/// String::from("Ohmrkxqsxq Uxygvonqo")
/// );
/// ```
pub struct Solution;
impl Solution {
pub fn main(str: &str, cipher: u32) -> String {
if cipher == 0 {
return str.to_string();
}
if !(MIN_CIPHER..=MAX_CIPHER).contains(&cipher) {
return invalid_input();
}
str.chars()
.map(|c| match c {
'A'..='Z' => ((((c as u8) - b'A' + cipher as u8) % 26) + b'A') as char,
'a'..='z' => ((((c as u8) - b'a' + cipher as u8) % 26) + b'a') as char,
_ => c,
})
.collect()
}
}
pub fn main() -> Result<(), Error> {
let str = get_input()?;
let cipher = get_numeric_input::<u32>()?;
println!("{}", Solution::main(&str, cipher));
Ok(())
}