diff --git a/go/tests/tofix/func/correct/base.go b/go/tests/base/base.go similarity index 90% rename from go/tests/tofix/func/correct/base.go rename to go/tests/base/base.go index 74c39a8e3..155ee53b6 100644 --- a/go/tests/tofix/func/correct/base.go +++ b/go/tests/base/base.go @@ -1,9 +1,7 @@ -package correct - import ( "math/rand" - "github.com/01-edu/z01" + "lib" ) func Valid() string { @@ -39,7 +37,7 @@ func Invalid() string { func StringFrom(base string) string { letters := []rune(base) - size := z01.RandIntBetween(1, 10) + size := lib.RandIntBetween(1, 10) runes := make([]rune, size) for i := range runes { runes[i] = letters[rand.Intn(len(letters))] @@ -67,5 +65,3 @@ func Convert(nbr, baseFrom, baseTo string) string { return resultFinal } - -// TODO: fix base exercises diff --git a/go/tests/func/printnbrbase_test/main.go b/go/tests/func/printnbrbase_test/main.go index 54322a0b6..c89bfd8c6 100644 --- a/go/tests/func/printnbrbase_test/main.go +++ b/go/tests/func/printnbrbase_test/main.go @@ -1,13 +1,51 @@ package main import ( + "fmt" + "strings" + student "student" - "./base" - "./correct" - "github.com/01-edu/public/go/lib" + "base" + "lib" ) +var m = map[rune]struct{}{} + +func uniqueChar(s string) bool { + for _, r := range s { + if _, ok := m[r]; ok { + return false + } + m[r] = struct{}{} + } + return true +} + +func validBase(base string) bool { + return len(base) >= 2 && !strings.ContainsAny(base, "+-") && uniqueChar(base) +} + +func printNbrBase(n int, base string) { + if validBase(base) { + length := len(base) + sign := 1 + rbase := []rune(base) + if n < 0 { + fmt.Print("-") + sign = -1 + } + if n < length && n >= 0 { + fmt.Printf("%c", rbase[n]) + } else { + printNbrBase(sign*(n/length), base) + fmt.Printf("%c", rbase[sign*(n%length)]) + } + } else { + fmt.Print("NV") + } +} + func main() { type node struct { n int @@ -45,8 +83,6 @@ func main() { node{n: lib.MinInt, base: "0123456789"}, ) for _, arg := range table { - lib.Challenge("PrintNbrBase", student.PrintNbrBase, correct.PrintNbrBase, arg.n, arg.base) + lib.Challenge("PrintNbrBase", student.PrintNbrBase, printNbrBase, arg.n, arg.base) } } - -// TODO: fix base exercises diff --git a/go/tests/tofix/func/correct/printnbrbase.go b/go/tests/tofix/func/correct/printnbrbase.go deleted file mode 100644 index 26a373d0a..000000000 --- a/go/tests/tofix/func/correct/printnbrbase.go +++ /dev/null @@ -1,23 +0,0 @@ -package correct - -import "fmt" - -func PrintNbrBase(n int, base string) { - if validBase(base) { - length := len(base) - sign := 1 - rbase := []rune(base) - if n < 0 { - fmt.Print("-") - sign = -1 - } - if n < length && n >= 0 { - fmt.Printf("%c", rbase[n]) - } else { - PrintNbrBase(sign*(n/length), base) - fmt.Printf("%c", rbase[sign*(n%length)]) - } - } else { - fmt.Print("NV") - } -}