mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.4 KiB
72 lines
1.4 KiB
5 years ago
|
package main
|
||
5 years ago
|
|
||
|
import (
|
||
5 years ago
|
"fmt"
|
||
|
|
||
5 years ago
|
student "student"
|
||
|
|
||
4 years ago
|
"github.com/01-edu/public/go/tests/lib"
|
||
4 years ago
|
"github.com/01-edu/public/go/tests/lib/base"
|
||
5 years ago
|
)
|
||
|
|
||
5 years ago
|
func printNbrBase(n int, b string) {
|
||
|
if base.IsValid(b) {
|
||
|
length := len(b)
|
||
5 years ago
|
sign := 1
|
||
5 years ago
|
rbase := []rune(b)
|
||
5 years ago
|
if n < 0 {
|
||
|
fmt.Print("-")
|
||
|
sign = -1
|
||
|
}
|
||
|
if n < length && n >= 0 {
|
||
|
fmt.Printf("%c", rbase[n])
|
||
|
} else {
|
||
5 years ago
|
printNbrBase(sign*(n/length), b)
|
||
5 years ago
|
fmt.Printf("%c", rbase[sign*(n%length)])
|
||
|
}
|
||
|
} else {
|
||
|
fmt.Print("NV")
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
type node struct {
|
||
|
n int
|
||
|
base string
|
||
|
}
|
||
|
|
||
|
table := []node{}
|
||
|
|
||
|
// 15 random pairs of ints with valid bases
|
||
|
for i := 0; i < 15; i++ {
|
||
5 years ago
|
validBaseToInput := base.Valid()
|
||
5 years ago
|
val := node{
|
||
5 years ago
|
n: lib.RandIntBetween(-1000000, 1000000),
|
||
5 years ago
|
base: validBaseToInput,
|
||
|
}
|
||
|
table = append(table, val)
|
||
|
}
|
||
|
|
||
|
// 15 random pairs of ints with invalid bases
|
||
|
for i := 0; i < 15; i++ {
|
||
5 years ago
|
invalidBaseToInput := base.Invalid()
|
||
5 years ago
|
val := node{
|
||
5 years ago
|
n: lib.RandIntBetween(-1000000, 1000000),
|
||
5 years ago
|
base: invalidBaseToInput,
|
||
|
}
|
||
|
table = append(table, val)
|
||
|
}
|
||
|
|
||
|
table = append(table,
|
||
|
node{n: 125, base: "0123456789"},
|
||
|
node{n: -125, base: "01"},
|
||
|
node{n: 125, base: "0123456789ABCDEF"},
|
||
|
node{n: -125, base: "choumi"},
|
||
|
node{n: 125, base: "-ab"},
|
||
5 years ago
|
node{n: lib.MinInt, base: "0123456789"},
|
||
5 years ago
|
)
|
||
|
for _, arg := range table {
|
||
5 years ago
|
lib.Challenge("PrintNbrBase", student.PrintNbrBase, printNbrBase, arg.n, arg.base)
|
||
5 years ago
|
}
|
||
|
}
|