mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 21:24:46 +01:00
Code kata restarting
This commit is contained in:
48
prime_factors_230801/main_test.go
Normal file
48
prime_factors_230801/main_test.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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}},
|
||||||
|
{"3", 3, []int{3}},
|
||||||
|
{"4", 4, []int{2, 2}},
|
||||||
|
{"6", 6, []int{2, 3}},
|
||||||
|
{"8", 8, []int{2, 2, 2}},
|
||||||
|
{"9", 9, []int{3, 3}},
|
||||||
|
{"a large number", 2 * 2 * 3 * 5 * 7 * 11 * 13 * 71, []int{2, 2, 3, 5, 7, 11, 13, 71}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := PrimeFactorsOf(tc.input)
|
||||||
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
|
t.Errorf("PrimeFactors(%d) = %v, want %v", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
47
prime_factors_230802/main_test.go
Normal file
47
prime_factors_230802/main_test.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FactorsOfNumber(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 TestFactorsOfNumber(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
input int
|
||||||
|
want []int
|
||||||
|
}{
|
||||||
|
{"1", 1, []int{}},
|
||||||
|
{"2", 2, []int{2}},
|
||||||
|
{"3", 3, []int{3}},
|
||||||
|
{"4", 4, []int{2, 2}},
|
||||||
|
{"6", 6, []int{2, 3}},
|
||||||
|
{"8", 8, []int{2, 2, 2}},
|
||||||
|
{"9", 9, []int{3, 3}},
|
||||||
|
{"a very large number", 2 * 3 * 5 * 7 * 11 * 13 * 31 * 71, []int{2, 3, 5, 7, 11, 13, 31, 71}},
|
||||||
|
}
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := FactorsOfNumber(tc.input)
|
||||||
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
|
t.Errorf("FactorsOfNumber(%d) = %v, want %v", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
48
prime_factors_230803/main_test.go
Normal file
48
prime_factors_230803/main_test.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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}},
|
||||||
|
{"3", 3, []int{3}},
|
||||||
|
{"4", 4, []int{2, 2}},
|
||||||
|
{"6", 6, []int{2, 3}},
|
||||||
|
{"8", 8, []int{2, 2, 2}},
|
||||||
|
{"9", 9, []int{3, 3}},
|
||||||
|
{"a very large number", 2 * 3 * 5 * 7 * 11 * 13 * 31 * 71, []int{2, 3, 5, 7, 11, 13, 31, 71}},
|
||||||
|
}
|
||||||
|
|
||||||
|
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", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
45
prime_factors_230804/main_test.go
Normal file
45
prime_factors_230804/main_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
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}},
|
||||||
|
{"6", 6, []int{2, 3}},
|
||||||
|
{"8", 8, []int{2, 2, 2}},
|
||||||
|
{"9", 9, []int{3, 3}},
|
||||||
|
{"a very large number", 2 * 3 * 5 * 11 * 13 * 17 * 71, []int{2, 3, 5, 11, 13, 17, 71}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
got := PrimeFactorsOf(tc.input)
|
||||||
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
|
t.Errorf("PrimeFactors(%d) = %v, want %v", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
32
reverseString/main_test.go
Normal file
32
reverseString/main_test.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func ReverseString(s string) string {
|
||||||
|
tm := []byte(s)
|
||||||
|
l := len(s)
|
||||||
|
for i := 0; i < l/2; i++ {
|
||||||
|
tm[i], tm[l-1-i] = tm[l-1-i], tm[i]
|
||||||
|
}
|
||||||
|
return string(tm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReverseString(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"1", "test", "tset"},
|
||||||
|
{"2", "testing", "gnitset"},
|
||||||
|
{"3", "this is a testing string", "gnirts gnitset a si siht"},
|
||||||
|
}
|
||||||
|
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", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
30
reverse_string+230804/main_test.go
Normal file
30
reverse_string+230804/main_test.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func ReverseString(s string) string {
|
||||||
|
tmp := []byte(s)
|
||||||
|
l := len(tmp)
|
||||||
|
for i := 0; i < l/2; i++ {
|
||||||
|
tmp[i], tmp[l-i-1] = tmp[l-i-1], tmp[i]
|
||||||
|
}
|
||||||
|
return string(tmp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReverseString(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"test", "test", "tset"},
|
||||||
|
{"testing", "testing", "gnitset"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
got := ReverseString(tc.input)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("ReverseString(%s) = %s, want %s", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
32
reverse_string_230803/main_test.go
Normal file
32
reverse_string_230803/main_test.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func ReverseString(s string) string {
|
||||||
|
tm := []byte(s)
|
||||||
|
l := len(s)
|
||||||
|
for i := 0; i < l/2; i++ {
|
||||||
|
tm[i], tm[l-1-i] = tm[l-1-i], tm[i]
|
||||||
|
}
|
||||||
|
return string(tm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReverseString(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"1", "test", "tset"},
|
||||||
|
{"2", "testing", "gnitset"},
|
||||||
|
}
|
||||||
|
|
||||||
|
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", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user