From 88bd9d57c16ff7ad7e1d52e2c44a305b49b5a494 Mon Sep 17 00:00:00 2001 From: "Capt. Cutlass" <5120290+ParanoidUser@users.noreply.github.com> Date: Sun, 18 Feb 2024 21:10:08 -0500 Subject: [PATCH] feat(8-kyu): kata/collinearity --- docs/README.md | 2 +- kata/8-kyu/collinearity/README.md | 45 +++++++++++++++++++ kata/8-kyu/collinearity/main/Kata.java | 5 +++ .../8-kyu/collinearity/test/SolutionTest.java | 34 ++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 kata/8-kyu/collinearity/README.md create mode 100644 kata/8-kyu/collinearity/main/Kata.java create mode 100644 kata/8-kyu/collinearity/test/SolutionTest.java diff --git a/docs/README.md b/docs/README.md index a97f92f60..6305f0c5f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,7 +25,7 @@ slug. | [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) | |:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:| -| 0 | 1 | 2 | 26 | 44 | 421 | 562 | 208 | 55 | 79 | +| 0 | 1 | 2 | 26 | 44 | 421 | 562 | 209 | 55 | 79 | **Note:** The source code is written in Java 17 and may use language features that are incompatible with Java 8, 11. diff --git a/kata/8-kyu/collinearity/README.md b/kata/8-kyu/collinearity/README.md new file mode 100644 index 000000000..815be24b6 --- /dev/null +++ b/kata/8-kyu/collinearity/README.md @@ -0,0 +1,45 @@ +# [Collinearity](https://www.codewars.com/kata/collinearity "https://www.codewars.com/kata/65ba420888906c1f86e1e680") + +## Theoretical Material + +You are given two vectors starting from the origin (x=0, y=0) with coordinates (x1,y1) and (x2,y2). Your task is to find +out if these vectors are collinear. Collinear vectors are vectors that lie on the same straight line. They can be +directed in the same or opposite directions. One vector can be obtained from another by multiplying it by a certain +number. In terms of coordinates, vectors (x1, y1) and (x2, y2) are collinear if (x1, y1) = (k\*x2, k\*y2) , where k is +any number acting as a coefficient. + +![](https://d138zd1ktt9iqe.cloudfront.net/media/seo_landing_files/collinear-vectors-1627481628.png) + +For more information, check out this [article on collinearity](https://www.cuemath.com/geometry/collinear-vectors/). + +## Problem Description + +Write the function `collinearity(x1, y1, x2, y2)`, which returns a Boolean type depending on whether the vectors are +collinear or not. + +``` +all coordinates are integers +-1000 <= any coordinate <= 1000 +``` + +## Notes + +- All vectors start from the origin (x=0, y=0). +- Be careful when handling cases where x1, x2, y1, or y2 are zero to avoid division by zero errors. +- A vector with coordinates (0, 0) is collinear to all vectors. + +## Examples + +``` +(1,1,1,1) ➞ true +(1,2,2,4) ➞ true +(1,1,6,1) ➞ false +(1,2,-1,-2) ➞ true +(1,2,1,-2) ➞ false +(4,0,11,0) ➞ true +(0,1,6,0) ➞ false +(4,4,0,4) ➞ false +(0,0,0,0) ➞ true +(0,0,1,0) ➞ true +(5,7,0,0) ➞ true +``` \ No newline at end of file diff --git a/kata/8-kyu/collinearity/main/Kata.java b/kata/8-kyu/collinearity/main/Kata.java new file mode 100644 index 000000000..ad31ce024 --- /dev/null +++ b/kata/8-kyu/collinearity/main/Kata.java @@ -0,0 +1,5 @@ +interface Kata { + static boolean collinearity(int x1, int y1, int x2, int y2) { + return x1 * y2 == y1 * x2; + } +} \ No newline at end of file diff --git a/kata/8-kyu/collinearity/test/SolutionTest.java b/kata/8-kyu/collinearity/test/SolutionTest.java new file mode 100644 index 000000000..b34c170b9 --- /dev/null +++ b/kata/8-kyu/collinearity/test/SolutionTest.java @@ -0,0 +1,34 @@ +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SolutionTest { + @ParameterizedTest + @CsvSource(textBlock = """ + 0, 0, 0, 0 + 0, 0, 1, 0 + 1, 1, 1, 1 + 1, 2, 2, 4 + 1, 1, -1, -1 + 1, -2, -2, 4 + 4, 0, 11, 0 + 5, 7, 0, 0 + """) + void collinear(int x1, int y1, int x2, int y2) { + assertTrue(Kata.collinearity(x1, y1, x2, y2)); + } + + @ParameterizedTest + @CsvSource(textBlock = """ + 0, 1, 6, 0 + 1, 1, 6, 1 + 1, 2, 1, -2 + 4, 4, 0, 4 + -9, 171, -574, -214 + """) + void nonCollinear(int x1, int y1, int x2, int y2) { + assertFalse(Kata.collinearity(x1, y1, x2, y2)); + } +} \ No newline at end of file