Created
November 4, 2020 05:39
-
-
Save arkadyark/84cb62f8ea5a8a623b457ed3ff9f7c25 to your computer and use it in GitHub Desktop.
cassidoo_problem_2020_11_04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
You have a character who jumps forward n number of units at a time, and an array representing the road in front of them (where 0 is a flat piece of road, and 1 is an obstacle). Return true or false if your character can jump to the end without hitting an obstacle in front of them. | |
Trying it out in rust this time! | |
Example: | |
character_jump(3, [0,1,0,0,0,1,0]) -> true | |
character_jump(3, [0,1,0,1,0,1,0]) -> false | |
*/ | |
fn character_jump(step_size: u32, road: &[u32]) -> bool { | |
let mut index: usize = 0; | |
while (index) < road.len() { | |
if road[index] == 1 { | |
return false; | |
} | |
index += step_size as usize; | |
} | |
true | |
} | |
fn main() { | |
if character_jump(3, &[0,1,0,0,0,1,0]) == true && | |
character_jump(3, &[0,1,0,1,0,1,0]) == false && | |
character_jump(4, &[0,1,1,0,1,0,0,0,0]) == false { | |
println!("All tests passed!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment