Para acampar, a mochila aguenta no máximo limit kg. Percorra items em ordem: se colocar um item ainda fica dentro do limite, coloque; se não, pule e veja o próximo. Escreva Pack(items, limit) para devolver os nomes colocados e o peso total.
Pack([]Item{{"tent", 5}, {"stove", 2}, {"chair", 4}}, 8)→(['tent', 'stove'], 7)Pack([]Item{}, 10)→([], 0)func Pack(items []Item, limit int) ([]string, int)package main
type Item struct {
Name string
Kg int
}
func Pack(items []Item, limit int) ([]string, int) {
packed := []string{}
total := 0
for _, it := range items {
// seu código aqui: se couber no limite, adicione a packed e some a total
}
return packed, total
}
Executar testa os exemplos. Quando parecerem certos, Enviar.