-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (60 loc) · 1.6 KB
/
main.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
package main
import (
"fmt"
"strconv"
"strings"
"os"
"log"
)
func main() {
args := os.Args[1:]
if (len(args) == 0) {
log.Fatal("Argument error, missing argument number.\nExample: go run main.go 10")
}
count, err := strconv.Atoi(args[0])
if (err != nil) {
log.Fatal("Argument error, argument number should be integer.\nExample: go run main.go 10")
}
sequence := makeSeq(count)
fmt.Println(strings.Join(sequence, "\n"))
}
func makeSeq(count int) []string {
i := 0
addNextElement, result := elementGenerator()
for i != count {
addNextElement()
i = i + 1
}
return result()
}
type computedElem struct {
count int
elem string
}
func elementGenerator() (func(), func() []string) {
resultArray := make([]string, 1)
resultArray[0] = "1"
addNextElement := func() {
var arrayOfElements []computedElem
letter := resultArray[len(resultArray) - 1]
arrayStr := strings.Split(letter, "")
for index, elem := range arrayStr {
if (index != 0 && elem == arrayStr[index - 1]) {
arrayOfElements[len(arrayOfElements) - 1].count++
} else {
arrayOfElements = append(arrayOfElements, computedElem{1, elem})
}
}
interArray := []string{}
for _, computedElem := range arrayOfElements {
countStr := strconv.Itoa(computedElem.count)
strArray := []string{countStr, computedElem.elem}
interArray = append(interArray, strings.Join(strArray, ""))
}
resultArray = append(resultArray, strings.Join(interArray, ""))
}
result := func() []string {
return resultArray
}
return addNextElement, result
}