mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
823 B
54 lines
823 B
5 years ago
|
package correct
|
||
5 years ago
|
|
||
5 years ago
|
import "strings"
|
||
5 years ago
|
|
||
5 years ago
|
var m = map[rune]struct{}{}
|
||
5 years ago
|
|
||
5 years ago
|
func uniqueChar(s string) bool {
|
||
|
for _, r := range s {
|
||
|
if _, ok := m[r]; ok {
|
||
5 years ago
|
return false
|
||
|
}
|
||
5 years ago
|
m[r] = struct{}{}
|
||
5 years ago
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
5 years ago
|
func validBase(base string) bool {
|
||
|
return len(base) >= 2 && !strings.ContainsAny(base, "+-") && uniqueChar(base)
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
func power(nbr int, pwr int) int {
|
||
5 years ago
|
if pwr == 0 {
|
||
|
return 1
|
||
|
}
|
||
|
if pwr == 1 {
|
||
|
return nbr
|
||
|
}
|
||
5 years ago
|
return nbr * power(nbr, pwr-1)
|
||
5 years ago
|
}
|
||
|
|
||
|
func AtoiBase(s string, base string) int {
|
||
|
var result int
|
||
|
var i int
|
||
|
sign := 1
|
||
|
lengthBase := len(base)
|
||
|
lengths := len(s)
|
||
|
|
||
5 years ago
|
if !validBase(base) {
|
||
5 years ago
|
return 0
|
||
|
}
|
||
|
if s[i] == '-' {
|
||
|
sign = -1
|
||
|
}
|
||
|
if s[i] == '-' || s[i] == '+' {
|
||
|
i++
|
||
|
}
|
||
|
for i < len(s) {
|
||
5 years ago
|
result += (Index(base, string(s[i])) * power(lengthBase, lengths-1))
|
||
5 years ago
|
i++
|
||
|
lengths--
|
||
|
}
|
||
|
return result * sign
|
||
|
}
|