Created
January 17, 2017 10:37
-
-
Save shiny/7e4f64c2b72677d4cdc5ddfea8af638f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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