Kodewick
Empieza a aprender →
Mini reto←→0 / 70

La mochila de acampada

#066Mini reto7 min

Para una acampada, la mochila aguanta como mucho limit kg. Recorre items en orden: si al añadir uno sigues dentro del límite, mételo; si no, sáltalo y mira el siguiente. Escribe Pack(items, limit) para devolver los nombres que has metido y el peso total.

Ejemplos
Pack([]Item{{"tent", 5}, {"stove", 2}, {"chair", 4}}, 8)→(['tent', 'stove'], 7)
Pack([]Item{}, 10)→([], 0)
La funciónfunc Pack(items []Item, limit int) ([]string, int)
main.go
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 {
		// tu código aquí: si cabe dentro del límite, añádelo a packed y a total
	}
	return packed, total
}
Ctrl + Enter también ejecuta
Resultado

Ejecutar prueba los ejemplos. Cuando salgan bien, Enviar.