-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask.go
98 lines (79 loc) · 1.53 KB
/
task.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
var n int
input, err := getInputData()
if err != nil {
showError(err)
}
input.Scan()
n, err = strconv.Atoi(input.Text())
if err != nil {
showError(err)
}
input.Scan()
arrStr := strings.Fields(input.Text())
if res, err := solution(n, arrStr); err != nil {
showError(err)
} else {
fmt.Println(strings.Join(res, " "))
}
}
func solution(n int, arr []string) ([]string, error) {
if n == 0 {
return nil, nil
}
counter := [3]int{}
for i := 0; i < len(arr); i++ {
if arr[i] == "" {
continue
}
vInt, err := strconv.Atoi(arr[i])
if err != nil {
return nil, err
}
counter[vInt] += 1
}
result := make([]string, n)
i := 0
for g, v := range counter {
for v > 0 {
result[i] = strconv.Itoa(g)
v--
i++
}
}
return result, nil
}
// getInputData подготовка входных данных
func getInputData() (scan *bufio.Scanner, err error) {
var input *os.File
const maxCapacity = 1024 * 1024 * 10
input, err = getInputFromFile()
if err != nil {
showError(err)
}
scanner := bufio.NewScanner(input)
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)
scanner.Split(bufio.ScanLines)
return scanner, nil
}
// getInputFromFile получение ввода из файла
func getInputFromFile() (*os.File, error) {
file, err := os.Open("input.txt")
if err != nil {
return nil, err
}
return file, nil
}
// showError вывод ошибки
func showError(err interface{}) {
panic(err)
}