-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.go
187 lines (158 loc) · 3.83 KB
/
lists.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package sider
import (
"fmt"
"strconv"
)
var lists = make(map[string][]string)
func GetList(options ...string) ([]string, error) {
var listName string
if len(options) == 0 {
return []string{}, fmt.Errorf("not enough arguments")
}
listName = options[0]
if !isList(listName) {
return []string{}, fmt.Errorf("'%v' is not a list", listName)
}
switch {
case len(options) == 2:
if start, err := strconv.Atoi(options[1]); err == nil {
return lists[listName][start:], nil
}
case len(options) == 3:
if start, errStart := strconv.Atoi(options[1]); errStart == nil {
if stop, errStop := strconv.Atoi(options[2]); errStop == nil {
if stop > len(lists[listName]) {
stop = len(lists[listName])
} else {
stop += 1
}
return lists[listName][start:stop], nil
}
}
}
return lists[listName], nil
}
func LPush(key string, value string) (bool, error) {
if len(key) == 0 {
return false, fmt.Errorf("error pushing '%v' at left", key)
}
var slice = []string{value}
lists[key] = append(slice, lists[key]...)
return true, nil
}
func RPush(key string, value string) (bool, error) {
if len(key) == 0 {
return false, fmt.Errorf("error pushing '%v' at right", key)
}
var slice = []string{value}
lists[key] = append(lists[key], slice...)
return true, nil
}
func Pop(listName string, direction string) (string, error) {
var list []string
var popped string
var errors []string
if len(listName) == 0 {
errors = append(errors, "listName")
}
if direction != "left" && direction != "right" {
errors = append(errors, "direction")
}
if notEnoughArguments(errors) {
return "", fmt.Errorf("not enough arguments")
}
list, err := GetList(listName)
if err != nil {
return "", err
}
if len(list) == 0 {
return "", fmt.Errorf("list '%s' is empty", listName)
}
switch {
case direction == "right":
popped = list[len(list)-1]
lists[listName] = append([]string{}, list[:len(list)-1]...)
case direction == "left":
popped = list[0]
lists[listName] = append([]string{}, list[1:]...)
}
return popped, nil
}
func LLen(key string) (int, error) {
if !isList(key) {
return 0, fmt.Errorf("'%s' is not a list", key)
}
return len(lists[key]), nil
}
func IndexOf(listName, element string) (int, error) {
if isList(listName) {
for index, value := range lists[listName] {
if value == element {
return index, nil
}
}
}
return 0, fmt.Errorf("error getting index of '%v'", element)
}
func ReplaceList(listName string, index int, newElement string) (bool, error) {
var errors []string
switch {
case len(listName) == 0:
errors = append(errors, "listName")
case len(newElement) == 0:
errors = append(errors, "newElement")
}
if len(errors) > 0 {
notEnoughArguments(errors)
return false, fmt.Errorf("not enough arguments")
}
if list, err := GetList(listName); err != nil {
return false, fmt.Errorf("error trying to read list: %v", err)
} else {
if len(list) > index {
lists[listName][index] = newElement
}
}
return true, nil
}
func CountList(listName string) (int, error) {
list, err := GetList(listName)
return len(list), err
}
func DeleteList(listName string) error {
for key, _ := range lists {
if key == listName {
delete(lists, listName)
return nil
}
}
return fmt.Errorf("list '%s' doesn't exist", listName)
}
func DeleteItemByContent(listName string, item string) bool {
var list, err = GetList(listName)
if err != nil {
return false
}
var newList []string
for _, value := range list {
if value != item {
newList = append(newList, value)
}
}
lists[listName] = newList
return true
}
func DeleteItemByIndex(listName string, index int) bool {
var list, err = GetList(listName)
if err != nil {
return false
}
var newList []string
for i, value := range list {
if i != index {
newList = append(newList, value)
}
}
lists[listName] = newList
return true
}