From 80930f574d4af92f65efedb01c269404f07efafc Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Mon, 13 Oct 2025 15:39:31 +0000 Subject: [PATCH] [Sync Iteration] go/card-tricks/1 --- solutions/go/card-tricks/1/card_tricks.go | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 solutions/go/card-tricks/1/card_tricks.go 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..aa2b6e0 --- /dev/null +++ b/solutions/go/card-tricks/1/card_tricks.go @@ -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:]...) +}