Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/practice/tree-building/.meta/example.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tree
package treebuilding

import (
"fmt"
Expand Down
76 changes: 76 additions & 0 deletions exercises/practice/tree-building/.meta/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"../../../../gen"
"fmt"
"log"
"strings"
"text/template"
)

func main() {
t, err := template.New("").Parse(tmpl)
if err != nil {
log.Fatal(err)
}
j := map[string]any{
"buildTree": &[]testCase{},
}
if err := gen.Gen("tree-building", j, t); err != nil {
log.Fatal(err)
}
}

type Node struct {
ID int `json:"recordId"`
Children []Node `json:"children"`
}

func (n *Node) String() string {
var out strings.Builder
if len(n.Children) == 0 {
return fmt.Sprintf("&Node{ID: %d}", n.ID)
}
out.WriteString(fmt.Sprintf("&Node{\nID: %d,\n", n.ID))
out.WriteString("Children: []*Node{\n")
for _, c := range n.Children {
out.WriteString(c.String())
out.WriteString(",\n")
}
out.WriteString("},\n")
out.WriteString("}")
return out.String()
}

type testCase struct {
Description string `json:"description"`
Input struct {
Records []struct {
RecordID int `json:"recordId"`
ParentID int `json:"parentId"`
} `json:"records"`
} `json:"input"`
Expected struct {
Error string `json:"error"`
Node *Node `json:"node"`
} `json:"expected"`
}

