From 62b2b729af7dc3f3397c091c23c7126a7a4e993e Mon Sep 17 00:00:00 2001 From: saip7795 Date: Thu, 12 Jan 2023 12:32:49 -0500 Subject: [PATCH] Ruby Solution for detecting squares --- ruby/2013-detect-squares.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ruby/2013-detect-squares.rb diff --git a/ruby/2013-detect-squares.rb b/ruby/2013-detect-squares.rb new file mode 100644 index 000000000..ac390954b --- /dev/null +++ b/ruby/2013-detect-squares.rb @@ -0,0 +1,28 @@ +class DetectSquares + def initialize() + @points_count = Hash.new(0) + @points = [] + end + + + def add(point) + @points_count[point] +=1 + @points.append(point) + end + + def count(point) + result = 0 + px = point[0] + py = point[1] + + @points.each do |p| + next if (((py-p[1]).abs != (px-p[0]).abs) || (p[0]==px) ||(p[1]==py)) + result += @points_count[[p[0],py]] * @points_count[[px,p[1]]] + end + + return result + + end + + +end \ No newline at end of file