From 0967a869ec4df592f335e251b60460ceb5b5e230 Mon Sep 17 00:00:00 2001 From: oabrivard Date: Wed, 4 Oct 2023 11:51:27 +0200 Subject: [PATCH] Solved Weird Algorithm exercise --- go.mod | 10 ++++++++++ go.sum | 9 +++++++++ introductory.go | 21 +++++++++++++++++++++ introductory_test.go | 11 +++++++++++ 4 files changed, 51 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 introductory.go create mode 100644 introductory_test.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..958d83e --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module gitea.paas.celticinfo.fr/oabrivard/cses + +go 1.21.1 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8cf6655 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/introductory.go b/introductory.go new file mode 100644 index 0000000..c6ab52d --- /dev/null +++ b/introductory.go @@ -0,0 +1,21 @@ +package cses + +import "fmt" + +func WeirdAlgo(n int) string { + seq := []int{} + + for n != 1 { + seq = append(seq, n) + + if n%2 == 0 { + n /= 2 + } else { + n = n*3 + 1 + } + } + + seq = append(seq, 1) + + return fmt.Sprint(seq) +} diff --git a/introductory_test.go b/introductory_test.go new file mode 100644 index 0000000..b2c87cf --- /dev/null +++ b/introductory_test.go @@ -0,0 +1,11 @@ +package cses + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestWeirdAlgo(t *testing.T) { + assert.Equal(t, "[3 10 5 16 8 4 2 1]", WeirdAlgo(3)) +}