Skip to content

Commit

Permalink
counting quadrangle
Browse files Browse the repository at this point in the history
  • Loading branch information
agiledragon committed Sep 5, 2018
1 parent ffaebf3 commit 3791077
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 18 deletions.
4 changes: 4 additions & 0 deletions counting-shapes/app/service/counting_shapes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ import (
func CountingTriangles(points string, lines []string) int {
return len(service.CountingTriangles(points, lines))
}

func CountingQuadrangles(points string, lines []string) int {
return len(service.CountingQuadrangles(points, lines))
}
109 changes: 109 additions & 0 deletions counting-shapes/domain/model/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"strings"
//"fmt"
)

type Point = byte
Expand Down Expand Up @@ -75,4 +76,112 @@ func inSameLine(lines []Line) func(x, y, z Point) bool {

func not(flag bool) bool {
return !flag
}

func ringOrderConnected(lines []Line) func(points ...Point) bool {
connected := hasConnected(lines)

return func(points ...Point) bool {
// fmt.Println("ring:", points)
prev := points[0]
for i := 1; i < len(points); i++ {
next := points[i]
if !connected(prev, next) {
return false
}
prev = next
}
first := points[0]
last := points[len(points) - 1]
if connected(last, first) {
return true
}
return false
}
}

func hasCrossConnected(lines []Line) func(ls, rs Points) bool {
return func(ls, rs Points) bool {
flag := false
lfound := false
rfound := false
ll := ""
rl := ""
for _, line := range lines {
if !lfound {
flag = true
for _, l := range ls {
if !strings.ContainsRune(line, l) {
flag = false
break
}
}
if flag {
ll = line
lfound = true
}
}

if !rfound {
flag = true
for _, r := range rs {
if !strings.ContainsRune(line, r) {
flag = false
break
}
}
if flag {
rl = line
rfound = true
}
}

if lfound && rfound {
break
}

}

//fmt.Println("ll:", ll)
//fmt.Println("rl:", rl)

lstart := 0
lend := 0
for i := range ll {
if ll[i] == ls[0] {
lstart = i
} else if ll[i] == ls[1] {
lend = i
}
}
if lstart > lend {
lstart, lend = lend, lstart
}
//fmt.Println("lstart, lend:", lstart, lend)


rstart := 0
rend := 0
for i := range rl {
if rl[i] == rs[0] {
rstart = i
} else if rl[i] == rs[1] {
rend = i
}
}
if rstart > rend {
rstart, rend = rend, rstart
}
//fmt.Println("rstart, rend:", rstart, rend)
for i := 1; i < lend - lstart; i++ {
for j := 1; j < rend - rstart; j++ {
//fmt.Println("ll[i]:", ll[lstart + i])
//fmt.Println("rl[j]:", rl[rstart + j])
if ll[lstart + i] == rl[rstart + j] {
return true
}
}
}
return false
}
}
50 changes: 50 additions & 0 deletions counting-shapes/domain/model/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package model

import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func TestRingConnected(t *testing.T) {
lines := []string{"abh", "acgi", "adfj", "aek", "bcde", "hgfe", "hijk"}
ring_connected := ringOrderConnected(lines)
Convey("TestRingConnected", t, func() {

Convey("succ", func() {
So(ring_connected([]Point("abc")...), ShouldBeTrue)
So(ring_connected([]Point("bcgh")...), ShouldBeTrue)
So(ring_connected([]Point("cdfg")...), ShouldBeTrue)
So(ring_connected([]Point("abeg")...), ShouldBeTrue)
So(ring_connected([]Point("abhg")...), ShouldBeTrue)
So(ring_connected([]Point("defj")...), ShouldBeTrue)
})

Convey("failed", func() {
So(ring_connected([]Point("acf")...), ShouldBeFalse)
So(ring_connected([]Point("bcg")...), ShouldBeFalse)
So(ring_connected([]Point("defk")...), ShouldBeFalse)
})

})
}

func TestHasCrossConnected(t *testing.T) {
lines := []string{"abh", "acgi", "adfj", "aek", "bcde", "hgfe", "hijk"}
cross_connected := hasCrossConnected(lines)
Convey("TestHasCrossConnected", t, func() {

Convey("succ", func() {
So(cross_connected("ag", "bd"), ShouldBeTrue)
So(cross_connected("af", "ce"), ShouldBeTrue)
So(cross_connected("af", "be"), ShouldBeTrue)
So(cross_connected("ai", "eh"), ShouldBeTrue)
})

Convey("failed", func() {
So(cross_connected("bc", "hg"), ShouldBeFalse)
So(cross_connected("fj", "ek"), ShouldBeFalse)
})

})
}

42 changes: 42 additions & 0 deletions counting-shapes/domain/model/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package model

func IsTriangle(points Points, lines []Line) bool {
ring_order_connected := ringOrderConnected(lines)
in_same_line := inSameLine(lines)

a := points[0]
b := points[1]
c := points[2]

return ring_order_connected(a, b, c) &&
not(in_same_line(a, b, c))
}

func IsQuadrangle(points Points, lines []Line) bool {
ring_order_connected := ringOrderConnected(lines)
cross_connected := hasCrossConnected(lines)
in_same_line := inSameLine(lines)

a := points[0]
b := points[1]
c := points[2]
d := points[3]

ring_order_connected_without_cross := func(w, x, y, z Point) bool {
wx := Points([]Point{w, x})
yz := Points([]Point{y, z})
wz := Points([]Point{w, z})
xy := Points([]Point{x, y})
return ring_order_connected(w, x, y, z) &&
not(cross_connected(wx, yz)) &&
not(cross_connected(wz, xy))

}

return ring_order_connected_without_cross(a, b, c, d) &&
not(in_same_line(a, b, c)) &&
not(in_same_line(a, b, d)) &&
not(in_same_line(a, c, d)) &&
not(in_same_line(b, c, d))
}

17 changes: 0 additions & 17 deletions counting-shapes/domain/model/triangle.go

This file was deleted.

31 changes: 31 additions & 0 deletions counting-shapes/domain/service/counting_shapes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"github.com/agiledragon/ddd-sample-in-golang/counting-shapes/domain/model"
// "fmt"
)

