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..c57cbaf --- /dev/null +++ b/solutions/go/card-tricks/1/card_tricks.go @@ -0,0 +1,55 @@ +package cards + +// FavoriteCards returns a slice with the cards 2, 6 and 9 in that order. +func FavoriteCards() []int { + cards:= []int{2,6,9} + return cards + panic("Please implement the FavoriteCards function") +} + +// 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)-1 { + return -1 + } + return slice[index] + panic("Please implement the GetItem function") +} + +// 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)-1 { + slice := append(slice, value) + return slice + } + + slice[index] = value + return slice + panic("Please implement the SetItem function") +} + + +// PrependItems adds an arbitrary number of values at the front of a slice. +func PrependItems(slice []int, values ...int) []int { + if len(values) == 0{ + return slice + } + sliced := append(values, slice...) + return sliced + panic("Please implement the PrependItems function") +} + +// 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)-1 { + return slice + } + sliced := append(slice[:index], slice[index+1:]...) + return sliced + panic("Please implement the RemoveItem function") +}