diff --git a/solutions/go/card-tricks/1/card_tricks.go b/solutions/go/card-tricks/1/card_tricks.go new file mode 100644 index 0000000..9e7534c --- /dev/null +++ b/solutions/go/card-tricks/1/card_tricks.go @@ -0,0 +1,49 @@ +package cards + +// FavoriteCards returns a slice with the cards 2, 6 and 9 in that order. +func FavoriteCards() []int { + s:= []int{2,6,9} + return s +} + +// 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 > len(slice) || index < 0 { + return -1 + } else{ + 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 index < 0 || index >= len(slice) { + slice = append(slice, value) + return slice + } else{ + 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 { + if len(values) > 0 { + slice = append(values, slice...) + return slice + } + return slice +} + +// RemoveItem removes an item from a slice by modifying the existing slice. +func RemoveItem(slice []int, index int) []int { + if index < 0 || index >= len(slice){ + return slice + } + pt1 := slice[index + 1:] + pt2 := slice[:index] + slice = append(pt2,pt1...) + return slice +}