2012-11-18 13:30:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-11-19 00:00:50 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
import (
|
2012-12-20 02:10:56 +00:00
|
|
|
"github.com/meatballhat/box-o-sand/algs4/go/algs4"
|
2012-11-18 13:30:49 +00:00
|
|
|
)
|
|
|
|
|
2012-11-19 01:20:29 +00:00
|
|
|
const USAGE string = `Usage: algs4-binarysearch <whitelist-file>
|
|
|
|
|
|
|
|
You must also provide input on stdin.`
|
|
|
|
|
2012-11-18 13:30:49 +00:00
|
|
|
func main() {
|
2012-11-19 01:20:29 +00:00
|
|
|
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)
|
|
|
|
}
|
2012-11-19 00:00:50 +00:00
|
|
|
|
2012-11-19 01:20:29 +00:00
|
|
|
whiteList, err := algs4.ReadInts(bufio.NewReader(whiteListFile))
|
2012-11-19 00:00:50 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("UGH: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2012-11-18 13:30:49 +00:00
|
|
|
|
2012-11-19 01:20:29 +00:00
|
|
|
sort.Ints(whiteList)
|
|
|
|
|
|
|
|
inputs, err := algs4.ReadInts(bufio.NewReader(os.Stdin))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to read inputs:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2012-11-18 13:30:49 +00:00
|
|
|
|
2012-11-19 01:20:29 +00:00
|
|
|
for _, key := range inputs {
|
|
|
|
if algs4.BinarySearchRank(key, whiteList) == -1 {
|
|
|
|
fmt.Println(key)
|
|
|
|
}
|
2012-11-19 00:00:50 +00:00
|
|
|
}
|
2012-11-18 13:30:49 +00:00
|
|
|
}
|