8

[Golang] Lexicographic permutations - Problem 24 - Project Euler

 3 years ago
source link: http://siongui.github.io/2018/03/02/go-lexicographic-permutations-problem-24-project-euler/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

[Golang] Lexicographic permutations - Problem 24 - Project Euler

March 02, 2018

Problem: [1]

A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

Solution:

My previous post [2] will print the permutations of distinct characters in lexicographic order, so I count the prints and get the answer.

2785960341

package main

var count = 0

// Swap the i-th byte and j-th byte of the string
func swap(s string, i, j int) string {
      var result []byte
      for k := 0; k < len(s); k++ {
              if k == i {
                      result = append(result, s[j])
              } else if k == j {
                      result = append(result, s[i])
              } else {
                      result = append(result, s[k])
              }
      }
      return string(result)
}

// Function to find all Permutations of a given string str[i:n]
// containing all distinct characters
func permutations(str string, i, n int) {
      // base condition
      if i == n-1 {
              count += 1
              if count == 1000000 {
                      println(str)
              }
              return
      }

      // process each character of the remaining string
      for j := i; j < n; j++ {
              // swap character at index i with current character
              str = swap(str, i, j)

              // recursion for string [i+1:n]
              permutations(str, i+1, n)

              // backtrack (restore the string to its original state)
              str = swap(str, i, j)
      }
}

func main() {
      str := "0123456789"
      permutations(str, 0, len(str))
}

Tested on: Go 1.10 (not runable on Go Playground because process took too long)

Run Code on Go Playground


References:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK