mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 19:54:43 +01:00
20210114
This commit is contained in:
37
20210114/main.go
Normal file
37
20210114/main.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func readBinaryWatch(num int) []string {
|
||||||
|
var ret []string
|
||||||
|
for h := 0; h <= 12; h++ {
|
||||||
|
hCount := 0
|
||||||
|
s := fmt.Sprintf("%#b", h)
|
||||||
|
for _, c := range s {
|
||||||
|
if c == '1' {
|
||||||
|
hCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for m := 0; m <= 60; m++ {
|
||||||
|
mCount := 0
|
||||||
|
s := fmt.Sprintf("%#b", m)
|
||||||
|
for _, c := range s {
|
||||||
|
if c == '1' {
|
||||||
|
mCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if hCount+mCount == num {
|
||||||
|
if m > 10 {
|
||||||
|
ret = append(ret, fmt.Sprintf("%d:%d", h, m))
|
||||||
|
} else {
|
||||||
|
ret = append(ret, fmt.Sprintf("%d:0%d", h, m))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
36
20210114/main_test.go
Normal file
36
20210114/main_test.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_readBinaryWatch(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
num int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{
|
||||||
|
1,
|
||||||
|
},
|
||||||
|
[]string{"1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := readBinaryWatch(tt.args.num)
|
||||||
|
sort.Strings(got)
|
||||||
|
sort.Strings(tt.want)
|
||||||
|
if !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("readBinaryWatch() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user