Filling in example binary search thing with real implementation
This commit is contained in:
parent
0c300d7cdd
commit
e99e5165bd
@ -11,18 +11,38 @@ import (
|
||||
"meatballhat.com/algs4"
|
||||
)
|
||||
|
||||
func main() {
|
||||
whiteList, err := algs4.ReadInts(bufio.NewReader(os.Stdin))
|
||||
sort.Ints(whiteList)
|
||||
const USAGE string = `Usage: algs4-binarysearch <whitelist-file>
|
||||
|
||||
You must also provide input on stdin.`
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println(USAGE)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
whiteListFile, err := os.Open(string(os.Args[1]))
|
||||
if err != nil {
|
||||
fmt.Println("Failed to open whitelist file:", err)
|
||||
}
|
||||
|
||||
whiteList, err := algs4.ReadInts(bufio.NewReader(whiteListFile))
|
||||
if err != nil {
|
||||
fmt.Println("UGH: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("Sorted whitelist:")
|
||||
sort.Ints(whiteList)
|
||||
|
||||
for i := range whiteList {
|
||||
fmt.Println(i)
|
||||
inputs, err := algs4.ReadInts(bufio.NewReader(os.Stdin))
|
||||
if err != nil {
|
||||
fmt.Println("Failed to read inputs:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for _, key := range inputs {
|
||||
if algs4.BinarySearchRank(key, whiteList) == -1 {
|
||||
fmt.Println(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user