Exemple #1
0
/****************************************************************************
 *  Data files:   http://algs4.cs.princeton.edu/15uf/tinyUF.txt
 *                http://algs4.cs.princeton.edu/15uf/mediumUF.txt
 *                http://algs4.cs.princeton.edu/15uf/largeUF.txt
 *
 *  % ./uf < tinyUF.txt
 *  4 3
 *  3 8
 *  6 5
 *  9 4
 *  2 1
 *  5 0
 *  7 2
 *  6 1
 *  2 components
 *
 ****************************************************************************/
func main() {
	var n int
	stdin := stdin.NewStdin()

	if !stdin.IsEmpty() {
		n, _ = stdin.ReadInt()
	} else {
		panic("No input...")
	}

	// uf := uf.NewQuickFind(n) // initialize n components
	// uf := uf.NewQuickUnion(n) // initialize n components
	// uf := uf.NewWeightedQuickUnion(n) // initialize n components
	uf := uf.NewWeightedQuickUnionWithPathCompression(n) // initialize n components
	var p, q int

	for !stdin.IsEmpty() {
		p, _ = stdin.ReadInt()
		if !stdin.IsEmpty() {
			q, _ = stdin.ReadInt() // read pair to connect
		}

		if uf.Connected(p, q) { // ignore if connected
			continue
		}

		uf.Union(p, q)              // combine components
		fmt.Printf("%d %d\n", p, q) // print connection
	}

	fmt.Printf("%d components\n", uf.Count())
}
Exemple #2
0
// 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)
		}
	}
}
Exemple #3
0
// Dijkstra's Two-Stack Algorithm for Expression Evaluation
// $ ./evaluate
//   ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
//   101.0
// $ ./evaluate
//   ( ( 1 + sqrt ( 5.0 ) ) / 2.0 )
//   1.618033988749895
func main() {
	ops := stack.NewLinked()
	vals := stack.NewLinked()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		s, _ := stdin.ReadString()

		if s == "(" {
			// ignore...
		} else if s == "+" {
			ops.Push(s)
		} else if s == "-" {
			ops.Push(s)
		} else if s == "*" {
			ops.Push(s)
		} else if s == "/" {
			ops.Push(s)
		} else if s == "sqrt" {
			ops.Push(s)
		} else if s == ")" {
			// Pop, evaluate, and push result if token is ")"
			var value float64
			op, _ := ops.Pop()
			v, _ := vals.Pop()

			if op == "+" {
				v1, _ := vals.Pop()
				value = v1.(float64) + v.(float64)
			} else if op == "-" {
				v1, _ := vals.Pop()
				value = v1.(float64) - v.(float64)
			} else if op == "*" {
				v1, _ := vals.Pop()
				value = v1.(float64) * v.(float64)
			} else if op == "/" {
				v1, _ := vals.Pop()
				value = v1.(float64) / v.(float64)
			} else if op == "sqrt" {
				value = math.Sqrt(v.(float64))
			}
			vals.Push(value)
		} else {
			// Token not operator or paren: push value
			value, _ := strconv.ParseFloat(s, 64)
			vals.Push(value)
		}
	}

	result, _ := vals.Pop()
	fmt.Println(result.(float64))
}
Exemple #4
0
/*************************************************************************
 *  Execution: ./stack < input.txt
 *
 *  % more tobe.txt
 *  to be or not to - be - - that - - - is
 *
 *  % ./stack < tobe.txt
 *  to be not that or be (2 left on stack)
 *
 *************************************************************************/
func main() {
	s := stack.NewLinked()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		if item != "-" {
			s.Push(item)
		} else if !s.IsEmpty() {
			item, _ := s.Pop()
			fmt.Printf("%s ", item)
		}
	}

	fmt.Printf("(%d left on stack)\n", s.Size())
}
Exemple #5
0
// Execution: ./queue < input.txt
// Data files: http://algs4.cs.princeton.edu/13stacks/tobe.txt
// % ./queue < tobe.txt
//   to be or not to be (2 left on queue)
func main() {
	q := queue.NewLinked()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		if item != "-" {
			q.Enqueue(item)
		} else if !q.IsEmpty() {
			item, _ := q.Dequeue()
			fmt.Print(item, " ")
		}
	}

	fmt.Printf("(%d left on queue)\n", q.Size())
}
Exemple #6
0
/*************************************************************************
 *  Execution: ./bag < input.txt
 *
 *  % more tobe.txt
 *  to be or not to - be - - that - - - is
 *
 *  % ./bag < tobe.txt
 *  size of bag = 14
 *  is
 *  -
 *  -
 *  -
 *  that
 *  -
 *  -
 *  be
 *  -
 *  to
 *  not
 *  or
 *  be
 *  to
 *************************************************************************/
func main() {
	bag := bag.NewLinked()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		bag.Add(item)
	}

	fmt.Println("size of bag =", bag.Size())
	bag.Each(func(item interface{}) {
		fmt.Println(item)
	})
	// for e := range bag.Iter() {
	// 	fmt.Println(e)
	// }
}
Exemple #7
0
func main() {
	sum := 0.0
	cnt := 0

	stdin := stdin.NewStdin()
	for !stdin.IsEmpty() {
		number, err := stdin.ReadFloat()
		if err != nil {
			log.Fatal(err)
		}

		sum += number
		cnt++
	}

	avg := sum / float64(cnt)
	fmt.Printf("Average is %.5f\n", avg)
}
Exemple #8
0
func main() {
	q := queue.NewSliced()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		if item != "-" {
			q.Enqueue(item)
		} else if !q.IsEmpty() {
			item, _ := q.Dequeue()
			fmt.Printf("%s ", item)
		}
	}

	fmt.Printf("(%d left on queue)\n", q.Size())
	q.Each(func(item interface{}) {
		fmt.Println(item)
	})
}
Exemple #9
0
/*************************************************************************
 *  Execution:  ./slicedstack < input.txt
 *  Data files: http://algs4.cs.princeton.edu/13stacks/tobe.txt
 *
 *  % more tobe.txt
 *  to be or not to - be - - that - - - is
 *
 *  % ./slicedstack < tobe.txt
 *  to be not that or be (2 left on stack)
 *
 *************************************************************************/
func main() {
	s := stack.NewSliced()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		if item != "-" {
			s.Push(item)
		} else if !s.IsEmpty() {
			item, _ := s.Pop()
			fmt.Printf("%s ", item)
		}
	}

	fmt.Printf("(%d left on stack)\n", s.Size())
	s.Each(func(item interface{}) {
		fmt.Println(item)
	})
}