diff --git a/README.md b/README.md new file mode 100644 index 0000000..e0e55b5 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Jump game as a coding challenge + +This challenge was invented by google. +This code base serves as a solution to this problem + +The problem is described and can be tested on leetcode +under the following link: + +https://leetcode.com/problems/jump-game/description/ + +## Tests + +There is a number of unit tests with test cases. +These test cases were derived from the given examples on leetcode +and failed submissions of previously flawed solutions. + +## Status + +This solution passes the submission diff --git a/src/lib.rs b/src/lib.rs index 98abc21..4b96a49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -struct Solution; +pub struct Solution; // Copy from here for submitting this solution to leet code impl Solution { @@ -80,9 +80,10 @@ mod testing { } #[test] - //Input: nums = [2,3,1,1,4] - //Output: true - //Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. + // Example 1: + // Input: nums = [2,3,1,1,4] + // Output: true + // Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. fn jump_possible() { // Example 1: // @@ -91,10 +92,10 @@ mod testing { assert_case(given, expected); } - //Example 2: + // Example 2: // - //Input: nums = [3,2,1,0,4] - //Output: false + // Input: nums = [3,2,1,0,4] + // Output: false #[test] fn no_jump_possible() { let given = vec![3, 2, 1, 0, 4]; @@ -103,6 +104,7 @@ mod testing { } #[test] + // Taken from a failed submission fn jump_two_elements() { let given = vec![2, 0]; let expected = true; @@ -110,6 +112,7 @@ mod testing { } #[test] + // Taken from a failed submission fn can_jump_complex_array() { let given = vec![3, 0, 8, 2, 0, 0, 1]; let expected = true; diff --git a/src/main.rs b/src/main.rs index e7a11a9..17ffff6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,36 @@ -fn main() { - println!("Hello, world!"); +use std::process::ExitCode; + +fn main() -> ExitCode { + if std::env::args().len() != 2 { + eprintln!("Must provide one argument for the test input"); + return ExitCode::FAILURE; + } + let argument = std::env::args().into_iter().skip(1).next().unwrap(); + match argument.as_str() { + "-h" | "--help" => println!("{}", usage()), + input => { + match input + .split_whitespace() + .map(|to_parse| to_parse.parse::().map(|parsed| parsed as i32)) + .collect::, _>>() + { + Ok(seq) => { + let actual = jump_game::Solution::can_jump(seq); + println!("{}", actual); + } + Err(_error) => { + eprintln!("Input is not in a valid format"); + return ExitCode::FAILURE; + } + } + } + } + + return ExitCode::SUCCESS; +} + +fn usage() -> &'static str { + "jump_game [sequence as input]\n\ + Syntax for sequence (\\d+\\s)\n\ + Example: 2 4" }