Skip to content

Commit 62b2b72

Browse files
committed
Ruby Solution for detecting squares
1 parent 6b15c8f commit 62b2b72

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ruby/2013-detect-squares.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class DetectSquares
2+
def initialize()
3+
@points_count = Hash.new(0)
4+
@points = []
5+
end
6+
7+
8+
def add(point)
9+
@points_count[point] +=1
10+
@points.append(point)
11+
end
12+
13+
def count(point)
14+
result = 0
15+
px = point[0]
16+
py = point[1]
17+
18+
@points.each do |p|
19+
next if (((py-p[1]).abs != (px-p[0]).abs) || (p[0]==px) ||(p[1]==py))
20+
result += @points_count[[p[0],py]] * @points_count[[px,p[1]]]
21+
end
22+
23+
return result
24+
25+
end
26+
27+
28+
end

0 commit comments

Comments
 (0)