Browse Source

mb:go x$ pwd

/Users/x/x/01/public/tests/go
mb:go x$ goimports -w .
mb:go x$
content-update
Xavier Petit 4 years ago committed by xpetit
parent
commit
59b9bf9b7a
  1. 3
      tests/go/activebits_test.go
  2. 3
      tests/go/any_test.go
  3. 3
      tests/go/appendrange_test.go
  4. 3
      tests/go/btreesearchitem_test.go
  5. 3
      tests/go/btreetransplant_test.go
  6. 3
      tests/go/enigma_test.go
  7. 3
      tests/go/join_test.go
  8. 3
      tests/go/max_test.go
  9. 3
      tests/go/pilot_test.go
  10. 3
      tests/go/printalphabet_test.go
  11. 3
      tests/go/printdigits_test.go
  12. 3
      tests/go/printprogramname_test.go
  13. 3
      tests/go/printreversealphabet_test.go
  14. 3
      tests/go/raid2_test.go
  15. 3
      tests/go/revparams_test.go
  16. 6
      tests/go/solutions/anagram/anagram_test.go
  17. 3
      tests/go/solutions/brainfuck/main.go
  18. 3
      tests/go/solutions/comparetrees.go
  19. 2
      tests/go/solutions/findprevprimeprog/main.go
  20. 24
      tests/go/solutions/inverttree.go
  21. 242
      tests/go/solutions/inverttree/inverttree_test.go
  22. 25
      tests/go/solutions/inverttree/main.go
  23. 6
      tests/go/solutions/itoabaseprog/main.go
  24. 20
      tests/go/solutions/lcm.go
  25. 2
      tests/go/solutions/nenokku/main.go
  26. 10
      tests/go/solutions/nenokku/nenokku_test.go
  27. 3
      tests/go/solutions/onlyz/onlyz_test.go
  28. 1
      tests/go/solutions/piglatin/main.go
  29. 3
      tests/go/solutions/pilot/pilot.go
  30. 3
      tests/go/solutions/printalphabetprog/printalphabetprog_test.go
  31. 3
      tests/go/solutions/printchessboard.go
  32. 3
      tests/go/solutions/printchessboard/main.go
  33. 3
      tests/go/solutions/printchessboard/printchessboard_test.go
  34. 3
      tests/go/solutions/printdigitsprog/printdigitsprog_test.go
  35. 2
      tests/go/solutions/priorprime.go
  36. 8
      tests/go/solutions/priorprime/priorprime_test.go
  37. 3
      tests/go/solutions/repeatalpha/main.go
  38. 3
      tests/go/solutions/robottoorigin/main.go
  39. 3
      tests/go/solutions/robottoorigin/robottoorigin_test.go
  40. 34
      tests/go/solutions/sametree.go
  41. 38
      tests/go/solutions/sametree/main.go
  42. 193
      tests/go/solutions/sametree/sametree_test.go
  43. 3
      tests/go/solutions/searchreplace/main.go
  44. 3
      tests/go/solutions/strrevprog/main.go
  45. 3
      tests/go/solutions/swapbits/swapbits_test.go
  46. 3
      tests/go/solutions/testConvertBase/main.go
  47. 2
      tests/go/solutions/twosum.go
  48. 17
      tests/go/solutions/twosum/main.go
  49. 14
      tests/go/solutions/twosum/twosum_test.go
  50. 3
      tests/go/solutions/uniqueoccurences/main.go
  51. 3
      tests/go/solutions/uniqueoccurences/uniqueoccurences_test.go
  52. 3
      tests/go/solutions/ztail/main.go
  53. 3
      tests/go/sortparams_test.go
  54. 3
      tests/go/swapbits_test.go
  55. 3
      tests/go/ztail_test.go

3
tests/go/activebits_test.go

