Problem overview
The function undoLastAdd receives a slice of strings, items, and two words, first and second. It adds first to the end, then adds second to the end, then undoes the last add by removing the last item, and returns the list.
With items = ["milk", "eggs"], first = "bread" and second = "jam", the list grows to ["milk", "eggs", "bread", "jam"] and the undo drops "jam". The result is ["milk" "eggs" "bread"]. With an empty list, "apples" and "pears", the result is ["apples"].
The undo removes the item at the end, by position, not by value. With items = ["water"], first = "juice" and second = "water" the list becomes ["water", "juice", "water"], and the result is ["water" "juice"]: the "water" that goes is the last one, not the first.
Approach
Go slices have no push or pop methods. Two built-in tools do the work:
append(s, x)returns a slice withxadded at the end.- A slice expression
s[a:b]returns the part ofsfrom indexaup to, but not including, indexb.
Removing the last item means keeping everything before the last index: items[:len(items)-1]. Since the cut is made by position, it doesn't matter whether the same word appears earlier.
Step by step
- Add
first. Callappend(items, first)and assign the result back toitems. The assignment is required:appenddoesn't change the variable you pass in, it returns the longer slice. - Add
secondthe same way. Nowsecondis the last item. - Undo. Re-slice from the start up to
len(items)-1, which leaves out the last item, and assign that back toitems. - Return
items. It ends withfirst.
The solution in Go
func undoLastAdd(items []string, first string, second string) []string {
items = append(items, first)
items = append(items, second)
items = items[:len(items)-1]
return items
}
Two details are worth knowing:
- An empty or
nilslice works.appendon an empty slice, or on anilone, simply creates the first element. CallingundoLastAdd(nil, "a", "b")returns["a"]. - Slices share memory. A slice is a window onto an array. If the array behind
itemshas spare room,appendwrites into it instead of copying. With a caller's slice of length 2 and capacity 10, the function returns a slice of length 3, while the caller's own slice still has length 2, but the array behind it now holds[milk eggs bread jam]in its first four positions. When the capacity is exactly the length,appendcopies into a new array, and the caller's slice stays[milk eggs]. The returned list is the same in both cases.
If you want the removed word as well, read it before cutting: last := items[len(items)-1]. This lesson only needs the shorter list.
Edge cases and common mistakes
- Dropping the result of
append. Writingappend(items, first)on its own line is rejected by the compiler because its result is not used, and keeping the olditemsinstead would lose the new word. - Removing by value. Searching for
secondand deleting it removes the wrong item when the word is also earlier in the list, as in the"water"example. - Using
items[:len(items)]. The end index is exclusive, so that keeps everything. Subtract 1. - Negative index. Go has no
items[-1]. The last index islen(items)-1.
Testing
["milk", "eggs"],"bread","jam"gives["milk" "eggs" "bread"].[],"apples","pears"gives["apples"].["water"],"juice","water"gives["water" "juice"].
