-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard_tricks.go
39 lines (33 loc) · 1.05 KB
/
card_tricks.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
package cards
// FavoriteCards returns a slice with the cards 2, 6 and 9 in that order.
func FavoriteCards() []int {
return []int{2,6,9}
}
// GetItem retrieves an item from a slice at given position.
// If the index is out of range, we want it to return -1.
func GetItem(slice []int, index int) int {
if index < 0 || len(slice) <= index {
return -1
}
return slice[index]
}
// SetItem writes an item to a slice at given position overwriting an existing value.
// If the index is out of range the value needs to be appended.
func SetItem(slice []int, index, value int) []int {
if GetItem(slice,index) == -1{
return append(slice,value)
}
slice[index] = value
return slice
}
// PrependItems adds an arbitrary number of values at the front of a slice.
func PrependItems(slice []int, values ...int) []int {
return append(values,slice...)
}
// RemoveItem removes an item from a slice by modifying the existing slice.
func RemoveItem(slice []int, index int) []int {
if GetItem(slice,index) == -1{
return slice
}
return append(slice[:index],slice[index +1:]...)
}