mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 18:34:42 +01:00
code kata 230811
This commit is contained in:
@@ -92,6 +92,21 @@ func postOrder2(n *node) {
|
||||
}
|
||||
}
|
||||
|
||||
func levelOrder(n *node) {
|
||||
q := MakeQueue()
|
||||
q.EnQueue(n)
|
||||
for !q.IsEmpty() {
|
||||
p := q.DeQueue().(*node)
|
||||
visit(p)
|
||||
if p.left != nil {
|
||||
q.EnQueue(p.left)
|
||||
}
|
||||
if p.right != nil {
|
||||
q.EnQueue(p.right)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreOrder(t *testing.T) {
|
||||
preOrder(makeBinary())
|
||||
}
|
||||
@@ -116,6 +131,10 @@ func TestPostOrder2(t *testing.T) {
|
||||
postOrder2(makeBinary())
|
||||
}
|
||||
|
||||
func TestLevelOrder(t *testing.T) {
|
||||
levelOrder(makeBinary())
|
||||
}
|
||||
|
||||
func visit(n *node) {
|
||||
if n == nil {
|
||||
return
|
||||
|
||||
28
binary_tree_230809/queue.go
Normal file
28
binary_tree_230809/queue.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
type Queue []interface{}
|
||||
|
||||
func MakeQueue() *Queue {
|
||||
return &Queue{}
|
||||
}
|
||||
|
||||
func (q Queue) Len() int {
|
||||
return len(q)
|
||||
}
|
||||
|
||||
func (q Queue) IsEmpty() bool {
|
||||
return len(q) == 0
|
||||
}
|
||||
|
||||
func (q *Queue) EnQueue(item interface{}) {
|
||||
*q = append(append(*q, item))
|
||||
}
|
||||
|
||||
func (q *Queue) DeQueue() interface{} {
|
||||
if (*q).Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
item := (*q)[0]
|
||||
*q = (*q)[1:len(*q)]
|
||||
return item
|
||||
}
|
||||
168
code_kata_230810/binary_tree_test.go
Normal file
168
code_kata_230810/binary_tree_test.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func preOrder(n *node) {
|
||||
if n == nil {
|
||||
return
|
||||
}
|
||||
visit(n)
|
||||
preOrder(n.left)
|
||||
preOrder(n.right)
|
||||
}
|
||||
|
||||
func preOrder2(n *node) {
|
||||
stack := MakeStack()
|
||||
p := n
|
||||
for p != nil || !stack.IsEmpty() {
|
||||
if p != nil {
|
||||
visit(p)
|
||||
stack.Push(p)
|
||||
p = p.left
|
||||
} else {
|
||||
p = stack.Pop().(*node)
|
||||
p = p.right
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func inOrder(n *node) {
|
||||
if n == nil {
|
||||
return
|
||||
}
|
||||
inOrder(n.left)
|
||||
visit(n)
|
||||
inOrder(n.right)
|
||||
}
|
||||
|
||||
func inOrder2(n *node) {
|
||||
stack := MakeStack()
|
||||
p := n
|
||||
for p != nil || !stack.IsEmpty() {
|
||||
if p != nil {
|
||||
stack.Push(p)
|
||||
p = p.left
|
||||
} else {
|
||||
p = stack.Pop().(*node)
|
||||
visit(p)
|
||||
p = p.right
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func postOrder(n *node) {
|
||||
if n == nil {
|
||||
return
|
||||
}
|
||||
postOrder(n.left)
|
||||
postOrder(n.right)
|
||||
visit(n)
|
||||
}
|
||||
|
||||
func postOrder2(n *node) {
|
||||
stack := MakeStack()
|
||||
p := n
|
||||
var r *node = nil
|
||||
for p != nil || !stack.IsEmpty() {
|
||||
if p != nil {
|
||||
stack.Push(p)
|
||||
p = p.left
|
||||
} else {
|
||||
p = stack.Top().(*node)
|
||||
if p.right != nil && p.right != r {
|
||||
p = p.right
|
||||
stack.Push(p)
|
||||
p = p.left
|
||||
} else {
|
||||
p = stack.Pop().(*node)
|
||||
visit(p)
|
||||
r = p
|
||||
p = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func levelOrder(n *node) {
|
||||
queue := MakeQueue()
|
||||
p := n
|
||||
queue.EnQueue(p)
|
||||
for !queue.IsEmpty() {
|
||||
p = queue.DeQueue().(*node)
|
||||
visit(p)
|
||||
if p.left != nil {
|
||||
queue.EnQueue(p.left)
|
||||
}
|
||||
if p.right != nil {
|
||||
queue.EnQueue(p.right)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreOrder(t *testing.T) {
|
||||
preOrder(makeBinaryTree())
|
||||
}
|
||||
|
||||
func TestPreOrder2(t *testing.T) {
|
||||
preOrder2(makeBinaryTree())
|
||||
}
|
||||
|
||||
func TestInOrder(t *testing.T) {
|
||||
inOrder(makeBinaryTree())
|
||||
}
|
||||
|
||||
func TestInOrder2(t *testing.T) {
|
||||
inOrder2(makeBinaryTree())
|
||||
}
|
||||
|
||||
func TestPostOrder(t *testing.T) {
|
||||
postOrder(makeBinaryTree())
|
||||
}
|
||||
|
||||
func TestPostOrder2(t *testing.T) {
|
||||
postOrder2(makeBinaryTree())
|
||||
}
|
||||
|
||||
func TestLevelOrder(t *testing.T) {
|
||||
levelOrder(makeBinaryTree())
|
||||
}
|
||||
|
||||
type node struct {
|
||||
left *node
|
||||
right *node
|
||||
value string
|
||||
}
|
||||
|
||||
func visit(n *node) {
|
||||
if n == nil {
|
||||
panic("invalid node")
|
||||
}
|
||||
fmt.Printf("Visiting node %s\n", n.value)
|
||||
}
|
||||
|
||||
func makeBinaryTree() *node {
|
||||
return &node{
|
||||
left: &node{
|
||||
left: &node{
|
||||
value: "D",
|
||||
},
|
||||
right: &node{
|
||||
value: "E",
|
||||
},
|
||||
value: "B",
|
||||
},
|
||||
right: &node{
|
||||
left: &node{
|
||||
value: "F",
|
||||
},
|
||||
right: &node{
|
||||
value: "G",
|
||||
},
|
||||
value: "C",
|
||||
},
|
||||
value: "A",
|
||||
}
|
||||
}
|
||||
37
code_kata_230810/int_to_string_test.go
Normal file
37
code_kata_230810/int_to_string_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func IntToString(n int) string {
|
||||
ret := make([]byte, 0)
|
||||
for n > 0 {
|
||||
ret = append([]byte{byte('0' + n%10)}, ret...)
|
||||
n /= 10
|
||||
}
|
||||
//for l, r := 0, len(ret)-1; l < r; l, r = l+1, r-1 {
|
||||
// ret[l], ret[r] = ret[r], ret[l]
|
||||
//}
|
||||
return string(ret)
|
||||
}
|
||||
|
||||
func TestIntToString(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
input int
|
||||
want string
|
||||
}{
|
||||
{"1", 1, "1"},
|
||||
{"11", 11, "11"},
|
||||
{"112", 112, "112"},
|
||||
{"11123456", 11123456, "11123456"},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := IntToString(tc.input)
|
||||
if got != tc.want {
|
||||
t.Errorf("IntToString(%d) = %s, want %s\n", tc.input, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
46
code_kata_230810/prime_factors_test.go
Normal file
46
code_kata_230810/prime_factors_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func PrimeFactorsOf(n int) []int {
|
||||
ret := make([]int, 0)
|
||||
d := 2
|
||||
for d < n {
|
||||
for n%d == 0 {
|
||||
ret = append(ret, d)
|
||||
n /= d
|
||||
}
|
||||
d++
|
||||
}
|
||||
if n > 1 {
|
||||
ret = append(ret, n)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func TestPrimeFactorsOf(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
input int
|
||||
want []int
|
||||
}{
|
||||
{"1", 1, []int{}},
|
||||
{"2", 2, []int{2}},
|
||||
{"4", 4, []int{2, 2}},
|
||||
{"8", 8, []int{2, 2, 2}},
|
||||
{"9", 9, []int{3, 3}},
|
||||
{"a very large number", 2 * 3 * 17 * 37 * 71 * 73, []int{2, 3, 17, 37, 71, 73}},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := PrimeFactorsOf(tc.input)
|
||||
if !reflect.DeepEqual(got, tc.want) {
|
||||
t.Errorf("PrimeFactorsOf(%d) = %v, want %v\n", tc.input, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
28
code_kata_230810/queue.go
Normal file
28
code_kata_230810/queue.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
type Queue []interface{}
|
||||
|
||||
func MakeQueue() *Queue {
|
||||
return &Queue{}
|
||||
}
|
||||
|
||||
func (q Queue) IsEmpty() bool {
|
||||
return len(q) == 0
|
||||
}
|
||||
|
||||
func (q Queue) Len() int {
|
||||
return len(q)
|
||||
}
|
||||
|
||||
func (q *Queue) EnQueue(item interface{}) {
|
||||
*q = append(*q, item)
|
||||
}
|
||||
|
||||
func (q *Queue) DeQueue() interface{} {
|
||||
if q.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
ret := (*q)[0]
|
||||
*q = (*q)[1:]
|
||||
return ret
|
||||
}
|
||||
32
code_kata_230810/reverse_string_test.go
Normal file
32
code_kata_230810/reverse_string_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func ReverseString(s string) string {
|
||||
r := []rune(s)
|
||||
for l, e := 0, len(r)-1; l < e; l, e = l+1, e-1 {
|
||||
r[l], r[e] = r[e], r[l]
|
||||
}
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func TestReverseString(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
}{
|
||||
{"test", "test", "tset"},
|
||||
{"testing", "testing", "gnitset"},
|
||||
{"i am testing", "i am testing", "gnitset ma i"},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := ReverseString(tc.input)
|
||||
if got != tc.want {
|
||||
t.Errorf("ReverseString(%s) = %s, want %s\n", tc.input, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
35
code_kata_230810/stack.go
Normal file
35
code_kata_230810/stack.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
type Stack []interface{}
|
||||
|
||||
func MakeStack() *Stack {
|
||||
return &Stack{}
|
||||
}
|
||||
|
||||
func (s Stack) IsEmpty() bool {
|
||||
return len(s) == 0
|
||||
}
|
||||
|
||||
func (s Stack) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
func (s Stack) Top() interface{} {
|
||||
if s.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
return s[len(s)-1]
|
||||
}
|
||||
|
||||
func (s *Stack) Push(item interface{}) {
|
||||
*s = append(*s, item)
|
||||
}
|
||||
|
||||
func (s *Stack) Pop() interface{} {
|
||||
if s.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
ret := (*s)[len(*s)-1]
|
||||
*s = (*s)[:len(*s)-1]
|
||||
return ret
|
||||
}
|
||||
Reference in New Issue
Block a user