示例#1
0
文件: all_test.go 项目: cznic/dns
func TestTreeAdd(t *testing.T) {
	data := sort.StringSlice{
		".",
		"1.",
		"1.0.",
		"1.1.",
		"1.0.0.",
		"1.0.1.",
		"1.1.0.",
		//"1.1.1.",
	}

	n := 1
	for mathutil.PermutationFirst(data); ; n++ {
		m := map[string]bool{}
		tr := NewTree()
		for _, owner := range data {
			tr.Add(owner, owner, func(interface{}) interface{} {
				t.Fatal(10)
				panic("unreachable")
			})
			m[owner] = true
			for k := range m {
				x := tr.Get(k)
				if s, ok := x.(string); !ok {
					t.Fatalf("owner %q: got '%T', expected 'string'", owner, x)
				} else {
					if s != k {
						t.Fatalf("got %q, expected %q", s, k)
					}
				}
			}
		}
		for _, owner := range data {
			tr.Add(owner, owner, func(data interface{}) interface{} {
				return "!" + data.(string)
			})
		}
		for k := range m {
			x := tr.Get(k)
			if s, ok := x.(string); !ok {
				t.Fatalf("owner %q: got '%T', expected 'string'", k, x)
			} else {
				if s != "!"+k {
					t.Fatalf("got %q, expected !%q", s, k)
				}
			}
		}

		if !mathutil.PermutationNext(data) {
			break
		}

	}
}
示例#2
0
func main() {
	var people_happy = make(map[Pair]int)
	var people = []string{"Alice",
		"Bob",
		"Carol",
		"David",
		"Eric",
		"Frank",
		"George",
		"Mallory",
		"Me"}

	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		var person1, person2 string
		var happy_value int
		if strings.Contains(scanner.Text(), "gain") {
			fmt.Sscanf(scanner.Text(), "%s would gain %d happiness units by sitting next to %s", &person1, &happy_value, &person2)
			AddCost(Pair{person1, person2}, happy_value, &people_happy)
		} else {
			fmt.Sscanf(scanner.Text(), "%s would lose %d happiness units by sitting next to %s", &person1, &happy_value, &person2)
			AddCost(Pair{person1, person2}, -happy_value, &people_happy)
		}
	}

	fmt.Println(len(people_happy))

	var data = sort.IntSlice{0, 1, 2, 3, 4, 5, 6, 7, 8}
	i := 0
	var shortest_distance = math.MaxInt64
	var longest_distance = 0
	for mathutil.PermutationFirst(data); ; i++ {
		permutation_distance := LengthOfCombination(data, people, people_happy)
		if permutation_distance < shortest_distance {
			shortest_distance = permutation_distance
		}

		if permutation_distance > longest_distance {
			longest_distance = permutation_distance
		}
		if !mathutil.PermutationNext(data) {
			break
		}
	}

	fmt.Println("Shortest distance", shortest_distance)
	fmt.Println("Longest distance", longest_distance)
}
示例#3
0
func main() {
	var city_dist = make(map[CityPair]int)
	var cities = []string{"AlphaCentauri",
		"Snowdin",
		"Tambi",
		"Faerun",
		"Norrath",
		"Straylight",
		"Tristram",
		"Arbre"}

	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		var city1, city2 string
		var distance int
		fmt.Sscanf(scanner.Text(), "%s to %s = %d", &city1, &city2, &distance)
		AddCityCost(CityPair{city1, city2}, distance, &city_dist)
	}

	fmt.Println(city_dist)

	var data = sort.IntSlice{0, 1, 2, 3, 4, 5, 6, 7}
	i := 0
	var shortest_distance = math.MaxInt64
	var longest_distance = 0
	for mathutil.PermutationFirst(data); ; i++ {
		// fmt.Println(data)
		permutation_distance := LengthOfCombination(data, cities, city_dist)
		if permutation_distance < shortest_distance {
			shortest_distance = permutation_distance
		}

		if permutation_distance > longest_distance {
			longest_distance = permutation_distance
		}
		if !mathutil.PermutationNext(data) {
			break
		}
	}

	fmt.Println("Shortest distance", shortest_distance)
	fmt.Println("Longest distance", longest_distance)
}
示例#4
0
func Permutations(input []int) [][]int {
	result := [][]int{}
	temp := make([]int, len(input))
	copy(temp, input)
	mathutil.PermutationFirst(sort.IntSlice(temp))
	appendme := make([]int, len(input))
	copy(appendme, temp)
	result = append(result, appendme)

	for {
		done := mathutil.PermutationNext(sort.IntSlice(temp))
		if !done {
			return result
		}
		appendme = make([]int, len(input))
		copy(appendme, temp)
		result = append(result, appendme)
	}
}
示例#5
0
文件: all_test.go 项目: cznic/dns
func TestTreePut2(t *testing.T) {
	data := sort.StringSlice{
		".",
		"1.",
		"1.0.",
		"1.1.",
		"1.0.0.",
		"1.0.1.",
		"1.1.0.",
		//"1.1.1.",
	}

	n := 1
	for mathutil.PermutationFirst(data); ; n++ {
		m := map[string]bool{}
		tr := NewTree()
		for _, owner := range data {
			tr.Put(owner, owner)
			m[owner] = true
			for k := range m {
				x := tr.Get(k)
				if s, ok := x.(string); !ok {
					t.Fatalf("owner %q: got '%T', expected 'string'", owner, x)
				} else {
					if s != k {
						t.Fatalf("got %q, expected %q", s, k)
					}
				}
			}
		}

		if !mathutil.PermutationNext(data) {
			break
		}

	}
}