mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-29 13:18:39 +01:00
42: coded triangle numbers
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const BASE = 64
|
||||
|
||||
func main() {
|
||||
b, err := ioutil.ReadFile("p042_words.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
word := readWords(string(b))
|
||||
count := 0
|
||||
for _, w := range word {
|
||||
if isTriangleNumber(worthOf(w)) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
fmt.Println("count:", count)
|
||||
}
|
||||
|
||||
func readWords(s string) []string {
|
||||
if len(s) == 0 {
|
||||
return nil
|
||||
}
|
||||
str := strings.Replace(s, "\"", "", -1)
|
||||
ret := strings.Split(str, ",")
|
||||
return ret
|
||||
}
|
||||
|
||||
func worthOf(word string) int {
|
||||
if len(word) == 0 {
|
||||
return 0
|
||||
}
|
||||
sum := 0
|
||||
for i := 0; i < len(word); i++ {
|
||||
a := word[i : i+1][0]
|
||||
w := uint8(a) - BASE
|
||||
sum += int(w)
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func isTriangleNumber(n int) bool {
|
||||
//tn = ½n(n+1)
|
||||
x := 2 * n
|
||||
res := x
|
||||
//牛顿法求平方根
|
||||
for res*res > x {
|
||||
res = (res + x/res) / 2
|
||||
}
|
||||
for i := res; i > 0; i-- {
|
||||
if i*(i+1) == x {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_readWords(t *testing.T) {
|
||||
type args struct {
|
||||
s string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
"empty_input",
|
||||
args{""},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"one_name",
|
||||
args{"\"VIC\""},
|
||||
[]string{"VIC"},
|
||||
},
|
||||
{
|
||||
"two_names",
|
||||
args{"\"VIC\",\"COCO\""},
|
||||
[]string{"VIC", "COCO"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := readWords(tt.args.s); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("readWords() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_worthOf(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
word string
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"COLIN",
|
||||
"COLIN",
|
||||
53,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := worthOf(tc.word)
|
||||
if got != tc.want {
|
||||
t.Errorf("worthOf() = %d, want %d", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_isTriangleNumber(t *testing.T) {
|
||||
type args struct {
|
||||
n int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
"1",
|
||||
args{1},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"28",
|
||||
args{28},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"29",
|
||||
args{29},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := isTriangleNumber(tt.args.n); got != tt.want {
|
||||
t.Errorf("isTriangleNumber() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user