Skip to content

Latest commit

 

History

History
63 lines (49 loc) · 1.57 KB

quadrants.md

File metadata and controls

63 lines (49 loc) · 1.57 KB

Description

Task

Given a point in a Euclidean plane (x and y), return the quadrant the point exists in: 1, 2, 3 or 4 (integer). x and y are non-zero integers, therefore the given point never lies on the axes.

Examples

(1, 2)     => 1
(3, 5)     => 1
(-10, 100) => 2
(-1, -9)   => 3
(19, -56)  => 4

Reference

Quadrants

There are four quadrants:

  1. First quadrant, the quadrant in the top-right, contains all points with positive X and Y
  2. Second quadrant, the quadrant in the top-left, contains all points with negative X, but positive Y
  3. Third quadrant, the quadrant in the bottom-left, contains all points with negative X and Y
  4. Fourth quadrant, the quadrant in the bottom-right, contains all points with positive X, but negative Y

More on quadrants: Quadrant (plane geometry) - Wikipedia

My Solution

def quadrant(x, y)
  if x.positive?
    if y.positive?
      return 1
    end
    4
  elsif y.positive?
    2
  else
    3
  end
end

Better/Alternative solution from Codewars

def quadrant(x, y)
  return 1 if x.positive? && y.positive?
  return 2 if x.negative? && y.positive?
  return 3 if x.negative? && y.negative?
  return 4 if x.positive? && y.negative?
end