Added documentation
This commit is contained in:
parent
39c3fae613
commit
f16c2aa665
3 changed files with 64 additions and 9 deletions
19
README.md
Normal file
19
README.md
Normal file
|
@ -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
|
|
@ -1,4 +1,4 @@
|
|||
struct Solution;
|
||||
pub struct Solution;
|
||||
|
||||
// Copy from here for submitting this solution to leet code
|
||||
impl Solution {
|
||||
|
@ -80,6 +80,7 @@ mod testing {
|
|||
}
|
||||
|
||||
#[test]
|
||||
// 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.
|
||||
|
@ -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;
|
||||
|
|
37
src/main.rs
37
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::<u32>().map(|parsed| parsed as i32))
|
||||
.collect::<Result<Vec<i32>, _>>()
|
||||
{
|
||||
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"
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue