Goofing around more with mechanics instead of reading the dang book

This commit is contained in:
Dan Buch 2012-11-18 19:00:50 -05:00
parent 3f696f3060
commit 1ddd54676c
2 changed files with 24 additions and 15 deletions

View File

@ -1,19 +1,28 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"sort"
)
import (
"meatballhat.com/algs4"
) )
func main() { func main() {
whiteListTxt, err := ioutil.ReadAll(os.Stdin) whiteList, err := algs4.ReadInts(bufio.NewReader(os.Stdin))
sort.Ints(whiteList)
if err != nil { if err != nil {
fmt.Println("UGH: ", err) fmt.Println("UGH: ", err)
os.Exit(1) os.Exit(1)
} }
fmt.Println("YAP") fmt.Println("Sorted whitelist:")
fmt.Println("Got: ", string(whiteListTxt))
for i := range whiteList {
fmt.Println(i)
}
} }

View File

@ -7,10 +7,10 @@ import (
"strings" "strings"
) )
func ReadInts(inbuf io.Reader) ([]int64, error) { func ReadInts(inbuf io.Reader) ([]int, error) {
ints := make([]int64, 0) ints := make([]int, 0)
var i int64 var i64 int64
fileReader := bufio.NewReader(inbuf) fileReader := bufio.NewReader(inbuf)
line, err := fileReader.ReadString('\n') line, err := fileReader.ReadString('\n')
@ -19,13 +19,13 @@ func ReadInts(inbuf io.Reader) ([]int64, error) {
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if len(line) > 0 { if len(line) > 0 {
i, err = strconv.ParseInt(line, 10, 0) i64, err = strconv.ParseInt(line, 10, 32)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ints = append(ints, i) ints = append(ints, int(i64))
} }
line, err = fileReader.ReadString('\n') line, err = fileReader.ReadString('\n')