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 package student_test
import ( import (
"testing"
solutions "./solutions" solutions "./solutions"
student "./student" student "./student"
"github.com/01-edu/z01" "github.com/01-edu/z01"
"testing"
) )
func TestActiveBits(t *testing.T) { func TestActiveBits(t *testing.T) {

3
tests/go/any_test.go

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

3
tests/go/appendrange_test.go

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

3
tests/go/btreesearchitem_test.go

@ -5,9 +5,10 @@ import (
"github.com/01-edu/z01" "github.com/01-edu/z01"
"fmt"
solutions "./solutions" solutions "./solutions"
student "./student" student "./student"
"fmt"
) )
func errorMessage_search(t *testing.T, fn interface{}, root, a *solutions.TreeNode, b *student.TreeNode, 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" "github.com/01-edu/z01"
"fmt"
solutions "./solutions" solutions "./solutions"
student "./student" student "./student"
"fmt"
) )
func parentListTransp(root *student.TreeNode) string { func parentListTransp(root *student.TreeNode) string {

3
tests/go/enigma_test.go

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

3
tests/go/join_test.go

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

3
tests/go/max_test.go

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

3
tests/go/pilot_test.go

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

3
tests/go/printalphabet_test.go

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

3
tests/go/printdigits_test.go

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

3
tests/go/printprogramname_test.go

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

3
tests/go/printreversealphabet_test.go

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

3
tests/go/raid2_test.go

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

3
tests/go/revparams_test.go

@ -1,8 +1,9 @@
package student_test package student_test
import ( import (
"github.com/01-edu/z01"
"testing" "testing"
"github.com/01-edu/z01"
) )
func TestRevParams(t *testing.T) { 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++ { for i := 0; i < 15; i++ {
value := node { value := node{
s: z01.RandStr(z01.RandIntBetween(15, 20), "qwertyuiopasdfghjklzxcvbnm "), s: z01.RandStr(z01.RandIntBetween(15, 20), "qwertyuiopasdfghjklzxcvbnm "),
t: 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) table = append(table, value)
} }
for _, arg := range(table) { for _, arg := range table {
z01.Challenge(t, IsAnagram, solutions.IsAnagram, arg.s, arg.t) z01.Challenge(t, IsAnagram, solutions.IsAnagram, arg.s, arg.t)
} }
} }

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

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

3
tests/go/solutions/comparetrees.go

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

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

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

24
tests/go/solutions/inverttree.go

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

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

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

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

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

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

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

20
tests/go/solutions/lcm.go

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

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

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

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

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

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

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

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

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

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

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

3
tests/go/solutions/printchessboard.go

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

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

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

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

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

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

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

2
tests/go/solutions/priorprime.go

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

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

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

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

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

34
tests/go/solutions/sametree.go

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

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

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

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

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

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

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

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

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

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

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

2
tests/go/solutions/twosum.go

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

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

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

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

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

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

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

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

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

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

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

3
tests/go/sortparams_test.go

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

3
tests/go/swapbits_test.go

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

3
tests/go/ztail_test.go

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

Loading…
Cancel
Save