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
|
17
src/lib.rs
17
src/lib.rs
|
@ -1,4 +1,4 @@
|
||||||
struct Solution;
|
pub struct Solution;
|
||||||
|
|
||||||
// Copy from here for submitting this solution to leet code
|
// Copy from here for submitting this solution to leet code
|
||||||
impl Solution {
|
impl Solution {
|
||||||
|
@ -80,9 +80,10 @@ mod testing {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
//Input: nums = [2,3,1,1,4]
|
// Example 1:
|
||||||
//Output: true
|
// Input: nums = [2,3,1,1,4]
|
||||||
//Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
|
// Output: true
|
||||||
|
// Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
|
||||||
fn jump_possible() {
|
fn jump_possible() {
|
||||||
// Example 1:
|
// Example 1:
|
||||||
//
|
//
|
||||||
|
@ -91,10 +92,10 @@ mod testing {
|
||||||
assert_case(given, expected);
|
assert_case(given, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Example 2:
|
// Example 2:
|
||||||
//
|
//
|
||||||
//Input: nums = [3,2,1,0,4]
|
// Input: nums = [3,2,1,0,4]
|
||||||
//Output: false
|
// Output: false
|
||||||
#[test]
|
#[test]
|
||||||
fn no_jump_possible() {
|
fn no_jump_possible() {
|
||||||
let given = vec![3, 2, 1, 0, 4];
|
let given = vec![3, 2, 1, 0, 4];
|
||||||
|
@ -103,6 +104,7 @@ mod testing {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
// Taken from a failed submission
|
||||||
fn jump_two_elements() {
|
fn jump_two_elements() {
|
||||||
let given = vec![2, 0];
|
let given = vec![2, 0];
|
||||||
let expected = true;
|
let expected = true;
|
||||||
|
@ -110,6 +112,7 @@ mod testing {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
// Taken from a failed submission
|
||||||
fn can_jump_complex_array() {
|
fn can_jump_complex_array() {
|
||||||
let given = vec![3, 0, 8, 2, 0, 0, 1];
|
let given = vec![3, 0, 8, 2, 0, 0, 1];
|
||||||
let expected = true;
|
let expected = true;
|
||||||
|
|
37
src/main.rs
37
src/main.rs
|
@ -1,3 +1,36 @@
|
||||||
fn main() {
|
use std::process::ExitCode;
|
||||||
println!("Hello, world!");
|
|
||||||
|
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