Kodewick
Start learning →
Mini challenge←→0 / 70

Camp cabins

#068Mini challengeStructsMaps6 min

Summer camp puts every camper in a cabin; each one in campers has a Name and a Cabin. Write ByCabin(campers) to return a map from each cabin to the names staying in it (in their original order). Nobody gives an empty map.

Examples
ByCabin([]Camper{{"Li", "Oak"}, {"Bo", "Pine"}, {"Mia", "Oak"}})→{'Oak': ['Li', 'Mia'], 'Pine': ['Bo']}
ByCabin([]Camper{})→{}
The functionfunc ByCabin(campers []Camper) map[string][]string
main.go
package main

type Camper struct {
	Name  string
	Cabin string
}

func ByCabin(campers []Camper) map[string][]string {
	cabins := map[string][]string{}
	for _, c := range campers {
		// your code here: add c.Name to the end of c.Cabin's list
	}
	return cabins
}
Ctrl + Enter runs too
Result

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