-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformat-the-string.go
75 lines (64 loc) · 1.52 KB
/
reformat-the-string.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
)
func isNumber(r byte) bool {
return r >= 48 && r <= 57
}
func reformat(s string) string {
if s == "" {
return ""
}
bs := []byte(s)
for i := 1; i <= len(bs)-1; i++ {
// fmt.Printf("%v\n", string(bs))
if isNumber(bs[i-1]) == isNumber(bs[i]) {
gotReplacement := false
for r := i + 1; r < len(bs); r++ {
if isNumber(bs[i]) != isNumber(bs[r]) {
bs[i], bs[r] = bs[r], bs[i]
// fmt.Printf("%v %v | %v\n", string(bs[r]), string(bs[i]), string(bs))
gotReplacement = true
break
}
}
if !gotReplacement {
if i == len(bs)-1 && (isNumber(bs[0]) != isNumber(bs[len(bs)-1])) {
return string(append(bs[len(bs)-1:], bs[:len(bs)-1]...))
}
return ""
}
}
}
return string(bs)
}
func main() {
test := ""
fmt.Println("Testing:", test)
result := reformat(test)
fmt.Println("Yields :", result, "\n")
test = "a"
fmt.Println("Testing:", test)
result = reformat(test)
fmt.Println("Yields :", result, "\n")
test = "44"
fmt.Println("Testing:", test)
result = reformat(test)
fmt.Println("Yields :", result, "\n")
test = "bba"
fmt.Println("Testing:", test)
result = reformat(test)
fmt.Println("Yields :", result, "\n")
test = "397d"
fmt.Println("Testing:", test)
result = reformat(test)
fmt.Println("Yields :", result, "\n")
test = "1qw3e"
fmt.Println("Testing:", test)
result = reformat(test)
fmt.Println("Yields :", result, "\n")
test = "q2w3be4r1"
fmt.Println("Testing:", test)
result = reformat(test)
fmt.Println("Yields :", result, "\n")
}