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.
191 lines
3.4 KiB
191 lines
3.4 KiB
5 years ago
|
package correct
|
||
5 years ago
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"runtime"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
|
||
4 years ago
|
"github.com/01-edu/public/go/tests/lib"
|
||
5 years ago
|
)
|
||
|
|
||
|
func FormatTree(root *TreeNode) string {
|
||
|
if root == nil {
|
||
|
return ""
|
||
|
}
|
||
|
res := root.Data + "\n"
|
||
|
res += formatSubTree(root, "")
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func formatSubTree(root *TreeNode, prefix string) string {
|
||
|
if root == nil {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
var res string
|
||
|
|
||
|
hasLeft := root.Left != nil
|
||
|
hasRight := root.Right != nil
|
||
|
|
||
|
if !hasLeft && !hasRight {
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
res += prefix
|
||
|
if hasLeft && hasRight {
|
||
|
res += "├── "
|
||
|
}
|
||
|
|
||
|
if !hasLeft && hasRight {
|
||
|
res += "└── "
|
||
|
}
|
||
|
|
||
|
if hasRight {
|
||
|
printStrand := (hasLeft && hasRight && (root.Right.Right != nil || root.Right.Left != nil))
|
||
|
newPrefix := prefix
|
||
|
if printStrand {
|
||
|
newPrefix += "│ "
|
||
|
} else {
|
||
|
newPrefix += " "
|
||
|
}
|
||
|
res += root.Right.Data + "\n"
|
||
|
res += formatSubTree(root.Right, newPrefix)
|
||
|
}
|
||
|
|
||
|
if hasLeft {
|
||
|
if hasRight {
|
||
|
res += prefix
|
||
|
}
|
||
|
res += "└── " + root.Left.Data + "\n"
|
||
|
res += formatSubTree(root.Left, prefix+" ")
|
||
|
}
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func ParentList(root *TreeNode) string {
|
||
|
if root == nil {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
var parent string
|
||
|
|
||
|
if root.Parent == nil {
|
||
|
parent = "nil"
|
||
|
} else {
|
||
|
parent = root.Parent.Data
|
||
|
}
|
||
|
|
||
|
r := "Node: " + root.Data + " Parent: " + parent + "\n"
|
||
|
r += ParentList(root.Left) + ParentList(root.Right)
|
||
|
return r
|
||
|
}
|
||
|
|
||
5 years ago
|
func ChallengeTree(
|
||
|
name string,
|
||
5 years ago
|
fn1, fn2 interface{},
|
||
|
arg1 *TreeNode, arg2 interface{},
|
||
5 years ago
|
args ...interface{},
|
||
|
) {
|
||
5 years ago
|
args1 := []interface{}{arg1}
|
||
|
args2 := []interface{}{arg2}
|
||
|
|
||
|
if args != nil {
|
||
|
for _, v := range args {
|
||
|
args1 = append(args1, v)
|
||
|
args2 = append(args2, v)
|
||
|
}
|
||
|
}
|
||
4 years ago
|
st1 := lib.Monitor(fn1, args1)
|
||
|
st2 := lib.Monitor(fn2, args2)
|
||
5 years ago
|
|
||
|
if st1.Stdout != st2.Stdout {
|
||
4 years ago
|
lib.Fatalf("%s(\n%s)\n prints %s instead of %s\n",
|
||
5 years ago
|
name,
|
||
5 years ago
|
FormatTree(arg1),
|
||
4 years ago
|
lib.Format(st2.Stdout),
|
||
|
lib.Format(st1.Stdout),
|
||
5 years ago
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func PrintList(n *NodeI) string {
|
||
|
var res string
|
||
|
it := n
|
||
|
for it != nil {
|
||
|
res += strconv.Itoa(it.Data) + "-> "
|
||
|
it = it.Next
|
||
|
}
|
||
|
res += "<nil>"
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func ListToString(n *NodeL) string {
|
||
|
var res string
|
||
|
it := n
|
||
|
for it != nil {
|
||
|
switch it.Data.(type) {
|
||
|
case int:
|
||
|
res += strconv.Itoa(it.Data.(int)) + "-> "
|
||
|
case string:
|
||
|
res += it.Data.(string) + "-> "
|
||
|
}
|
||
|
it = it.Next
|
||
|
}
|
||
|
res += "<nil>"
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func ConvertIntToInterface(t []int) []interface{} {
|
||
4 years ago
|
RandLen := lib.RandIntBetween(0, len(t))
|
||
5 years ago
|
s := make([]interface{}, RandLen)
|
||
|
for j := 0; j < RandLen; j++ {
|
||
4 years ago
|
for i := 0; i < lib.RandIntBetween(1, len(t)); i++ {
|
||
5 years ago
|
s[j] = t[i]
|
||
|
}
|
||
|
}
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func ConvertIntToStringface(t []string) []interface{} {
|
||
4 years ago
|
RandLen := lib.RandIntBetween(0, len(t))
|
||
5 years ago
|
s := make([]interface{}, RandLen)
|
||
|
for j := 0; j < RandLen; j++ {
|
||
4 years ago
|
for i := 0; i < lib.RandIntBetween(1, len(t)); i++ {
|
||
5 years ago
|
s[j] = t[i]
|
||
|
}
|
||
|
}
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
type NodeTest struct {
|
||
|
Data []interface{}
|
||
|
}
|
||
|
|
||
|
func ElementsToTest(table []NodeTest) []NodeTest {
|
||
|
table = append(table,
|
||
|
NodeTest{
|
||
|
Data: []interface{}{},
|
||
|
},
|
||
|
)
|
||
|
for i := 0; i < 3; i++ {
|
||
|
val := NodeTest{
|
||
4 years ago
|
Data: ConvertIntToInterface(lib.MultRandInt()),
|
||
5 years ago
|
}
|
||
|
table = append(table, val)
|
||
|
}
|
||
|
for i := 0; i < 3; i++ {
|
||
|
val := NodeTest{
|
||
4 years ago
|
Data: ConvertIntToStringface(lib.MultRandWords()),
|
||
5 years ago
|
}
|
||
|
table = append(table, val)
|
||
|
}
|
||
|
return table
|
||
|
}
|
||
|
|
||
|
// GetName gets the function name
|
||
|
func GetName(f interface{}) string {
|
||
|
pathFuncUsed := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), "/")
|
||
|
return pathFuncUsed[len(pathFuncUsed)-1]
|
||
|
}
|