forked from agiledragon/ddd-sample-in-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0476352
commit ffaebf3
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/counting-shapes/domain/service" | ||
) | ||
|
||
func CountingTriangles(points string, lines []string) int { | ||
return len(service.CountingTriangles(points, lines)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package model | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type Point = byte | ||
type Line = string | ||
type Points = string | ||
|
||
|
||
func belong(points Points, lines []Line) bool { | ||
flag := false | ||
for _, line := range lines { | ||
flag = true | ||
for _, point := range points { | ||
if !strings.ContainsRune(line, point) { | ||
flag = false | ||
break | ||
} | ||
} | ||
if flag { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func Subset(points Points, n int) []Points { | ||
l := len(points) | ||
if l < n { | ||
return nil | ||
} | ||
if l == n { | ||
return []string{points} | ||
} | ||
|
||
results := make([]Points, 0) | ||
if n == 1 { | ||
for i := range points { | ||
results = append(results, Points([]byte{points[i]})) | ||
} | ||
return results | ||
} | ||
|
||
firsts := Subset(points[1:], n - 1) | ||
for _, first := range firsts { | ||
results = append(results, Points([]byte{points[0]}) + first) | ||
} | ||
|
||
lasts := Subset(points[1:], n) | ||
results = append(results, lasts...) | ||
|
||
return results | ||
} | ||
|
||
func hasConnected(lines []Line) func(x, y Point) bool { | ||
return func(x, y Point) bool { | ||
points := make([]Point, 2) | ||
points[0] = x | ||
points[1] = y | ||
return belong(Points(points), lines) | ||
} | ||
} | ||
|
||
func inSameLine(lines []Line) func(x, y, z Point) bool { | ||
return func(x, y, z Point) bool { | ||
points := make([]Point, 3) | ||
points[0] = x | ||
points[1] = y | ||
points[2] = z | ||
return belong(Points(points), lines) | ||
} | ||
} | ||
|
||
func not(flag bool) bool { | ||
return !flag | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package model | ||
|
||
func IsTriangle(points Points, lines []Line) bool { | ||
connected := hasConnected(lines) | ||
in_same_line := inSameLine(lines) | ||
p0 := Point(points[0]) | ||
p1 := Point(points[1]) | ||
p2 := Point(points[2]) | ||
if connected(p0, p1) && | ||
connected(p1, p2) && | ||
connected(p0, p2) && | ||
not(in_same_line(p0, p1, p2)) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/counting-shapes/domain/model" | ||
) | ||
|
||
func CountingTriangles(points model.Points, lines []model.Line) []model.Points { | ||
sets := model.Subset(points, 3) | ||
matches := make([]model.Points, 0) | ||
for _, set := range sets { | ||
if model.IsTriangle(set, lines) { | ||
matches = append(matches, set) | ||
} | ||
} | ||
return matches | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package test | ||
|
||
import ( | ||
. "github.com/smartystreets/goconvey/convey" | ||
"testing" | ||
"github.com/agiledragon/ddd-sample-in-golang/counting-shapes/app/service" | ||
) | ||
|
||
func TestCountingShapes(t *testing.T) { | ||
points := "abcdefghijk" | ||
lines := []string{"abh", "acgi", "adfj", "aek", "bcde", "hgfe", "hijk"} | ||
|
||
Convey("TestCountingShapes", t, func() { | ||
|
||
Convey("counting triangles", func() { | ||
num := service.CountingTriangles(points, lines) | ||
So(num, ShouldEqual, 24) | ||
}) | ||
|
||
}) | ||
} | ||
|