-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoibase.go
59 lines (53 loc) · 986 Bytes
/
atoibase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package piscine
func AtoiBase(s string, base string) int {
baseF := Lens(base)
result := []rune(base)
pos := 0
if constrains1(baseF, result) {
for i := 0; i < Lens(s); i++ {
pos *= Lens(base)
pos += Index1(s[i:], base)
}
return pos
}
return 0
}
func constrains1(baseF int, result []rune) bool {
if baseF == 1 {
return false
}
for a := 0; a < Lens(string(result)); a++ {
for x := a + 1; x < Lens(string(result)); x++ {
if baseF < 2 || result[a] == result[x] || result[a] == '+' || result[a] == '-' {
return false
}
}
}
return true
}
func Index1(s string, toFind string) int {
arrayStr := []rune(s)
array := []rune(toFind)
for j := 0; j < Lens(s); j++ {
for x := 0; x < Lens(toFind); x++ {
if arrayStr[j] == array[x] {
return x
}
}
}
return -1
}
func Lens(r interface{}) int {
count := 0
switch a := r.(type) {
case []string:
for range a {
count++
}
case string:
for range a {
count++
}
}
return count
}