@ -1,10 +1,11 @@
package student_test
import (
"testing"
solutions "./solutions"
student "./student"
"github.com/01-edu/z01"
"testing"
)
func TestActiveBits(t *testing.T) {

3
tests/go/any_test.go

@ -1,10 +1,11 @@
package student_test
import (
"testing"
solutions "./solutions"
student "./student"
"github.com/01-edu/z01"
"testing"
)
func TestAny(t *testing.T) {

3
tests/go/appendrange_test.go

@ -1,9 +1,10 @@
package student_test
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
solutions "./solutions"
student "./student"
)

3
tests/go/btreesearchitem_test.go

@ -5,9 +5,10 @@ import (
"github.com/01-edu/z01"
"fmt"
solutions "./solutions"
student "./student"
"fmt"
)
func errorMessage_search(t *testing.T, fn interface{}, root, a *solutions.TreeNode, b *student.TreeNode,

3
tests/go/btreetransplant_test.go

@ -5,9 +5,10 @@ import (
"github.com/01-edu/z01"
"fmt"
solutions "./solutions"
student "./student"
"fmt"
)
func parentListTransp(root *student.TreeNode) string {

3
tests/go/enigma_test.go

@ -1,10 +1,11 @@
package student_test
import (
"testing"
solutions "./solutions"
student "./student"
"github.com/01-edu/z01"
"testing"
)
func TestEnigma(t *testing.T) {

3
tests/go/join_test.go

@ -1,10 +1,11 @@
package student_test
import (
"testing"
solutions "./solutions"
student "./student"
"github.com/01-edu/z01"
"testing"
)
func TestJoin(t *testing.T) {

3
tests/go/max_test.go

@ -1,10 +1,11 @@
package student_test
import (
"testing"
solutions "./solutions"
student "./student"
"github.com/01-edu/z01"
"testing"
)
func TestMax(t *testing.T) {

3
tests/go/pilot_test.go

@ -1,8 +1,9 @@
package student_test
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestPilot(t *testing.T) {

3
tests/go/printalphabet_test.go

@ -1,8 +1,9 @@
package student_test
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestPrintAlphabet(t *testing.T) {

3
tests/go/printdigits_test.go

@ -1,8 +1,9 @@
package student_test
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestPrintDigits(t *testing.T) {

3
tests/go/printprogramname_test.go

@ -1,9 +1,10 @@
package student_test
import (
"github.com/01-edu/z01"
"strings"
"testing"
"github.com/01-edu/z01"
)
func TestPrintProgramName(t *testing.T) {

3
tests/go/printreversealphabet_test.go

@ -1,8 +1,9 @@
package student_test
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestPrintReverseAlphabet(t *testing.T) {

3
tests/go/raid2_test.go

@ -1,8 +1,9 @@
package student_test
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestRaid2(t *testing.T) {

3
tests/go/revparams_test.go

@ -1,8 +1,9 @@
package student_test
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestRevParams(t *testing.T) {

6
tests/go/solutions/anagram/anagram_test.go

@ -36,7 +36,7 @@ func TestIsAnagram(t *testing.T) {
)
for i := 0; i < 15; i++ {
value := node {
value := node{
s: z01.RandStr(z01.RandIntBetween(15, 20), "qwertyuiopasdfghjklzxcvbnm "),
t: z01.RandStr(z01.RandIntBetween(15, 20), "qwertyuiopasdfghjklzxcvbnm "),
}
@ -44,7 +44,7 @@ func TestIsAnagram(t *testing.T) {
table = append(table, value)
}
for _, arg := range(table) {
for _, arg := range table {
z01.Challenge(t, IsAnagram, solutions.IsAnagram, arg.s, arg.t)
}
}
}

3
tests/go/solutions/brainfuck/main.go

@ -1,8 +1,9 @@
package main
import (
"github.com/01-edu/z01"
"os"
"github.com/01-edu/z01"
)
const SIZE = 2048

3
tests/go/solutions/comparetrees.go

@ -3,8 +3,9 @@
package solutions
import (
student "../student"
"testing"
student "../student"
)
func CompareTrees(root *solutions.TreeNode, rootS *student.TreeNode, t *testing.T) {

2
tests/go/solutions/findprevprimeprog/main.go

@ -1,6 +1,6 @@
package main
func main(){
func main() {
}

24
tests/go/solutions/inverttree.go

@ -1,23 +1,23 @@
package solutions
type TNode struct {
Val int
Left *TNode
Right *TNode
Val int
Left *TNode
Right *TNode
}
func Invert(root *TNode) {
if root != nil {
temp := root.Left
root.Left = root.Right
root.Right = temp
if root != nil {
temp := root.Left
root.Left = root.Right
root.Right = temp
Invert(root.Left)
Invert(root.Right)
}
Invert(root.Left)
Invert(root.Right)
}
}
func InvertTree(root *TNode) *TNode {
Invert(root)
return root
Invert(root)
return root
}

242
tests/go/solutions/inverttree/inverttree_test.go

@ -4,9 +4,9 @@ import (
"io"
// "os"
"fmt"
"math/rand"
"math/rand"
"strconv"
"testing"
"strconv"
// "github.com/01-edu/z01"
solutions "../../solutions"
@ -16,116 +16,116 @@ type stuNode = TNode
type solNode = solutions.TNode
func solInsert(N *solNode, newVal int) {
if N == nil {
return
} else if newVal <= N.Val {
if N.Left == nil {
N.Left = &solNode{Val: newVal, Left: nil, Right: nil}
} else {
solInsert(N.Left, newVal)
}
} else {
if N.Right == nil {
N.Right = &solNode{Val: newVal, Left: nil, Right: nil}
} else {
solInsert(N.Right, newVal)
}
}
if N == nil {
return
} else if newVal <= N.Val {
if N.Left == nil {
N.Left = &solNode{Val: newVal, Left: nil, Right: nil}
} else {
solInsert(N.Left, newVal)
}
} else {
if N.Right == nil {
N.Right = &solNode{Val: newVal, Left: nil, Right: nil}
} else {
solInsert(N.Right, newVal)
}
}
}
func stuInsert(N *stuNode, newVal int) {
if N == nil {
return
} else if newVal <= N.Val {
if N.Left == nil {
N.Left = &stuNode{Val: newVal, Left: nil, Right: nil}
} else {
stuInsert(N.Left, newVal)
}
} else {
if N.Right == nil {
N.Right = &stuNode{Val: newVal, Left: nil, Right: nil}
} else {
stuInsert(N.Right, newVal)
}
}
if N == nil {
return
} else if newVal <= N.Val {
if N.Left == nil {
N.Left = &stuNode{Val: newVal, Left: nil, Right: nil}
} else {
stuInsert(N.Left, newVal)
}
} else {
if N.Right == nil {
N.Right = &stuNode{Val: newVal, Left: nil, Right: nil}
} else {
stuInsert(N.Right, newVal)
}
}
}
func IsIdentical(root1 *solNode, root2 *stuNode) int {
if root1 == nil && root2 == nil {
return 1
} else if root1 == nil && root2 != nil {
return 0
} else if root2 == nil && root1 != nil {
return 0
} else {
if root1.Val == root2.Val && IsIdentical(root1.Left, root2.Left) == 1 && IsIdentical(root1.Right, root2.Right) == 1 {
return 1
} else {
return 0
}
}
return 1
if root1 == nil && root2 == nil {
return 1
} else if root1 == nil && root2 != nil {
return 0
} else if root2 == nil && root1 != nil {
return 0
} else {
if root1.Val == root2.Val && IsIdentical(root1.Left, root2.Left) == 1 && IsIdentical(root1.Right, root2.Right) == 1 {
return 1
} else {
return 0
}
}
return 1
}
func stuPrint(w io.Writer, node *stuNode, ns int, ch rune) {
if node == nil {
return
}
for i := 0; i < ns; i++ {
fmt.Fprint(w, " ")
}
fmt.Fprintf(w, "%c:%v\n", ch, node.Val)
stuPrint(w, node.Left, ns+2, 'L')
stuPrint(w, node.Right, ns+2, 'R')
if node == nil {
return
}
for i := 0; i < ns; i++ {
fmt.Fprint(w, " ")
}
fmt.Fprintf(w, "%c:%v\n", ch, node.Val)
stuPrint(w, node.Left, ns+2, 'L')
stuPrint(w, node.Right, ns+2, 'R')
}
func solPrint(w io.Writer, node *solNode, ns int, ch rune) {
if node == nil {
return
}
for i := 0; i < ns; i++ {
fmt.Fprint(w, " ")
}
fmt.Fprintf(w, "%c:%v\n", ch, node.Val)
solPrint(w, node.Left, ns+2, 'L')
solPrint(w, node.Right, ns+2, 'R')
if node == nil {
return
}
for i := 0; i < ns; i++ {
fmt.Fprint(w, " ")
}
fmt.Fprintf(w, "%c:%v\n", ch, node.Val)
solPrint(w, node.Left, ns+2, 'L')
solPrint(w, node.Right, ns+2, 'R')
}
func returnStuTree(root *stuNode) string {
if (root == nil) {
return ""
}
ans := strconv.Itoa(root.Val)
if (root.Left == nil && root.Right == nil) {
return ans
}
if (root.Left != nil) {
ans += " " + returnStuTree(root.Left)
}
if (root.Right != nil) {
ans += " " + returnStuTree(root.Right)
}
return ans
if root == nil {
return ""
}
ans := strconv.Itoa(root.Val)
if root.Left == nil && root.Right == nil {
return ans
}
if root.Left != nil {
ans += " " + returnStuTree(root.Left)
}
if root.Right != nil {
ans += " " + returnStuTree(root.Right)
}
return ans
}
func returnSolTree(root *solNode) string{
if (root == nil) {
return ""
}
ans := strconv.Itoa(root.Val)
if (root.Left == nil && root.Right == nil) {
return ans
}
if (root.Left != nil) {
ans += " " + returnSolTree(root.Left)
}
if (root.Right != nil) {
ans += " " + returnSolTree(root.Right)
}
return ans
func returnSolTree(root *solNode) string {
if root == nil {
return ""
}
ans := strconv.Itoa(root.Val)
if root.Left == nil && root.Right == nil {
return ans
}
if root.Left != nil {
ans += " " + returnSolTree(root.Left)
}
if root.Right != nil {
ans += " " + returnSolTree(root.Right)
}
return ans
}
func TestInvertTree(t *testing.T) {
@ -136,32 +136,32 @@ func TestInvertTree(t *testing.T) {
TestTree := &stuNode{Val: root, Left: nil, Right: nil}
for i := 0; i < 15; i++ {
tree = &solNode{Val: root, Left: nil, Right: nil}
temp := tree
temp := tree
val1, val2, val3, val4 = rand.Intn(30), rand.Intn(30), rand.Intn(30), rand.Intn(30)
solInsert(tree, val1)
solInsert(tree, val2)
solInsert(tree, val3)
solInsert(tree, val4)
// solPrint(os.Stdout, tree, 0, 'M')
solutions.InvertTree(tree)
// solPrint(os.Stdout, tree, 0, 'M')
TestTree = &stuNode{Val: root, Left: nil, Right: nil}
tmp := TestTree
stuInsert(TestTree, val1)
stuInsert(TestTree, val2)
stuInsert(TestTree, val3)
stuInsert(TestTree, val4)
// stuPrint(os.Stdout, TestTree, 0, 'M')
InvertTree(TestTree)
// stuPrint(os.Stdout, TestTree, 0, 'M')
ret := IsIdentical(tree, TestTree)
if ret != 1 {
tree1 := returnSolTree(temp)
tree2 := returnStuTree(tmp)
t.Errorf("\n\"%v\" instead of \"%v\"\n\n", tree1, tree2)
// t.Errorf("\nError\n\n")
}
solInsert(tree, val1)
solInsert(tree, val2)
solInsert(tree, val3)
solInsert(tree, val4)
// solPrint(os.Stdout, tree, 0, 'M')
solutions.InvertTree(tree)
// solPrint(os.Stdout, tree, 0, 'M')
TestTree = &stuNode{Val: root, Left: nil, Right: nil}
tmp := TestTree
stuInsert(TestTree, val1)
stuInsert(TestTree, val2)
stuInsert(TestTree, val3)
stuInsert(TestTree, val4)
// stuPrint(os.Stdout, TestTree, 0, 'M')
InvertTree(TestTree)
// stuPrint(os.Stdout, TestTree, 0, 'M')
ret := IsIdentical(tree, TestTree)
if ret != 1 {
tree1 := returnSolTree(temp)
tree2 := returnStuTree(tmp)
t.Errorf("\n\"%v\" instead of \"%v\"\n\n", tree1, tree2)
// t.Errorf("\nError\n\n")
}
}
}

25
tests/go/solutions/inverttree/main.go

@ -1,28 +1,27 @@
package main
type TNode struct {
Val int
Left *TNode
Right *TNode
Val int
Left *TNode
Right *TNode
}
func Invert(root *TNode) {
if root != nil {
temp := root.Left
root.Left = root.Right
root.Right = temp
if root != nil {
temp := root.Left
root.Left = root.Right
root.Right = temp
Invert(root.Left)
Invert(root.Right)
}
Invert(root.Left)
Invert(root.Right)
}
}
func InvertTree(root *TNode) *TNode {
Invert(root)
return root
Invert(root)
return root
}
func main() {
}

6
tests/go/solutions/itoabaseprog/main.go

@ -2,11 +2,13 @@ package main
import (
"fmt"
"github.com/01-edu/z01"
"strconv"
student "../../student"
"github.com/01-edu/z01"
"strings"
student "../../student"
)
func ItoaBase(value, base int) string {

20
tests/go/solutions/lcm.go

@ -1,19 +1,19 @@
package solutions
//This solution is the comparing file of the staff
// Because the solution is a function,
// This solution is the comparing file of the staff
// Because the solution is a function,
//
//1) here the package is solutions
//2) it does not need an empty func main(){}
//3) its location is 1 level below the folder of the nauuo_test.go file
// 1) here the package is solutions
// 2) it does not need an empty func main(){}
// 3) its location is 1 level below the folder of the nauuo_test.go file
func gcd(first, second int) int {
if (second == 0) {
return (first)
if second == 0 {
return first
}
return (gcd(second, first % second))
return gcd(second, first%second)
}
func Lcm(first, second int) int {
return (first / gcd(second, first % second) * second)
}
return first / gcd(second, first%second) * second
}

2
tests/go/solutions/nenokku/main.go

@ -1,8 +1,8 @@
package main
import (
"os"
"fmt"
"os"
"strings"
)

10
tests/go/solutions/nenokku/nenokku_test.go

@ -2,6 +2,7 @@ package main
import (
"testing"
"github.com/01-edu/z01"
)
@ -27,13 +28,13 @@ func TestNenokku(t *testing.T) {
},
)
sets := [][]string {
sets := [][]string{
[]string{"An", "array", "variable", "denotes", "the", "entire", "array"},
[]string{"This", "means", "that", "when", "you", "assign", "or", "pass"},
[]string{"To", "avoid", "the", "copy", "you", "could", "pass"},
[]string{"struct", "but", "with", "indexed", "rather", "than", "named", "fields"},
}
ops := []string {
ops := []string{
"?", "x", "A",
}
@ -44,13 +45,12 @@ func TestNenokku(t *testing.T) {
for j := 0; j < nOps; j++ {
k := z01.RandIntBetween(0, len(ops)-1)
s := z01.RandIntBetween(0, len(sets[index])-1)
result = append(result, ops[k] + " " + sets[index][s])
result = append(result, ops[k]+" "+sets[index][s])
}
table = append(table, node{result})
}
for _, arg := range table {
for _, arg := range table {
z01.ChallengeMainExam(t, arg.operations...)
}
}

3
tests/go/solutions/onlyz/onlyz_test.go

@ -1,8 +1,9 @@
package main
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestOnlyz(t *testing.T) {

1
tests/go/solutions/piglatin/main.go

@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"github.com/01-edu/z01"
)

3
tests/go/solutions/pilot/pilot.go

@ -1,8 +1,9 @@
package main
import (
student ".."
"fmt"
student ".."
)
func main() {

3
tests/go/solutions/printalphabetprog/printalphabetprog_test.go

@ -1,8 +1,9 @@
package main
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestPrintAlphabetProg(t *testing.T) {

3
tests/go/solutions/printchessboard.go

@ -1,9 +1,10 @@
package solutions
import (
"github.com/01-edu/z01"
"os"
"strconv"
"github.com/01-edu/z01"
)
func printLineCh(str string) {

3
tests/go/solutions/printchessboard/main.go

@ -1,9 +1,10 @@
package main
import (
"github.com/01-edu/z01"
"os"
"strconv"
"github.com/01-edu/z01"
)
func printLine(str string) {

3
tests/go/solutions/printchessboard/printchessboard_test.go

@ -1,9 +1,10 @@
package main
import (
"github.com/01-edu/z01"
"strconv"
"testing"
"github.com/01-edu/z01"
)
func TestPrintChessBoard(t *testing.T) {

3
tests/go/solutions/printdigitsprog/printdigitsprog_test.go

@ -1,8 +1,9 @@
package main
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestPrintDigitsProg(t *testing.T) {

2
tests/go/solutions/priorprime.go

@ -22,4 +22,4 @@ func Priorprime(x int) int {
}
}
return ans
}
}

8
tests/go/solutions/priorprime/priorprime_test.go

@ -15,7 +15,7 @@ func TestPriorprime(t *testing.T) {
// (note: a node is not always necessary but in this case it makes the writing of the test easier)
type node struct {
first int
first int
}
// Declaration of an empty array of type node{}
@ -31,8 +31,8 @@ func TestPriorprime(t *testing.T) {
node{13},
node{10},
node{0},
node{1},
node{2},
node{1},
node{2},
)
// If we were to leave the table as it is, a student could just do a program with 4 ifs and get
@ -40,7 +40,7 @@ func TestPriorprime(t *testing.T) {
for i := 0; i < 15; i++ {
value := node{
first: z01.RandIntBetween(0, 1000),
first: z01.RandIntBetween(0, 1000),
//this z01.RandIntBetween function allows the randomization of
//the int for each value in a desired range.
//Note that they are many others of those functions for other types of data

3
tests/go/solutions/repeatalpha/main.go

@ -1,8 +1,9 @@
package main
import (
"github.com/01-edu/z01"
"os"
"github.com/01-edu/z01"
)
func toLowerCase(a rune) rune {

3
tests/go/solutions/robottoorigin/main.go

@ -1,8 +1,9 @@
package main
import (
"github.com/01-edu/z01"
"os"
"github.com/01-edu/z01"
)
func solve(str string) bool {

3
tests/go/solutions/robottoorigin/robottoorigin_test.go

@ -2,6 +2,7 @@ package main
import (
"testing"
"github.com/01-edu/z01"
)
@ -21,4 +22,4 @@ func TestRobotToOrigin(t *testing.T) {
for _, arg := range table {
z01.ChallengeMainExam(t, arg)
}
}
}

34
tests/go/solutions/sametree.go

@ -1,27 +1,27 @@
package solutions
type TreeNodeL struct {
Left *TreeNodeL
Val int
Right *TreeNodeL
Left *TreeNodeL
Val int
Right *TreeNodeL
}
func IsSameTree(p *TreeNodeL, q *TreeNodeL) bool {
if (p == nil && q == nil) {
return true
}
if (checkIfEq(p, q) == true) {
return true
}
return false
if p == nil && q == nil {
return true
}
if checkIfEq(p, q) == true {
return true
}
return false
}
func checkIfEq(t1 *TreeNodeL, t2 *TreeNodeL) bool {
if (t1 == nil && t2 == nil) {
return true
}
if (t1 == nil || t2 == nil) {
return false;
}
return (t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left))
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
return (t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left))
}

38
tests/go/solutions/sametree/main.go

@ -1,31 +1,31 @@
package main
type TreeNodeL struct {
Left *TreeNodeL
Val int
Right *TreeNodeL
Left *TreeNodeL
Val int
Right *TreeNodeL
}
func IsSameTree(p *TreeNodeL, q *TreeNodeL) bool {
if (p == nil && q == nil) {
return true
}
if (checkIfEq(p, q) == true) {
return true
}
return false
if p == nil && q == nil {
return true
}
if checkIfEq(p, q) == true {
return true
}
return false
}
func checkIfEq(t1 *TreeNodeL, t2 *TreeNodeL) bool {
if (t1 == nil && t2 == nil) {
return true
}
if (t1 == nil || t2 == nil) {
return false;
}
return (t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left))
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
return (t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left))
}
func main() {
}
}

193
tests/go/solutions/sametree/sametree_test.go

@ -1,124 +1,123 @@
package main
import (
"testing"
"math/rand"
"strconv"
"github.com/01-edu/z01"
solutions "../../solutions"
"math/rand"
"strconv"
"testing"
solutions "../../solutions"
"github.com/01-edu/z01"
)
type stuTreeNode = TreeNodeL
type solTreeNode = solutions.TreeNodeL
func insertStu(t *stuTreeNode, v int) *stuTreeNode {
if t == nil {
return &stuTreeNode{Left: nil, Val: v, Right: nil}
}
if v < t.Val {
t.Left = insertStu(t.Left, v)
return t
}
t.Right = insertStu(t.Right, v)
return t
if t == nil {
return &stuTreeNode{Left: nil, Val: v, Right: nil}
}
if v < t.Val {
t.Left = insertStu(t.Left, v)
return t
}
t.Right = insertStu(t.Right, v)
return t
}
func insertSol(t *solTreeNode, v int) *solTreeNode {
if t == nil {
return &solTreeNode{Left: nil, Val: v, Right: nil}
}
if v < t.Val {
t.Left = insertSol(t.Left, v)
return t
}
t.Right = insertSol(t.Right, v)
return t
if t == nil {
return &solTreeNode{Left: nil, Val: v, Right: nil}
}
if v < t.Val {
t.Left = insertSol(t.Left, v)
return t
}
t.Right = insertSol(t.Right, v)
return t
}
func New(n, k int) (*solTreeNode, *stuTreeNode, *solTreeNode) {
var cop *solTreeNode
var stu *stuTreeNode
var sol *solTreeNode
for _, v := range rand.Perm(n) {
cop = insertSol(cop, (1+v)*k)
stu = insertStu(stu, (1+v)*k)
sol = insertSol(sol, (1+v)*k)
}
return cop, stu, sol
var cop *solTreeNode
var stu *stuTreeNode
var sol *solTreeNode
for _, v := range rand.Perm(n) {
cop = insertSol(cop, (1+v)*k)
stu = insertStu(stu, (1+v)*k)
sol = insertSol(sol, (1+v)*k)
}
return cop, stu, sol
}
func returnSolTree(root *solTreeNode) string{
if (root == nil) {
return ""
}
ans := strconv.Itoa(root.Val)
if (root.Left == nil && root.Right == nil) {
return ans
}
if (root.Left != nil) {
ans += " " + returnSolTree(root.Left)
}
if (root.Right != nil) {
ans += " " + returnSolTree(root.Right)
}
return ans
func returnSolTree(root *solTreeNode) string {
if root == nil {
return ""
}
ans := strconv.Itoa(root.Val)
if root.Left == nil && root.Right == nil {
return ans
}
if root.Left != nil {
ans += " " + returnSolTree(root.Left)
}
if root.Right != nil {
ans += " " + returnSolTree(root.Right)
}
return ans
}
func returnStuTree(root *stuTreeNode) string {
if (root == nil) {
return ""
}
ans := strconv.Itoa(root.Val)
if (root.Left == nil && root.Right == nil) {
return ans
}
if (root.Left != nil) {
ans += " " + returnStuTree(root.Left)
}
if (root.Right != nil) {
ans += " " + returnStuTree(root.Right)
}
return ans
if root == nil {
return ""
}
ans := strconv.Itoa(root.Val)
if root.Left == nil && root.Right == nil {
return ans
}
if root.Left != nil {
ans += " " + returnStuTree(root.Left)
}
if root.Right != nil {
ans += " " + returnStuTree(root.Right)
}
return ans
}
func compareResults(t *testing.T, stuResult, solResult bool, solTree1, solTree2 *solTreeNode) {
if stuResult != solResult {
tree1 := returnSolTree(solTree1)
tree2 := returnSolTree(solTree2)
t.Errorf("\nIsSameTree(\"%v\", \"%v\") == \"%v\" instead of \"%v\"\n\n", tree1, tree2, stuResult, solResult)
}
if stuResult != solResult {
tree1 := returnSolTree(solTree1)
tree2 := returnSolTree(solTree2)
t.Errorf("\nIsSameTree(\"%v\", \"%v\") == \"%v\" instead of \"%v\"\n\n", tree1, tree2, stuResult, solResult)
}
}
func TestMerge(t *testing.T) {
type node struct {
n int
k int
}
table := []node{}
for i := 0; i < 15; i++ {
value := node{z01.RandIntBetween(10, 15), z01.RandIntBetween(1, 10)}
table = append(table, value)
}
// Check for different trees
for _, arg := range table {
cop1, stuTree1, solTree1 := New(arg.n, arg.k)
cop2, stuTree2, solTree2 := New(arg.n, arg.k)
stuResult := IsSameTree(stuTree1, stuTree2)
solResult := solutions.IsSameTree(solTree1, solTree2)
compareResults(t, stuResult, solResult, cop1, cop2)
}
// Check for same trees
for _, arg := range table {
cop1, stuTree1, solTree1 := New(arg.n, arg.k)
stuResult := IsSameTree(stuTree1, stuTree1)
solResult := solutions.IsSameTree(solTree1, solTree1)
compareResults(t, stuResult, solResult, cop1, cop1)
}
type node struct {
n int
k int
}
table := []node{}
for i := 0; i < 15; i++ {
value := node{z01.RandIntBetween(10, 15), z01.RandIntBetween(1, 10)}
table = append(table, value)
}
// Check for different trees
for _, arg := range table {
cop1, stuTree1, solTree1 := New(arg.n, arg.k)
cop2, stuTree2, solTree2 := New(arg.n, arg.k)
stuResult := IsSameTree(stuTree1, stuTree2)
solResult := solutions.IsSameTree(solTree1, solTree2)
compareResults(t, stuResult, solResult, cop1, cop2)
}
// Check for same trees
for _, arg := range table {
cop1, stuTree1, solTree1 := New(arg.n, arg.k)
stuResult := IsSameTree(stuTree1, stuTree1)
solResult := solutions.IsSameTree(solTree1, solTree1)
compareResults(t, stuResult, solResult, cop1, cop1)
}
}

3
tests/go/solutions/searchreplace/main.go

@ -1,8 +1,9 @@
package main
import (
"github.com/01-edu/z01"
"os"
"github.com/01-edu/z01"
)
func main() {

3
tests/go/solutions/strrevprog/main.go

@ -1,9 +1,10 @@
package main
import (
solutions ".."
"fmt"
"os"
solutions ".."
)
func main() {

3
tests/go/solutions/swapbits/swapbits_test.go

@ -1,10 +1,11 @@
package main
import (
"github.com/01-edu/z01"
"reflect"
"testing"
"github.com/01-edu/z01"
solutions "../../solutions"
)

3
tests/go/solutions/testConvertBase/main.go

@ -1,8 +1,9 @@
package main
import (
solutions "../../solutions"
"fmt"
solutions "../../solutions"
)
func main() {

2
tests/go/solutions/twosum.go

@ -3,7 +3,7 @@ package solutions
func TwoSum(nums []int, target int) []int {
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
if (nums[i] + nums[j] == target) {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}

17
tests/go/solutions/twosum/main.go

@ -1,17 +1,16 @@
package main
func TwoSum(nums []int, target int) []int {
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
if (nums[i] + nums[j] == target) {
return []int{i, j}
}
}
}
return nil
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
}
return nil
}
func main() {
}

14
tests/go/solutions/twosum/twosum_test.go

@ -1,17 +1,17 @@
package main
import (
"math/rand"
"math/rand"
"testing"
"github.com/01-edu/z01"
solutions "../../solutions"
"github.com/01-edu/z01"
)
func TestTwoSum(t *testing.T) {
for i := 0; i < 20; i++ {
token := rand.Perm(20)
target := rand.Intn(30)
z01.Challenge(t, TwoSum, solutions.TwoSum, token, target)
}
for i := 0; i < 20; i++ {
token := rand.Perm(20)
target := rand.Intn(30)
z01.Challenge(t, TwoSum, solutions.TwoSum, token, target)
}
}

3
tests/go/solutions/uniqueoccurences/main.go

@ -1,8 +1,9 @@
package main
import (
"github.com/01-edu/z01"
"os"
"github.com/01-edu/z01"
)
func solve(str string) bool {

3
tests/go/solutions/uniqueoccurences/uniqueoccurences_test.go

@ -2,6 +2,7 @@ package main
import (
"testing"
"github.com/01-edu/z01"
)
@ -23,4 +24,4 @@ func TestUniqueOccurences(t *testing.T) {
for _, arg := range table {
z01.ChallengeMainExam(t, arg)
}
}
}

3
tests/go/solutions/ztail/main.go

@ -2,9 +2,10 @@ package main
import (
"fmt"
"github.com/01-edu/z01"
"os"
"strconv"
"github.com/01-edu/z01"
)
func numberOfBytes(args []string) (int, []string) {

3
tests/go/sortparams_test.go

@ -1,8 +1,9 @@
package student_test
import (
"github.com/01-edu/z01"
"testing"
"github.com/01-edu/z01"
)
func TestSortParams(t *testing.T) {

3
tests/go/swapbits_test.go

@ -1,10 +1,11 @@
package student_test
import (
"github.com/01-edu/z01"
"reflect"
"testing"
"github.com/01-edu/z01"
solutions "./solutions"
student "./student"
)

3
tests/go/ztail_test.go

@ -1,9 +1,10 @@
package student_test
import (
"github.com/01-edu/z01"
"strings"
"testing"
"github.com/01-edu/z01"
)
//Compares only the stdout of each program

Loading…
Cancel
Save