Browse Source

K6 Help the bookseller

Frederic G. MARAND 4 years ago
parent
commit
5fe76a55d9

+ 24 - 0
go/.idea/runConfigurations/k6_help_the_bookseller.xml

@@ -0,0 +1,24 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="k6 help the bookseller" type="GoTestRunConfiguration" factoryName="Go Test" singleton="false">
+    <module name="codewars" />
+    <working_directory value="$PROJECT_DIR$" />
+    <go_parameters value="-i" />
+    <EXTENSION ID="net.ashald.envfile">
+      <option name="IS_ENABLED" value="false" />
+      <option name="IS_SUBST" value="false" />
+      <option name="IS_PATH_MACRO_SUPPORTED" value="false" />
+      <option name="IS_IGNORE_MISSING_FILES" value="false" />
+      <option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
+      <ENTRIES>
+        <ENTRY IS_ENABLED="true" PARSER="runconfig" />
+      </ENTRIES>
+    </EXTENSION>
+    <framework value="gotest" />
+    <kind value="PACKAGE" />
+    <package value="code.osinet.fr/fgm/codewars/kyu6/help_the_bookseller" />
+    <directory value="$PROJECT_DIR$/kyu6/counting_duplicates" />
+    <filePath value="$PROJECT_DIR$/" />
+    <pattern value="./..." />
+    <method v="2" />
+  </configuration>
+</component>

+ 13 - 0
go/kyu6/help_the_bookseller/help_the_bookseller_suite_test.go

@@ -0,0 +1,13 @@
+package kata_test
+
+import (
+	"testing"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+)
+
+func TestHelpTheBookseller(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "HelpTheBookseller Suite")
+}

+ 42 - 0
go/kyu6/help_the_bookseller/k.go

@@ -0,0 +1,42 @@
+package kata
+
+import (
+	"fmt"
+	"strings"
+)
+
+func initCounts(listCat []string) map[string]int {
+	cats := map[string]int{}
+	for _, s := range listCat {
+		cats[s[:1]] = 0
+	}
+	return cats
+}
+
+func StockList(listArt []string, listCat []string) string {
+	if len(listArt) == 0 || len(listCat) == 0 {
+		return ""
+	}
+	cats := initCounts(listCat)
+
+	var art string
+	var count int
+
+	for _, invent := range listArt {
+		fmt.Sscanf(invent, "%s %d", &art, &count)
+		_, ok := cats[art[:1]]
+		if !ok {
+			continue
+		}
+		cats[art[:1]] += count
+	}
+
+	slCounts := make([]string, len(cats))
+	for i, cat := range listCat {
+		slCounts[i] = fmt.Sprintf("(%s : %d)", cat, cats[cat])
+		i++
+	}
+
+	counts := strings.Join(slCounts, " - ")
+	return counts
+}

+ 27 - 0
go/kyu6/help_the_bookseller/k_test.go

@@ -0,0 +1,27 @@
+package kata_test
+
+import (
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+
+	. "code.osinet.fr/fgm/codewars/kyu6/help_the_bookseller"
+)
+
+func dotest(listArt []string, listCat []string, exp string) {
+	var ans = StockList(listArt, listCat)
+	Expect(ans).To(Equal(exp))
+}
+
+var _ = Describe("Tests StockList", func() {
+
+	It("should handle basic cases", func() {
+		var b = []string{"BBAR 150", "CDXE 515", "BKWR 250", "BTSQ 890", "DRTY 600"}
+		var c = []string{"A", "B", "C", "D"}
+		dotest(b, c, "(A : 0) - (B : 1290) - (C : 515) - (D : 600)")
+
+		b = []string{"ABAR 200", "CDXE 500", "BKWR 250", "BTSQ 890", "DRTY 600"}
+		c = []string{"A", "B"}
+		dotest(b, c, "(A : 200) - (B : 1140)")
+
+	})
+})