麵包店的 stock 記著每種還剩幾個,sold 是按順序賣出的(一次一個,賣的一定有貨)。寫 SellOut(stock, sold):哪種賣到 0,就從 stock 裡刪掉,名字按賣光的順序放進切片返回。沒有賣光的就返回空切片。
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']func SellOut(stock map[string]int, sold []string) []stringpackage main
func SellOut(stock map[string]int, sold []string) []string {
out := []string{}
for _, item := range sold {
stock[item]--
// 在這裡寫:賣到 0 了,就刪掉它,並把名字加進 out
}
return out
}
寫好了先點「執行」試試示例,沒問題再點「提交」。