From 1ddd54676cfd1a9151fc674bc6f2031e9b8168e8 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 18 Nov 2012 19:00:50 -0500 Subject: [PATCH] Goofing around more with mechanics instead of reading the dang book --- .../algs4-binarysearch/main.go | 29 ++++++++++++------- algs4/src/meatballhat.com/algs4/io.go | 10 +++---- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/algs4/src/meatballhat.com/algs4-binarysearch/main.go b/algs4/src/meatballhat.com/algs4-binarysearch/main.go index 170f1b6..0ce9559 100644 --- a/algs4/src/meatballhat.com/algs4-binarysearch/main.go +++ b/algs4/src/meatballhat.com/algs4-binarysearch/main.go @@ -1,19 +1,28 @@ package main import ( - "fmt" - "io/ioutil" - "os" + "bufio" + "fmt" + "os" + "sort" +) + +import ( + "meatballhat.com/algs4" ) func main() { - whiteListTxt, err := ioutil.ReadAll(os.Stdin) + whiteList, err := algs4.ReadInts(bufio.NewReader(os.Stdin)) + sort.Ints(whiteList) + + if err != nil { + fmt.Println("UGH: ", err) + os.Exit(1) + } - if err != nil { - fmt.Println("UGH: ", err) - os.Exit(1) - } + fmt.Println("Sorted whitelist:") - fmt.Println("YAP") - fmt.Println("Got: ", string(whiteListTxt)) + for i := range whiteList { + fmt.Println(i) + } } diff --git a/algs4/src/meatballhat.com/algs4/io.go b/algs4/src/meatballhat.com/algs4/io.go index 624a137..0c4799c 100644 --- a/algs4/src/meatballhat.com/algs4/io.go +++ b/algs4/src/meatballhat.com/algs4/io.go @@ -7,10 +7,10 @@ import ( "strings" ) -func ReadInts(inbuf io.Reader) ([]int64, error) { - ints := make([]int64, 0) +func ReadInts(inbuf io.Reader) ([]int, error) { + ints := make([]int, 0) - var i int64 + var i64 int64 fileReader := bufio.NewReader(inbuf) line, err := fileReader.ReadString('\n') @@ -19,13 +19,13 @@ func ReadInts(inbuf io.Reader) ([]int64, error) { line = strings.TrimSpace(line) if len(line) > 0 { - i, err = strconv.ParseInt(line, 10, 0) + i64, err = strconv.ParseInt(line, 10, 32) if err != nil { return nil, err } - ints = append(ints, i) + ints = append(ints, int(i64)) } line, err = fileReader.ReadString('\n')