Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions solutions/go/card-tricks/1/card_tricks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cards

// FavoriteCards returns a slice with the cards 2, 6 and 9 in that order.
func FavoriteCards() []int {
favCard := []int{2, 6, 9}
return favCard;
}

// 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 || index >= len(slice) {
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 {
return append(values, 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
}

return append(slice[:index], slice[index + 1:]...)
}