Skip to content

Commit 9e7fc67

Browse files
committedNov 28, 2016
Added practice problems and solutions for binary search
1 parent 9ae2cc7 commit 9e7fc67

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// Given a list of sorted words (strings with no spaces),
6+
// search for a user provided word in the list without
7+
// being case sensitive. Return -1 if the word isn't found
8+
// and return the index of the word if it is found.
9+
func main() {
10+
var words []string = []string{
11+
"ALLigator",
12+
"bat",
13+
"bEEtle",
14+
"camel",
15+
"cat",
16+
"cheetah",
17+
"COLT",
18+
"cow",
19+
"dog",
20+
"eagle",
21+
"froG",
22+
"hamster",
23+
"horse",
24+
"mink",
25+
"moose",
26+
"porcupine",
27+
"RaT",
28+
"rooster",
29+
"steer",
30+
}
31+
fmt.Println("Sorted Words:", words)
32+
var toFind string
33+
fmt.Println("What word should we search for? No spaces please!")
34+
fmt.Scanf("%s", &toFind)
35+
var index int
36+
index = binSearch(words, toFind)
37+
if index < 0 {
38+
fmt.Println("The word", toFind, "could not be found!")
39+
} else {
40+
fmt.Println("The word", toFind, "was found at index:", index, words[index])
41+
}
42+
}
43+
44+
func binSearch(words []string, word string) int {
45+
return -1
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// Given a list of sorted words (strings with no spaces),
9+
// search for a user provided word in the list without
10+
// being case sensitive. Return -1 if the word isn't found
11+
// and return the index of the word if it is found.
12+
func main() {
13+
var words []string = []string{
14+
"ALLigator",
15+
"bat",
16+
"bEEtle",
17+
"camel",
18+
"cat",
19+
"cheetah",
20+
"COLT",
21+
"cow",
22+
"dog",
23+
"eagle",
24+
"froG",
25+
"hamster",
26+
"horse",
27+
"mink",
28+
"moose",
29+
"porcupine",
30+
"RaT",
31+
"rooster",
32+
"steer",
33+
}
34+
fmt.Println("Sorted Words:", words)
35+
var toFind string
36+
fmt.Println("What word should we search for? No spaces please!")
37+
fmt.Scanf("%s", &toFind)
38+
var index int
39+
index = binSearch(words, toFind)
40+
if index < 0 {
41+
fmt.Println("The word", toFind, "could not be found!")
42+
} else {
43+
fmt.Println("The word", toFind, "was found at index:", index, words[index])
44+
}
45+
}
46+
47+
func binSearch(words []string, word string) int {
48+
var lo int = 0
49+
var hi int = len(words) - 1
50+
51+
for lo <= hi {
52+
var mid int = lo + (hi-lo)/2
53+
var midValue string = words[mid]
54+
55+
if compare(midValue, word) == 0 {
56+
return mid
57+
} else if compare(midValue, word) > 0 {
58+
// We want to use the left half of our list
59+
hi = mid - 1
60+
} else {
61+
// We want to use the right half of our list
62+
lo = mid + 1
63+
}
64+
}
65+
66+
// If we get here we tried to look at an invalid sub-list
67+
// which means the number isn't in our list.
68+
return -1
69+
}
70+
71+
func compare(a, b string) int {
72+
var aLow string = strings.ToLower(a)
73+
var bLow string = strings.ToLower(b)
74+
if aLow == bLow {
75+
return 0
76+
} else if aLow < bLow {
77+
return -1
78+
} else {
79+
return 1
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// Use a binary search to find the square root of a number
6+
func main() {
7+
var number float64 = 128.0
8+
fmt.Println("Number:", number)
9+
var squareRoot float64 = squareRoot(number)
10+
fmt.Println("Square root:", squareRoot)
11+
}
12+
13+
func squareRoot(n float64) float64 {
14+
return n
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// Use a binary search to find the square root of a number
6+
func main() {
7+
var number float64 = 128.0
8+
fmt.Println("Number:", number)
9+
var squareRoot float64 = squareRoot(number)
10+
fmt.Println("Square root:", squareRoot)
11+
}
12+
13+
func squareRoot(n float64) float64 {
14+
lo := 0.0
15+
hi := n
16+
for i := 0; i < 2000; i++ {
17+
mid := lo + (hi-lo)/2.0
18+
if mid*mid > n {
19+
hi = mid
20+
} else {
21+
lo = mid
22+
}
23+
}
24+
return lo
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.