瀏覽代碼

K7 The Baum-Sweet sequence, and Growth of a population.

Frederic G. MARAND 4 年之前
父節點
當前提交
b7e8450261

+ 11 - 0
go/.idea/runConfigurations/k7_growth_of_a_population.xml

@@ -0,0 +1,11 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="k7 growth of a population" type="GoTestRunConfiguration" factoryName="Go Test">
+    <module name="codewars" />
+    <working_directory value="$PROJECT_DIR$/" />
+    <go_parameters value="-i" />
+    <framework value="gotest" />
+    <kind value="PACKAGE" />
+    <package value="code.osinet.fr/fgm/codewars/kyu7/growth_of_a_population" />
+    <method v="2" />
+  </configuration>
+</component>

+ 11 - 0
go/.idea/runConfigurations/k7_the_Baum_Sweet_sequence.xml

@@ -0,0 +1,11 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="k7 the Baum-Sweet sequence" type="GoTestRunConfiguration" factoryName="Go Test">
+    <module name="codewars" />
+    <working_directory value="$PROJECT_DIR$/" />
+    <go_parameters value="-i" />
+    <framework value="gotest" />
+    <kind value="PACKAGE" />
+    <package value="code.osinet.fr/fgm/codewars/kyu7/the_baum_sweet_sequence" />
+    <method v="2" />
+  </configuration>
+</component>

+ 13 - 0
go/kyu7/growth_of_a_population/growth_of_a_population_suite_test.go

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

+ 28 - 0
go/kyu7/growth_of_a_population/k.go

@@ -0,0 +1,28 @@
+package kata
+
+import "math"
+
+// percent is no predivided by 100: 2% is passed as 2.
+func NextPop(pn int, percent float64, aug int) int {
+	next := int(math.Floor(float64(pn) * (1 + percent/100))) + aug
+	return next
+}
+
+// Cannot use the power formula because of integer truncation at each step.
+func PopN(p0 int, percent float64, aug int, n int) int {
+	pn := p0
+	for i := 0; i < n; i++ {
+		pn = NextPop(pn, percent, aug)
+	}
+	return pn
+}
+
+func NbYear(p0 int, percent float64, aug int, p int) int {
+	pn := p0
+	year := 0
+	// Support population decrease caused by invalid parameters.
+	for year = 0; pn < p && pn >= 0; year++ {
+		pn = NextPop(pn, percent, aug)
+	}
+	return year
+}

+ 39 - 0
go/kyu7/growth_of_a_population/k_test.go

@@ -0,0 +1,39 @@
+package kata_test
+
+import (
+	"testing"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+
+	. "code.osinet.fr/fgm/codewars/kyu7/growth_of_a_population"
+)
+
+var _ = Describe("NbYear", func() {
+	It("fixed tests", func() {
+		Expect(NbYear(1500, 5, 100, 5000)).To(Equal(15))
+		Expect(NbYear(1500000, 2.5, 10000, 2000000)).To(Equal(10))
+		Expect(NbYear(1500000, 0.25, 1000, 2000000)).To(Equal(94))
+		Expect(NbYear(1500000, 0.25, -1000, 2000000)).To(Equal(151))
+	})
+})
+
+func TestPopN(t *testing.T) {
+	checks := []struct {
+		year, p0      int
+		percent       float64
+		aug, expected int
+	}{
+		{1, 1000, 2, 50, 1070},
+		{2, 1000, 2, 50, 1141},
+		{3, 1000, 2, 50, 1213},
+	}
+
+	for _, check := range checks {
+		actual := PopN(check.p0, check.percent, check.aug, check.year)
+		if actual != check.expected {
+			t.Errorf("P0 %d + %0.2f + %d at year %d. Expected %d, got %d",
+				check.p0, check.percent, check.aug, check.year, check.expected, actual)
+		}
+	}
+}

+ 41 - 0
go/kyu7/the_baum_sweet_sequence/k.go

@@ -0,0 +1,41 @@
+package kata
+
+import (
+	"fmt"
+)
+
+type state byte
+
+/*
+The sequence is documented as only being tested for up to 1E6, i.e. 24 bits.
+
+It is assumed to ignore leading zeroes.
+
+A faster solution might use the 2-automaton mechanism described in
+https://www.emis.de//journals/SLC/opapers/s30allouche.pdf
+*/
+func BaumSweet(ch chan<- int) {
+	// Handle n == 0 as binary empty string, par kata discussion.
+	ch <- 1
+N_LOOP:
+	for n := 1; n < 1E4+1; n++ {
+		// Adding external 1s does not changes the result, and ensures the potential
+		// final 0 sequence is measured without a special case.
+		s := []byte(fmt.Sprintf("1%b1", n))
+		// Runes will be '0' and '1' by construction, so always single-byte.
+		segmentLength := 0
+		for i := 0; i < len(s); i++ {
+			if s[i] == '1' {
+				if segmentLength%2 == 1 {
+					ch <- 0
+					continue N_LOOP
+				} else {
+					// Do nothing
+				}
+			} else {
+				segmentLength++
+			}
+		}
+		ch <- 1
+	}
+}

+ 21 - 0
go/kyu7/the_baum_sweet_sequence/k_test.go

@@ -0,0 +1,21 @@
+package kata_test
+
+import (
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+
+	. "code.osinet.fr/fgm/codewars/kyu7/the_baum_sweet_sequence"
+)
+
+var _ = Describe("Baum-Sweet", func() {
+	It("20 elements", func() {
+		arr := make([]int, 20)
+		p := make(chan int, 100)
+		go BaumSweet(p)
+
+		for i := 0; i < 20; i++ {
+			arr[i] = <-p
+		}
+		Expect(arr).To(Equal([]int{1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1}))
+	})
+})

+ 13 - 0
go/kyu7/the_baum_sweet_sequence/the_baum_sweet_sequence_suite_test.go

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