From b1333fead8e40c335314752a117edafdab4ad19d Mon Sep 17 00:00:00 2001 From: miguel Date: Mon, 22 Apr 2024 18:45:07 +0100 Subject: [PATCH 1/3] 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)) +} From 53f8044e3d9b3be5c7537b7295152e6ca075bf5a Mon Sep 17 00:00:00 2001 From: miguel Date: Mon, 6 May 2024 18:23:41 +0100 Subject: [PATCH 2/3] fix(canjump): change to uint --- subjects/canjump/README.md | 12 ++++++------ subjects/canjump/main.go | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/subjects/canjump/README.md b/subjects/canjump/README.md index f9e72a067..2c618ba8b 100644 --- a/subjects/canjump/README.md +++ b/subjects/canjump/README.md @@ -1,8 +1,8 @@ -### Can Jump +## 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. +Given an array of non-negative integers representing the maximum number of steps you can take forward from each position, implement the function `CanJump()` which takes a `[]uint` 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 and stay at the last index without stepping out of the array 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`. +> Note: Remember, if the input has only one element, that is the last position in the array so the function will return `true` but if the array is empty it returns `false`. ### Usage @@ -17,13 +17,13 @@ import ( ) func main() { - input1 := []int{2, 3, 1, 1, 4} + input1 := []uint{2, 3, 1, 1, 4} fmt.Println(piscine.CanJump(input1)) - input2 := []int{3, 2, 1, 0, 4} + input2 := []uint{3, 2, 1, 0, 4} fmt.Println(piscine.CanJump(input2)) - input3 := []int{0} + input3 := []uint{0} fmt.Println(piscine.CanJump(input3)) } ``` diff --git a/subjects/canjump/main.go b/subjects/canjump/main.go index afeec8c92..144f46f76 100644 --- a/subjects/canjump/main.go +++ b/subjects/canjump/main.go @@ -6,12 +6,12 @@ import ( ) func main() { - input1 := []int{2, 3, 1, 1, 4} + input1 := []uint{2, 3, 1, 1, 4} fmt.Println(piscine.CanJump(input1)) - input2 := []int{3, 2, 1, 0, 4} + input2 := []uint{3, 2, 1, 0, 4} fmt.Println(piscine.CanJump(input2)) - input3 := []int{0} + input3 := []uint{0} fmt.Println(piscine.CanJump(input3)) } From cb54c233278521d46ea0daff2ff2b08e8aa6020f Mon Sep 17 00:00:00 2001 From: miguel Date: Wed, 8 May 2024 17:58:47 +0100 Subject: [PATCH 3/3] docs(canjump) improve readme --- subjects/canjump/README.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/subjects/canjump/README.md b/subjects/canjump/README.md index 2c618ba8b..604d584ac 100644 --- a/subjects/canjump/README.md +++ b/subjects/canjump/README.md @@ -1,8 +1,37 @@ ## Can Jump -Given an array of non-negative integers representing the maximum number of steps you can take forward from each position, implement the function `CanJump()` which takes a `[]uint` 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 and stay at the last index without stepping out of the array and `false` otherwise. +Given an array of non-negative integers representing the number of steps you can take forward from each position, implement the function `CanJump()` which takes a slice of unsigned integers `[]uint` as input and returns a `boolean` value. This function should determine if it's possible to reach and stay at the last index of the array starting from the first index, based on the steps you need to advance. Be aware that: -> Note: Remember, if the input has only one element, that is the last position in the array so the function will return `true` but if the array is empty it returns `false`. +- Each value represents the exact number of steps you must take forward from that position. +- The function should return `true` if it's possible to reach and stay at the last index without stepping out of the array, and `false` otherwise. +- If the input has only one element, that is the last position in the array so the function will return `true` but if the array is empty it returns `false`. + +Let's take the example array input := []uint{2, 3, 1, 1, 4}. + +```console + +Position: 0 1 2 3 4 +Steps: 2 3 1 1 4 + ^ + +// Starting from position 0, you have 2 steps to move forward. This means you will move to positions 2. + +Position: 0 1 2 3 4 +Steps: 2 3 1 1 4 + ^ + +// From position 2, you have 1 step, so you will move to position 3. + +Position: 0 1 2 3 4 +Steps: 2 3 1 1 4 + ^ + +// Finally, from position 3, you have 1 step to reach the last index at position 4 confirming that it's possible so the output will be "True". + +Position: 0 1 2 3 4 +Steps: 2 3 1 1 4 + ^ +``` ### Usage