Browse Source

Fixing base exercises

pull/533/head
5 years ago committed by xpetit
parent
commit
d54a1679bf
  1. 8
      go/tests/base/base.go
  2. 48
      go/tests/func/printnbrbase_test/main.go
  3. 23
      go/tests/tofix/func/correct/printnbrbase.go

8
go/tests/tofix/func/correct/base.go → 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

48
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

23
go/tests/tofix/func/correct/printnbrbase.go

@ -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")
}
}
Loading…
Cancel
Save