update 05.03.24

This commit is contained in:
ZennDev1337 2024-03-05 10:13:43 +01:00
commit 026061851d
6 changed files with 74 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/binary-search.iml generated Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/binary-search.iml" filepath="$PROJECT_DIR$/.idea/binary-search.iml" />
</modules>
</component>
</project>

1
go.mod Normal file
View file

@ -0,0 +1 @@
module binary-search

26
main.go Normal file
View file

@ -0,0 +1,26 @@
package main
import (
"errors"
"fmt"
)
func binarySearch(nums []int, target int) (int, error) {
low := 0
high := len(nums) - 1
for low <= high {
mid := low + (high-low)/2
if nums[mid] > target {
high = mid - 1
} else if nums[mid] < target {
low = mid + 1
} else if nums[mid] == target {
return mid, nil
}
}
return -1, errors.New("binarySearch: your target is not found in the given array")
}
func main() {
fmt.Println("Go Fast!")
}

22
main_test.go Normal file
View file

@ -0,0 +1,22 @@
package main
import (
"testing"
)
func TestBinarySearchWithWorkingTarget(t *testing.T) {
nums := []int{2, 3, 5, 7, 8}
target := 7
resu, err := binarySearch(nums, target)
if err != nil || resu != 3 {
t.Errorf("Test failed! expected: %v, but got: %v", 3, resu)
}
}
func TestBinarySearchWithNonWorkingTarget(t *testing.T) {
nums := []int{1, 4, 5, 8, 9}
target := 2
resu, err := binarySearch(nums, target)
if err == nil || resu != -1 {
t.Errorf("Test failed! expected: %v, but got: %v", "binarySearch: your target is not found in the given array", err)
}
}