예제 #1
0
파일: main.go 프로젝트: jvillasante/algs4
// This program takes the name of a whitelist file (a sequence of integers) as argument and filters any
// entry that is on the whitelist from standard input, leaving only integers that are not on the whitelist
// on standard output. It uses the binary search algorithm, implemented in the method Rank(),
// to accomplish the task efficiently.
func main() {
	if len(os.Args) <= 1 {
		fmt.Printf("usage: %s tinyW.txt < tinyT.txt\n", filepath.Base(os.Args[0]))
		return
	}

	whitelist, err := in.ReadAllIntsFromFile(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	sort.Ints(whitelist)

	stdin := stdin.NewStdin()
	for !stdin.IsEmpty() {
		// Read key, print if not in whitelist
		key, err := stdin.ReadInt()
		if err != nil {
			log.Fatal(err)
		}

		if bsearch.Rank(key, whitelist) == -1 {
			fmt.Println(key)
		}
	}
}
예제 #2
0
파일: main.go 프로젝트: jvillasante/algs4
func main() {
	if len(os.Args) <= 1 {
		fmt.Printf("usage: %s [1Kints|2Kints|4Kints|8Kints].txt\n", filepath.Base(os.Args[0]))
		return
	}

	a, _ := in.ReadAllIntsFromFile(os.Args[1])

	timer := stopwatch.New()
	cnt := threesum.Count(a)
	fmt.Printf("elapsed time: %.2f\n", timer.ElapsedTime())
	fmt.Println(cnt)
}