Since I wrote it completely for myself, most of the explanations are small and easy processing, but I will update it as needed because it is used as a memorandum.
string
→ int
func atoi(a string) (b int) {
b, err := strconv.Atoi(a)
if err != nil {
panic(err)
}
return
}
string
→ [] string
func aToSlice(a string) (b []string) {
b = strings.Fields(strings.TrimSpace(a))
return
}
string
→ [] int
func aToIntSlice(a string) (b []int) {
c := strings.Fields(strings.TrimSpace(a))
for _, v := range c {
d := atoi(v)
b = append(b, d)
}
return
}
String
→ [] float64
separated by half-width spacesfunc aToFloat64Slice(a string) (b []float64) {
c := strings.Fields(strings.TrimSpace(a))
for _, v := range c {
d, _ := strconv.ParseFloat(v, 64)
b = append(b, d)
}
return
}
[]string
→ []int
func toIntSlice(a []string) (b []int) {
for _, s := range a {
i := atoi(s)
b = append(b, i)
}
return
}
[]int
→ []string
func toStrSlice(a []int) (b []string) {
for _, i := range a {
s := strconv.Itoa(i)
b = append(b, s)
}
return
}
int
func abs(a int) (b int) {
b = int(math.Abs(float64(a)))
return
}
map [int] int
func findMaxValue(m map[int]int) (maxValue int) {
var max int
var maxIndex int
for i, v := range m {
if max <= v {
max = v
maxIndex = i
}
}
maxValue = m[maxIndex]
return
}
map [int] int
Go's associative array (map) is sorted and returned in ascending order because the order is different for each execution when using a range loop.
func findMaxValueKeys(m map[int]int) (maxKeys []int) {
maxValue := findMaxValue(m)
for i, v := range m {
if v == maxValue {
maxKeys = append(maxKeys, i)
}
}
sort.Slice(maxKeys, func (i, j int) bool { return maxKeys[i] < maxKeys[j] })
return
}
Recommended Posts