Created
August 14, 2022 15:23
-
-
Save ObsidianCat/639947ffe9212c74161268248da83951 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package doughnuts_box | |
import ( | |
"fmt" | |
) | |
type doughnutsBox struct { | |
capacity int | |
doughnuts []string | |
} | |
var knownDoughnutTypes = map[string]bool{ | |
"Matcha Tea": true, | |
"Lime & Coconut (ve)": true, | |
"Home Made Raspberry Jam": true, | |
"Cinnamon Scroll (ve)": true, | |
"Sri Lankan Cinnamon Sugar": true, | |
} | |
func newDoughnutsBox(capacity int) *doughnutsBox { | |
return &doughnutsBox{ | |
capacity: capacity, | |
doughnuts: make([]string, 0), | |
} | |
} | |
func (b *doughnutsBox) pack(doughnuts []string) (int, error) { | |
unrecognizedItems := make([]string, 0) | |
var err error | |
if len(doughnuts) > b.capacity { | |
return 0, fmt.Errorf("failed to put %d doughnuts in the box, it's only has %d doughnuts capacity", len(doughnuts), b.capacity) | |
} | |
for _, doughnut := range doughnuts { | |
if _, found := knownDoughnutTypes[doughnut]; found { | |
b.doughnuts = append(b.doughnuts, doughnut) | |
continue | |
} | |
unrecognizedItems = append(unrecognizedItems, doughnut) | |
} | |
if len(unrecognizedItems) > 0 { | |
err = fmt.Errorf("the following items cannot be placed into the box: %v", unrecognizedItems) | |
} | |
return len(b.doughnuts), err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment