mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Add solution for Project Euler problem 102 (#4051)
* Added solution for Project Euler problem 102 * Got rid of map functions * Snake case variable names * Type hints * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
ad5108d6a4
commit
0ccb213c11
|
@ -106,7 +106,7 @@
|
||||||
* [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py)
|
* [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py)
|
||||||
* [Octal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/octal_to_decimal.py)
|
* [Octal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/octal_to_decimal.py)
|
||||||
* [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py)
|
* [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py)
|
||||||
* [Roman To Integer](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_to_integer.py)
|
* [Roman Numerals](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_numerals.py)
|
||||||
* [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py)
|
* [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py)
|
||||||
* [Weight Conversion](https://github.com/TheAlgorithms/Python/blob/master/conversions/weight_conversion.py)
|
* [Weight Conversion](https://github.com/TheAlgorithms/Python/blob/master/conversions/weight_conversion.py)
|
||||||
|
|
||||||
|
@ -746,6 +746,8 @@
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_099/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_099/sol1.py)
|
||||||
* Problem 101
|
* Problem 101
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_101/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_101/sol1.py)
|
||||||
|
* Problem 102
|
||||||
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_102/sol1.py)
|
||||||
* Problem 112
|
* Problem 112
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_112/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_112/sol1.py)
|
||||||
* Problem 113
|
* Problem 113
|
||||||
|
@ -760,6 +762,8 @@
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_125/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_125/sol1.py)
|
||||||
* Problem 129
|
* Problem 129
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_129/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_129/sol1.py)
|
||||||
|
* Problem 135
|
||||||
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_135/sol1.py)
|
||||||
* Problem 173
|
* Problem 173
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_173/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_173/sol1.py)
|
||||||
* Problem 174
|
* Problem 174
|
||||||
|
|
0
project_euler/problem_102/__init__.py
Normal file
0
project_euler/problem_102/__init__.py
Normal file
1000
project_euler/problem_102/p102_triangles.txt
Normal file
1000
project_euler/problem_102/p102_triangles.txt
Normal file
File diff suppressed because it is too large
Load Diff
81
project_euler/problem_102/sol1.py
Normal file
81
project_euler/problem_102/sol1.py
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
"""
|
||||||
|
Three distinct points are plotted at random on a Cartesian plane,
|
||||||
|
for which -1000 ≤ x, y ≤ 1000, such that a triangle is formed.
|
||||||
|
|
||||||
|
Consider the following two triangles:
|
||||||
|
|
||||||
|
A(-340,495), B(-153,-910), C(835,-947)
|
||||||
|
|
||||||
|
X(-175,41), Y(-421,-714), Z(574,-645)
|
||||||
|
|
||||||
|
It can be verified that triangle ABC contains the origin, whereas
|
||||||
|
triangle XYZ does not.
|
||||||
|
|
||||||
|
Using triangles.txt (right click and 'Save Link/Target As...'), a 27K text
|
||||||
|
file containing the coordinates of one thousand "random" triangles, find
|
||||||
|
the number of triangles for which the interior contains the origin.
|
||||||
|
|
||||||
|
NOTE: The first two examples in the file represent the triangles in the
|
||||||
|
example given above.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List, Tuple
|
||||||
|
|
||||||
|
|
||||||
|
def vector_product(point1: Tuple[int, int], point2: Tuple[int, int]) -> int:
|
||||||
|
"""
|
||||||
|
Return the 2-d vector product of two vectors.
|
||||||
|
>>> vector_product((1, 2), (-5, 0))
|
||||||
|
10
|
||||||
|
>>> vector_product((3, 1), (6, 10))
|
||||||
|
24
|
||||||
|
"""
|
||||||
|
return point1[0] * point2[1] - point1[1] * point2[0]
|
||||||
|
|
||||||
|
|
||||||
|
def contains_origin(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int) -> bool:
|
||||||
|
"""
|
||||||
|
Check if the triangle given by the points A(x1, y1), B(x2, y2), C(x3, y3)
|
||||||
|
contains the origin.
|
||||||
|
>>> contains_origin(-340, 495, -153, -910, 835, -947)
|
||||||
|
True
|
||||||
|
>>> contains_origin(-175, 41, -421, -714, 574, -645)
|
||||||
|
False
|
||||||
|
"""
|
||||||
|
point_a: Tuple[int, int] = (x1, y1)
|
||||||
|
point_a_to_b: Tuple[int, int] = (x2 - x1, y2 - y1)
|
||||||
|
point_a_to_c: Tuple[int, int] = (x3 - x1, y3 - y1)
|
||||||
|
a: float = -vector_product(point_a, point_a_to_b) / vector_product(
|
||||||
|
point_a_to_c, point_a_to_b
|
||||||
|
)
|
||||||
|
b: float = +vector_product(point_a, point_a_to_c) / vector_product(
|
||||||
|
point_a_to_c, point_a_to_b
|
||||||
|
)
|
||||||
|
|
||||||
|
return a > 0 and b > 0 and a + b < 1
|
||||||
|
|
||||||
|
|
||||||
|
def solution(filename: str = "p102_triangles.txt") -> int:
|
||||||
|
"""
|
||||||
|
Find the number of triangles whose interior contains the origin.
|
||||||
|
>>> solution("test_triangles.txt")
|
||||||
|
1
|
||||||
|
"""
|
||||||
|
data: str = Path(__file__).parent.joinpath(filename).read_text(encoding="utf-8")
|
||||||
|
|
||||||
|
triangles: List[List[int]] = []
|
||||||
|
for line in data.strip().split("\n"):
|
||||||
|
triangles.append([int(number) for number in line.split(",")])
|
||||||
|
|
||||||
|
ret: int = 0
|
||||||
|
triangle: List[int]
|
||||||
|
|
||||||
|
for triangle in triangles:
|
||||||
|
ret += contains_origin(*triangle)
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(f"{solution() = }")
|
2
project_euler/problem_102/test_triangles.txt
Normal file
2
project_euler/problem_102/test_triangles.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
-340,495,-153,-910,835,-947
|
||||||
|
-175,41,-421,-714,574,-645
|
Loading…
Reference in New Issue
Block a user