func CountingTriangles(points model.Points, lines []model.Line) []model.Points {
Expand All @@ -12,5 +13,35 @@ func CountingTriangles(points model.Points, lines []model.Line) []model.Points {
matches = append(matches, set)
}
}
//fmt.Println("matches:", matches)
return matches
}

func CountingQuadrangles(points model.Points, lines []model.Line) []model.Points {
sets := model.Subset(points, 4)
matches := make([]model.Points, 0)
for _, set := range sets {
a := set[0]
b := set[1]
c := set[2]
d := set[3]
orderSets := []model.Points{
model.Points([]model.Point{a, b, c, d}),
model.Points([]model.Point{a, b, d, c}),
model.Points([]model.Point{a, c, b, d}),
model.Points([]model.Point{a, c, d, b}),
model.Points([]model.Point{a, d, b, c}),
model.Points([]model.Point{a, d, c, b}),
}

for _, orderSet := range orderSets {
if model.IsQuadrangle(orderSet, lines) {
matches = append(matches, orderSet)
break
}
}

}
//fmt.Println("matches:", matches)
return matches
}
6 changes: 5 additions & 1 deletion counting-shapes/test/counting_shapes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ func TestCountingShapes(t *testing.T) {
num := service.CountingTriangles(points, lines)
So(num, ShouldEqual, 24)
})


Convey("counting quadrangles", func() {
num := service.CountingQuadrangles(points, lines)
So(num, ShouldEqual, 18)
})
})
}

0 comments on commit 3791077

Please sign in to comment.