Example #1
0
func main() {
	ary := sort.StringSlice{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
	for i := 1; i < 1e6; i++ {
		mathutil.PermutationNext(ary)
	}
	fmt.Println(strings.Join(ary, ""))
}
Example #2
0
func main() {
	count := 0
	for mathutil.PermutationNext(nums) {
		count += check_patition(0, 0)
	}
	fmt.Println(count)
}
Example #3
0
File: 32.go Project: Rosalita/euler
func main() {

	// find number of permuations for 123456789.
	// there will be !9 permutations which is 362880

	myperm := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} // initialise myperm with the lowest value permutation

	var permslice []string // initialise a slice of strings

	var buffer bytes.Buffer // make a new buffer
	sstart := "123456789"

	permslice = append(permslice, sstart)
	for i := 1; i < 10; i++ { // set to < 10 while testing this will eventually be < 362880
		mathutil.PermutationNext(perm(myperm)) // generate the next permuation

		for _, v := range myperm {
			temp := strconv.Itoa(v)  // convert each item in the []int to string
			buffer.WriteString(temp) // store in a buffer
		}
		mystring := buffer.String()             // pull strings out of the buffer
		permslice = append(permslice, mystring) // store them in the []string
		buffer.Reset()
	}
	fmt.Println(permslice) // prints all permutations in string format

	// store perms as strings in a slice - done see above

	// for each permuation
	// work through this slice taking bits at the start, multiplying them and seeing if result contains any digits present in the multipliers

}
Example #4
0
func main() {
	permdata := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	for i := 1; i < 1000000; i++ {
		mathutil.PermutationNext(data(permdata))
	}
	fmt.Println(permdata)
}
Example #5
0
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
		}

	}
}
Example #6
0
func main() {
	ary := Uint64s{7, 6, 5, 4, 3, 2, 1}
	var num uint64
	for mathutil.PermutationNext(ary) {
		num = 0
		for _, v := range ary {
			num = 10*num + v
		}
		if mathutil.IsPrimeUint64(num) {
			fmt.Println(num)
			break
		}
	}
}
Example #7
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)
}
Example #8
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)
}
Example #9
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)
	}
}
Example #10
0
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
		}

	}
}