Exemplo n.º 1
0
func main() {

	var (
		scanner   *io.Scanner
		input     *io.Scanner
		err       error
		f         *os.File
		whitelist []int
		key       int
	)
	if len(os.Args) < 2 {
		fmt.Println("args")
		os.Exit(1)
	}
	fname := os.Args[1]
	if f, err = os.Open(fname); err != nil {
		fmt.Println("dud file")
		os.Exit(1)
	}
	scanner = io.NewScanner(f)
	input = io.NewScanner(os.Stdin)
	if whitelist, err = scanner.ReadAllInts(); err != nil {
		fmt.Println("dud whitelist")
		os.Exit(1)
	}
	sort.Ints(whitelist)

	key, err = input.ReadInt()
	for err == nil {
		if rank(key, whitelist) == -1 {
			fmt.Println(key)
		}
		key, err = input.ReadInt()
	}
}
func main() {

	in, err := os.Open(os.Args[1])
	if err != nil {
		log.Printf("error: %s", err)
		os.Exit(1)
	}
	s := io.NewScanner(in)
	size, err := s.ReadInt()
	if err != nil {
		panic(err)
	}

	log.Printf("N: %d, Log2(N): %f\n", size, math.Log2(float64(size)))

	parent = make([]int, size, size)
	sz = make([]int, size, size)

	for i := 0; i < len(parent); i++ {
		parent[i] = i
		sz[i] = 1
	}

	line, err := s.ReadLine()
	var p, q int
	// count := 0
	for err == nil {
		fmt.Sscanf(line, "%d %d", &p, &q)
		if !connected(p, q) {
			union(p, q)
		}
		line, err = s.ReadLine()
	}

	stdIn := io.NewScanner(os.Stdin)

	//fmt.Println(parent)

	m := make(map[int]int)
	for _, v := range parent {
		m[v] += 1
	}

	fmt.Println(m)
	for {
		p, _ := stdIn.ReadInt()
		q, _ := stdIn.ReadInt()
		fmt.Println(connected(p, q))
	}

}
Exemplo n.º 3
0
func main() {
	stdIn := io.NewScanner(os.Stdin)

	line, err := stdIn.ReadLine()
	//fmt.Println(token, err)
	for err == nil {
		//fmt.Println(token)
		tokens := strings.FieldsFunc(line, func(r rune) bool {
			switch r {
			case ' ', '\t', '\n', '\r', '.', ',', '-', '"', '!', ';', '?', ':', '(', ')':
				return true
			}
			return false
		})
		for _, token := range tokens {
			fmt.Println(token)
		}
		line, err = stdIn.ReadLine()
	}
}
Exemplo n.º 4
0
func main() {

	f, err := os.Open(os.Args[1])
	if err != nil {
		log.Printf("error: %s", err)
		os.Exit(1)
	}
	s := io.NewScanner(f)
	size, err := s.ReadInt()
	if err != nil {
		panic(err)
	}

	ids = make([]int, size, size)

	for i := 0; i < len(ids); i++ {
		ids[i] = i
	}

	line, err := s.ReadLine()
	var p, q int
	count := 0
	for err == nil {
		fmt.Sscanf(line, "%d %d", &p, &q)
		if !connected(p, q) {
			union(p, q)
		}
		count += 1
		if count%1000 == 0 {
			log.Println(count)
		}
		line, err = s.ReadLine()
	}

	fmt.Println(ids)

}
Exemplo n.º 5
0
func useStdIn(algorithm string, datatype string) {

	stdIn := io.NewScanner(os.Stdin)

	//var result sort.Interface
	var a sort.Interface

	switch datatype {
	case "string":
		log.Print("Reading tokens...")
		tokens, err := stdIn.ReadAllTokens()
		if err != nil {
			panic(err)
		}
		log.Printf("done, tokens: %d", len(tokens))
		// for _, token := range tokens {
		// 	fmt.Println(token)
		// }
		a = sort.Strings(tokens)
	case "int":
		log.Print("Reading ints...")
		ints, err := stdIn.ReadAllInts()
		if err != nil {
			panic(err)
		}
		log.Printf("done, ints: %d", len(ints))
		a = sort.Ints(ints)
	}
	log.Println("Starting sort")
	t := timeSort(algorithm, a)

	//log.Println(a)
	log.Printf("Sort done, algorithm: %s time: %s\n", algorithm, t)
	//os.Exit(result.Len())
	//fmt.Println(result)

}