Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(8-kyu): kata/collinearity #521

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions kata/8-kyu/collinearity/README.md
Original file line number Diff line number Diff line change
@@ -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
```
5 changes: 5 additions & 0 deletions kata/8-kyu/collinearity/main/Kata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface Kata {
static boolean collinearity(int x1, int y1, int x2, int y2) {
return x1 * y2 == y1 * x2;
}
}
34 changes: 34 additions & 0 deletions kata/8-kyu/collinearity/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}