From d9315ebdd706ad324120770ed7cbb591a6af039e Mon Sep 17 00:00:00 2001 From: miguel Date: Mon, 22 Apr 2024 18:45:07 +0100 Subject: [PATCH] feat(canjump) new exam exercise --- subjects/canjump/README.md | 39 ++++++++++++++++++++++++++++++++++++++ subjects/canjump/main.go | 17 +++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 subjects/canjump/README.md create mode 100644 subjects/canjump/main.go diff --git a/subjects/canjump/README.md b/subjects/canjump/README.md new file mode 100644 index 000000000..f9e72a067 --- /dev/null +++ b/subjects/canjump/README.md @@ -0,0 +1,39 @@ +### Can Jump + +Given an array of integers representing the maximum number of steps you can take forward from each position, implement the function `CanJump()` which takes an `integer slice` as input and returns a `boolean` value to determine if it's possible to reach the last index starting from the first index based on these maximum steps. The function should return `true` if it's possible to reach the last index and `false` otherwise. + +> Note: The function only needs to consider positive numbers or zero in the array of steps. Also remember if the input has only one element that is the last position in the array so the function will return `true`. + +### Usage + +Here is a possible program to test your function: + +```go +package main + +import ( + "fmt" + "piscine" +) + +func main() { + input1 := []int{2, 3, 1, 1, 4} + fmt.Println(piscine.CanJump(input1)) + + input2 := []int{3, 2, 1, 0, 4} + fmt.Println(piscine.CanJump(input2)) + + input3 := []int{0} + fmt.Println(piscine.CanJump(input3)) +} +``` + +And its output : + +```console +$ go run . +true +false +true +$ +``` diff --git a/subjects/canjump/main.go b/subjects/canjump/main.go new file mode 100644 index 000000000..afeec8c92 --- /dev/null +++ b/subjects/canjump/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "piscine" +) + +func main() { + input1 := []int{2, 3, 1, 1, 4} + fmt.Println(piscine.CanJump(input1)) + + input2 := []int{3, 2, 1, 0, 4} + fmt.Println(piscine.CanJump(input2)) + + input3 := []int{0} + fmt.Println(piscine.CanJump(input3)) +}