Added documentation

This commit is contained in:
BoolPurist 2024-08-06 10:24:46 +02:00
parent 39c3fae613
commit f16c2aa665
3 changed files with 64 additions and 9 deletions

19
README.md Normal file
View 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

View file

@ -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,6 +80,7 @@ mod testing {
} }
#[test] #[test]
// Example 1:
// Input: nums = [2,3,1,1,4] // Input: nums = [2,3,1,1,4]
// Output: true // Output: true
// Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. // Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
@ -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;

View file

@ -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"
} }