Skip to content

Instantly share code, notes, and snippets.

@shiny
Created January 17, 2017 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiny/7e4f64c2b72677d4cdc5ddfea8af638f to your computer and use it in GitHub Desktop.
Save shiny/7e4f64c2b72677d4cdc5ddfea8af638f to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "strings"
import "math"
func main() {
fmt.Println(get_num("r9"))
fmt.Println(generate_code(840))
}
func get_num (code string) int {
codes := "abcdefghjkmnpqrstuvwxyz23456789ABCDEFGHJKMNPQRSTUVWXYZ"
num := 0
i := len(code)
codes_len := len(codes)
for j:=0; j<len(code); j++ {
i--
char := string(code[j])
pos := strings.Index(codes, char)
tmp := math.Pow(float64(codes_len), float64(i))
num += int(tmp) * (pos + 1)
}
num--
return num
}
func generate_code (number int) string {
var out = ""
var key = 0
codes := "abcdefghjkmnpqrstuvwxyz23456789ABCDEFGHJKMNPQRSTUVWXYZ"
codes_len := len(codes)
for {
if number < codes_len {
break
}
key = number % codes_len
number = int(number / codes_len) - 1
out = string(codes[key]) + out
}
return string(codes[number]) + out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment