diff --git a/prime_factors_230806/main_test.go b/prime_factors_230806/main_test.go new file mode 100644 index 0000000..6db5239 --- /dev/null +++ b/prime_factors_230806/main_test.go @@ -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 * 5 * 71 * 73, []int{2, 3, 5, 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", tc.input, got, tc.want) + } + }) + } +} diff --git a/reverse_string_230806/main_test.go b/reverse_string_230806/main_test.go new file mode 100644 index 0000000..bd30316 --- /dev/null +++ b/reverse_string_230806/main_test.go @@ -0,0 +1,32 @@ +package main + +import "testing" + +func ReverseString(s string) string { + tmp := []rune(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"}, + {"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", tc.input, got, tc.want) + } + }) + } +}