-
Notifications
You must be signed in to change notification settings - Fork 0
/
chemistry_elements.go
176 lines (149 loc) · 2.81 KB
/
chemistry_elements.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"strconv"
)
type CharType string
const (
CharTypeCap CharType = "cap"
CharTypeSmall CharType = "small"
CharTypeNum CharType = "number"
CharTypeOpenGroup CharType = "open_group"
CharTypeCloseGroup CharType = "close_group"
)
func (ct CharType) IsLastCharNum() bool {
return ct == CharTypeNum
}
type res map[string]int
func (r res) add(elm, num string) {
var n int = 1
var err error
if num != "" {
n, err = strconv.Atoi(num)
if err != nil {
fmt.Println(err)
return
}
}
pn, ok := r[elm]
if !ok {
r[elm] = n
return
}
r[elm] = pn + n
}
func (r res) update(elm, num string) {
pn, ok := r[elm]
if !ok {
fmt.Errorf("invalid element to update")
return
}
num = fmt.Sprintf("%d%s", pn, num)
n, err := strconv.Atoi(num)
if err != nil {
fmt.Println(err)
return
}
r[elm] = n
}
func main() {
// H2(SO)4
// H13O11
// H2SO4
// H2O
// HCl
input := "H2(SO)4"
// form the element untill we reach either 1 number or cap letter
response := make(res)
// main the state of what pre element and preChar Type
element := ""
lastElem := ""
var lastCharNum CharType
isGroup := false
groupElements := make([]string, 0)
for _, r := range input {
// fmt.Println(r, string(r))
// group start
if r == 40 {
isGroup = true
continue
}
if r == 41 && element != "" {
groupElements = append(groupElements, element)
element = ""
continue
}
if isCap(r) {
lastCharNum = CharTypeCap
if element == "" {
// append
element = string(r)
continue
}
if isGroup {
groupElements = append(groupElements, element)
} else {
// add the pre element to result map
response.add(element, "")
lastElem = element
}
// now track this new element
element = string(r)
continue
}
if isSmall(r) {
lastCharNum = CharTypeSmall
// append
element += string(r)
continue
}
if isNumber(r) {
// add this to result map
// fmt.Println("calling to add ", element, string(r))
if isGroup {
for _, elm := range groupElements {
response.add(elm, string(r))
}
isGroup = false
lastCharNum = CharTypeNum
continue
}
if lastCharNum.IsLastCharNum() {
// update the elem freq
response.update(lastElem, string(r))
} else {
response.add(element, string(r))
lastElem = element
element = ""
}
lastCharNum = CharTypeNum
continue
}
}
if element != "" {
response.add(element, "")
element = ""
}
fmt.Println("Response: ", response)
}
// CAP letters 65 - 90
// Small case 97 - 122
// numbers 48 - 57
func isCap(r rune) bool {
if r >= 65 && r <= 90 {
return true
}
return false
}
func isSmall(r rune) bool {
if r >= 97 && r <= 122 {
return true
}
return false
}
func isNumber(r rune) bool {
if r >= 48 && r <= 57 {
return true
}
return false
}