Skip to content

Commit

Permalink
counting triangles
Browse files Browse the repository at this point in the history
  • Loading branch information
agiledragon committed Sep 3, 2018
1 parent 0476352 commit ffaebf3
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
9 changes: 9 additions & 0 deletions counting-shapes/app/service/counting_shapes.go
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))
}
78 changes: 78 additions & 0 deletions counting-shapes/domain/model/set.go
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
}
17 changes: 17 additions & 0 deletions counting-shapes/domain/model/triangle.go
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
}

16 changes: 16 additions & 0 deletions counting-shapes/domain/service/counting_shapes.go
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
}
22 changes: 22 additions & 0 deletions counting-shapes/test/counting_shapes_test.go
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)
})

})
}

0 comments on commit ffaebf3

Please sign in to comment.