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.
63 lines
1.2 KiB
63 lines
1.2 KiB
5 years ago
|
package main
|
||
5 years ago
|
|
||
|
import (
|
||
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
|
)
|
||
|
|
||
4 years ago
|
func convertNbrBase(n int, base string) string {
|
||
|
var result string
|
||
|
length := len(base)
|
||
|
|
||
|
for n >= length {
|
||
|
result = string(base[(n%length)]) + result
|
||
4 years ago
|
n /= length
|
||
4 years ago
|
}
|
||
|
result = string(base[n]) + result
|
||
|
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func convertBase(nbr, baseFrom, baseTo string) string {
|
||
|
resultIntermediary := base.Atoi(nbr, baseFrom)
|
||
|
|
||
|
resultFinal := convertNbrBase(resultIntermediary, baseTo)
|
||
|
|
||
|
return resultFinal
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
type node struct {
|
||
|
nbr string
|
||
|
baseFrom string
|
||
|
baseTo string
|
||
|
}
|
||
|
table := []node{}
|
||
|
|
||
|
for i := 0; i < 30; i++ {
|
||
5 years ago
|
validBaseToInput1 := base.Valid()
|
||
|
validBaseToInput2 := base.Valid()
|
||
5 years ago
|
str := base.ConvertNbr(lib.RandIntBetween(0, 1000000), validBaseToInput1)
|
||
5 years ago
|
val := node{
|
||
|
nbr: str,
|
||
|
baseFrom: validBaseToInput1,
|
||
|
baseTo: validBaseToInput2,
|
||
|
}
|
||
|
table = append(table, val)
|
||
|
}
|
||
|
|
||
|
table = append(table, node{
|
||
|
nbr: "101011",
|
||
|
baseFrom: "01",
|
||
4 years ago
|
baseTo: "0123456789",
|
||
|
})
|
||
5 years ago
|
|
||
|
for _, arg := range table {
|
||
4 years ago
|
lib.Challenge("ConvertBase", student.ConvertBase, convertBase, arg.nbr, arg.baseFrom, arg.baseTo)
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
// TODO: fix base exercises
|