generated from Gophing-Around/hashcode-golang-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
60 lines (44 loc) · 1.09 KB
/
utils.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
package main
import (
"io/ioutil"
"strconv"
"strings"
)
func readFile(source string) string {
in, err := ioutil.ReadFile(source)
if err != nil {
panic(err)
}
return string(in)
}
func toint(num string) int {
res, err := strconv.Atoi(num)
if err != nil {
panic(err)
}
return res
}
func splitNewLines(rawContent string) []string {
return strings.Split(rawContent, "\n")
}
func splitSpaces(lineContent string) []string {
return strings.Split(lineContent, " ")
}
/* useful pieces of code:
***** [1] take input set and split in lines *****
inputLines := strings.Split(inputSet, "\n")
***** [2] take one line and split values with space *****
line := inputLines[0]
values := strings.Split(line, " ")
***** [3] sorting *****
sort.Slice(arrayOfValues, func(i, j int) bool {
valueA := arrayOfValues[i]
valueB := arrayOfValues[j]
return valueA.score > valueB.score
})
***** [4] Join array of STRINGS (not integers or others) *****
strings.Join(arrayOfStrings, " ")
***** [5] Format output string with Sprintf *****
output := ""
output += fmt.Sprintf("%s %d\n", stringValue, intValue)
*/