-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path5.go
80 lines (62 loc) · 1.6 KB
/
5.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
package main
import (
"fmt"
"bufio"
"os"
"strings"
"regexp"
)
func main() {
in,out := getBuffers()
defer out.Flush()
fmt.Fprint(out, "[")
var setsCount int
fmt.Fscanln(in, &setsCount)
for set := 1; set <= setsCount; set++ { // перебираем наборы данных
output := inputTrimmedJson(in)
for{
oldlen := len(output)
output = pretty(output)
if(oldlen == len(output)){
break
}
}
fmt.Fprint(out, output)
if (set!=setsCount){
fmt.Fprint(out, ",")
}else{
fmt.Fprintln(out, "]")
}
}
}
func inputTrimmedJson(in *bufio.Reader)string{
output := ""
var strCount int
fmt.Fscanln(in, &strCount)
for j := 1; j <= strCount; j++ {
var str string
str, _ = in.ReadString('\n')
str = strings.ReplaceAll(str, " ", "")
str = strings.ReplaceAll(str, "\t", "")
output += strings.TrimSpace( str )
}
return output
}
func pretty(inp string)string {
var out string
out = inp
re1 := regexp.MustCompile(`[^,\[\]\{\}]*\[\],*`)
re2 := regexp.MustCompile(`[^,\[\]\{\}]*\{\},*`)
out = re1.ReplaceAllString(out, "")
out = re2.ReplaceAllString(out, "")
out = strings.ReplaceAll(out, ",}", "}")
out = strings.ReplaceAll(out, ",]", "]")
return out
}
func getBuffers()(*bufio.Reader,*bufio.Writer){
var in *bufio.Reader
var out *bufio.Writer
in = bufio.NewReader(os.Stdin)
out = bufio.NewWriter(os.Stdout)
return in,out
}