var tmpl = `{{.Header}}

var testCases = []struct {
description string
input []Record
wantErr bool
expected *Node
}{ {{range .J.buildTree}}
{
description: {{printf "%q" .Description}},
input: []Record{ {{range .Input.Records}}
Record{ID: {{.RecordID}}, Parent: {{.ParentID}}},{{end}}
},{{if ne .Expected.Error ""}}
wantErr: true,{{else if eq .Expected.Node nil}}
expected: nil,{{else}}
expected: {{.Expected.Node}},{{end}}
},{{end}}
}`
48 changes: 48 additions & 0 deletions exercises/practice/tree-building/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[761790a3-4c27-461a-b4e9-8bce8ccee5a1
description = "empty list"

[dcc89dc3-eb39-4f26-a3cd-964e607c95ff
description = "single record"

[dcdb80f0-e5da-43e1-8b8d-6f307be89c0e
description = "three records in order"

[2ff5b8f8-d95e-401e-9359-233919488d22
description = "three records in reverse order"

[de798d3b-8905-4446-a114-a0dd2476d945
description = "more than two children"

[13dd9b3c-6137-415f-b6fe-5044c1dfbc50
description = "binary tree"

[5cfd29dc-166b-47da-84ca-1c60b5ae5941
description = "unbalanced tree"

[a05ddb5d-2d11-4948-88d3-b5f18a44ddce
description = "one root node and has parent"

[9ed09df2-8fd6-4e37-aa37-e7753c057a1a
description = "root node has parent"

[8755a2c4-2c6b-4396-b155-b5bf4b6bc280
description = "no root node"

[c6ef8f9a-4045-4949-a1e1-e0ae804e4af4
description = "duplicate node"

[7a7b77a6-3447-4905-b79c-d22bfe43f408
description = "duplicate root"

[c6f51bd7-3608-4390-b446-dfd1bcbf3ddc
description = "non-continuous"

[1f3d1b50-4494-4b22-b88a-68f32f7d321d
description = "cycle directly"

[ac568b50-3f9b-4cb4-b602-e0eb13de4269
description = "cycle indirectly"

[cf954b21-3cef-420c-8e72-d19547505e1f
description = "higher id parent of lower id"

220 changes: 220 additions & 0 deletions exercises/practice/tree-building/cases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package treebuilding

// This is an auto-generated file. Do not change it manually. Run the generator to update the file.
// See https://github.com/exercism/go#synchronizing-tests-and-instructions
// Source: exercism/problem-specifications
// Commit: 884384d [tree-building] Add canonical data where there was none previously (#2643)

var testCases = []struct {
description string
input []Record
wantErr bool
expected *Node
}{
{
description: "empty list",
input: []Record{},
expected: nil,
},
{
description: "single record",
input: []Record{
Record{ID: 0, Parent: 0},
},
expected: &Node{ID: 0},
},
{
description: "three records in order",
input: []Record{
Record{ID: 0, Parent: 0},
Record{ID: 1, Parent: 0},
Record{ID: 2, Parent: 0},
},
expected: &Node{
ID: 0,
Children: []*Node{
&Node{ID: 1},
&Node{ID: 2},
},
},
},
{
description: "three records in reverse order",
input: []Record{
Record{ID: 2, Parent: 0},
Record{ID: 1, Parent: 0},
Record{ID: 0, Parent: 0},
},
expected: &Node{
ID: 0,
Children: []*Node{
&Node{ID: 1},
&Node{ID: 2},
},
},
},
{
description: "more than two children",
input: []Record{
Record{ID: 0, Parent: 0},
Record{ID: 1, Parent: 0},
Record{ID: 2, Parent: 0},
Record{ID: 3, Parent: 0},
},
expected: &Node{
ID: 0,
Children: []*Node{
&Node{ID: 1},
&Node{ID: 2},
&Node{ID: 3},
},
},
},
{
description: "binary tree",
input: []Record{
Record{ID: 5, Parent: 1},
Record{ID: 3, Parent: 2},
Record{ID: 2, Parent: 0},
Record{ID: 4, Parent: 1},
Record{ID: 1, Parent: 0},
Record{ID: 0, Parent: 0},
Record{ID: 6, Parent: 2},
},
expected: &Node{
ID: 0,
Children: []*Node{
&Node{
ID: 1,
Children: []*Node{
&Node{ID: 4},
&Node{ID: 5},
},
},
&Node{
ID: 2,
Children: []*Node{
&Node{ID: 3},
&Node{ID: 6},
},
},
},
},
},
{
description: "unbalanced tree",
input: []Record{
Record{ID: 5, Parent: 2},
Record{ID: 3, Parent: 2},
Record{ID: 2, Parent: 0},
Record{ID: 4, Parent: 1},
Record{ID: 1, Parent: 0},
Record{ID: 0, Parent: 0},
Record{ID: 6, Parent: 2},
},
expected: &Node{
ID: 0,
Children: []*Node{
&Node{
ID: 1,
Children: []*Node{
&Node{ID: 4},
},
},
&Node{
ID: 2,
Children: []*Node{
&Node{ID: 3},
&Node{ID: 5},
&Node{ID: 6},
},
},
},
},
},
{
description: "one root node and has parent",
input: []Record{
Record{ID: 0, Parent: 1},
},
wantErr: true,
},
{
description: "root node has parent",
input: []Record{
Record{ID: 0, Parent: 1},
Record{ID: 1, Parent: 0},
},
wantErr: true,
},
{
description: "no root node",
input: []Record{
Record{ID: 1, Parent: 0},
Record{ID: 2, Parent: 0},
},
wantErr: true,
},
{
description: "duplicate node",
input: []Record{
Record{ID: 0, Parent: 0},
Record{ID: 1, Parent: 0},
Record{ID: 1, Parent: 0},
},
wantErr: true,
},
{
description: "duplicate root",
input: []Record{
Record{ID: 0, Parent: 0},
Record{ID: 0, Parent: 0},
},
wantErr: true,
},
{
description: "non-continuous",
input: []Record{
Record{ID: 2, Parent: 0},
Record{ID: 4, Parent: 2},
Record{ID: 1, Parent: 0},
Record{ID: 0, Parent: 0},
},
wantErr: true,
},
{
description: "cycle directly",
input: []Record{
Record{ID: 5, Parent: 2},
Record{ID: 3, Parent: 2},
Record{ID: 2, Parent: 2},
Record{ID: 4, Parent: 1},
Record{ID: 1, Parent: 0},
Record{ID: 0, Parent: 0},
Record{ID: 6, Parent: 3},
},
wantErr: true,
},
{
description: "cycle indirectly",
input: []Record{
Record{ID: 5, Parent: 2},
Record{ID: 3, Parent: 2},
Record{ID: 2, Parent: 6},
Record{ID: 4, Parent: 1},
Record{ID: 1, Parent: 0},
Record{ID: 0, Parent: 0},
Record{ID: 6, Parent: 3},
},
wantErr: true,
},
{
description: "higher id parent of lower id",
input: []Record{
Record{ID: 0, Parent: 0},
Record{ID: 2, Parent: 0},
Record{ID: 1, Parent: 2},
},
wantErr: true,
},
}
2 changes: 1 addition & 1 deletion exercises/practice/tree-building/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module tree
module treebuilding

go 1.26
2 changes: 1 addition & 1 deletion exercises/practice/tree-building/tree_building.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tree
package treebuilding

type Record struct {
ID int
Expand Down
Loading
Loading