mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 18:34:42 +01:00
20201118
This commit is contained in:
22
20201118/main.go
Normal file
22
20201118/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println(findMaxLine(65535))
|
||||
}
|
||||
|
||||
func findMaxLine(n int32) int32 {
|
||||
var ret int32
|
||||
for {
|
||||
if findNumber(ret) > n {
|
||||
ret--
|
||||
return ret
|
||||
}
|
||||
ret++
|
||||
}
|
||||
}
|
||||
|
||||
func findNumber(n int32) int32 {
|
||||
return n * (n + 1) / 2
|
||||
}
|
||||
66
20201118/main_test.go
Normal file
66
20201118/main_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_findNumber(t *testing.T) {
|
||||
type args struct {
|
||||
n int32
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int32
|
||||
}{
|
||||
{
|
||||
"1",
|
||||
args{1},
|
||||
1,
|
||||
},
|
||||
{
|
||||
"2",
|
||||
args{2},
|
||||
3,
|
||||
},
|
||||
{
|
||||
"4",
|
||||
args{4},
|
||||
10,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := findNumber(tt.args.n); got != tt.want {
|
||||
t.Errorf("findNumber() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_findMaxLine(t *testing.T) {
|
||||
type args struct {
|
||||
n int32
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int32
|
||||
}{
|
||||
{
|
||||
"5",
|
||||
args{5},
|
||||
2,
|
||||
},
|
||||
{
|
||||
"8",
|
||||
args{8},
|
||||
3,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := findMaxLine(tt.args.n); got != tt.want {
|
||||
t.Errorf("findMaxLine() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user