Kodewick
Start learning →
Mini challenge←→0 / 70

Sold out!

#042Mini challengeMapsSlices6 min

The bakery's stock map has how many of each are left, and sold lists the sales in order (one at a time; there's always one to sell). Write SellOut(stock, sold): when something reaches 0, delete it from stock and add its name to a slice, in the order things sold out. Nothing sold out gives an empty slice.

Examples
SellOut(map[string]int{"bun": 2, "pie": 1}, []string{"pie", "bun"})→['pie']
SellOut(map[string]int{"bun": 1, "tart": 1}, []string{"tart", "bun"})→['tart', 'bun']
The functionfunc SellOut(stock map[string]int, sold []string) []string
main.go
package main

func SellOut(stock map[string]int, sold []string) []string {
	out := []string{}
	for _, item := range sold {
		stock[item]--
		// your code here: if it reached 0, delete it and add its name to out
	}
	return out
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.