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
49 changes: 49 additions & 0 deletions solutions/go/card-tricks/1/card_tricks.go
Original file line number Diff line number Diff line change
@@ -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
}