mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 10:28:39 +00:00
Compare commits
5 Commits
b1a8601df7
...
1e5066a4b9
Author | SHA1 | Date | |
---|---|---|---|
|
1e5066a4b9 | ||
|
8da7cb385e | ||
|
f9440027f7 | ||
|
0f047f306e | ||
|
db6bd4b17f |
@ -586,6 +586,7 @@
|
|||||||
* [Hardy Ramanujanalgo](maths/hardy_ramanujanalgo.py)
|
* [Hardy Ramanujanalgo](maths/hardy_ramanujanalgo.py)
|
||||||
* [Hexagonal Number](maths/hexagonal_number.py)
|
* [Hexagonal Number](maths/hexagonal_number.py)
|
||||||
* [Integration By Simpson Approx](maths/integration_by_simpson_approx.py)
|
* [Integration By Simpson Approx](maths/integration_by_simpson_approx.py)
|
||||||
|
* [Interquartile Range](maths/interquartile_range.py)
|
||||||
* [Is Int Palindrome](maths/is_int_palindrome.py)
|
* [Is Int Palindrome](maths/is_int_palindrome.py)
|
||||||
* [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py)
|
* [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py)
|
||||||
* [Is Square Free](maths/is_square_free.py)
|
* [Is Square Free](maths/is_square_free.py)
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
import heapq
|
import heapq
|
||||||
from copy import deepcopy
|
|
||||||
|
|
||||||
|
|
||||||
class UniformCostSearch:
|
class UniformCostSearch:
|
||||||
def __init__(self, current, final, grid):
|
def __init__(self, current: list, final: list, grid: list[list]) -> None:
|
||||||
self.m = len(grid[0])
|
self.m = len(grid[0])
|
||||||
self.n = len(grid)
|
self.n = len(grid)
|
||||||
|
|
||||||
@ -76,7 +75,9 @@ class UniformCostSearch:
|
|||||||
(-1, 1),
|
(-1, 1),
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_shortest_path(self, start, end, dist, dxy):
|
def get_shortest_path(
|
||||||
|
self, start: list, end: list, dist: list[list], dxy: list[tuple]
|
||||||
|
) -> list:
|
||||||
shortest_path = []
|
shortest_path = []
|
||||||
curr_node = end
|
curr_node = end
|
||||||
while curr_node != start:
|
while curr_node != start:
|
||||||
@ -97,7 +98,15 @@ class UniformCostSearch:
|
|||||||
shortest_path.append(start)
|
shortest_path.append(start)
|
||||||
return shortest_path
|
return shortest_path
|
||||||
|
|
||||||
def ucs(self, current, final, grid, prev, dxy, goal_answer):
|
def ucs(
|
||||||
|
self,
|
||||||
|
current: list,
|
||||||
|
final: list[list],
|
||||||
|
grid: list[list],
|
||||||
|
prev: list[list],
|
||||||
|
dxy: list[tuple],
|
||||||
|
goal_answer: list,
|
||||||
|
) -> list:
|
||||||
dist = [[float("inf") for _ in range(self.m)] for _ in range(self.n)]
|
dist = [[float("inf") for _ in range(self.m)] for _ in range(self.n)]
|
||||||
visited = [[0 for _ in range(self.m)] for _ in range(self.n)]
|
visited = [[0 for _ in range(self.m)] for _ in range(self.n)]
|
||||||
|
|
||||||
@ -145,8 +154,8 @@ class UniformCostSearch:
|
|||||||
heapq.heappush(heap, (new_dist, x + dx, y + dy))
|
heapq.heappush(heap, (new_dist, x + dx, y + dy))
|
||||||
|
|
||||||
def your_algorithm(
|
def your_algorithm(
|
||||||
self, start_point: List[int], end_point: List[int], grid: List[List[int]]
|
self, start_point: list, end_point: list, grid: list[list]
|
||||||
) -> List[int]:
|
) -> list[int]:
|
||||||
prev = [[None for _ in range(self.m)] for _ in range(self.n)]
|
prev = [[None for _ in range(self.m)] for _ in range(self.n)]
|
||||||
dxy = []
|
dxy = []
|
||||||
if start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] < 0:
|
if start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] < 0:
|
||||||
@ -168,7 +177,7 @@ class UniformCostSearch:
|
|||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
def run():
|
def run() -> None:
|
||||||
executed_object = UniformCostSearch(
|
executed_object = UniformCostSearch(
|
||||||
[0, 7],
|
[0, 7],
|
||||||
[19, 17],
|
[19, 17],
|
||||||
|
66
maths/interquartile_range.py
Normal file
66
maths/interquartile_range.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
"""
|
||||||
|
An implementation of interquartile range (IQR) which is a measure of statistical
|
||||||
|
dispersion, which is the spread of the data.
|
||||||
|
|
||||||
|
The function takes the list of numeric values as input and returns the IQR.
|
||||||
|
|
||||||
|
Script inspired by this Wikipedia article:
|
||||||
|
https://en.wikipedia.org/wiki/Interquartile_range
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
def find_median(nums: list[int | float]) -> float:
|
||||||
|
"""
|
||||||
|
This is the implementation of the median.
|
||||||
|
:param nums: The list of numeric nums
|
||||||
|
:return: Median of the list
|
||||||
|
>>> find_median(nums=([1, 2, 2, 3, 4]))
|
||||||
|
2
|
||||||
|
>>> find_median(nums=([1, 2, 2, 3, 4, 4]))
|
||||||
|
2.5
|
||||||
|
>>> find_median(nums=([-1, 2, 0, 3, 4, -4]))
|
||||||
|
1.5
|
||||||
|
>>> find_median(nums=([1.1, 2.2, 2, 3.3, 4.4, 4]))
|
||||||
|
2.65
|
||||||
|
"""
|
||||||
|
div, mod = divmod(len(nums), 2)
|
||||||
|
if mod:
|
||||||
|
return nums[div]
|
||||||
|
return (nums[div] + nums[(div) - 1]) / 2
|
||||||
|
|
||||||
|
|
||||||
|
def interquartile_range(nums: list[int | float]) -> float:
|
||||||
|
"""
|
||||||
|
Return the interquartile range for a list of numeric values.
|
||||||
|
:param nums: The list of numeric values.
|
||||||
|
:return: interquartile range
|
||||||
|
|
||||||
|
>>> interquartile_range(nums=[4, 1, 2, 3, 2])
|
||||||
|
2.0
|
||||||
|
>>> interquartile_range(nums = [-2, -7, -10, 9, 8, 4, -67, 45])
|
||||||
|
17.0
|
||||||
|
>>> interquartile_range(nums = [-2.1, -7.1, -10.1, 9.1, 8.1, 4.1, -67.1, 45.1])
|
||||||
|
17.2
|
||||||
|
>>> interquartile_range(nums = [0, 0, 0, 0, 0])
|
||||||
|
0.0
|
||||||
|
>>> interquartile_range(nums=[])
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: The list is empty. Provide a non-empty list.
|
||||||
|
"""
|
||||||
|
if not nums:
|
||||||
|
raise ValueError("The list is empty. Provide a non-empty list.")
|
||||||
|
nums.sort()
|
||||||
|
length = len(nums)
|
||||||
|
div, mod = divmod(length, 2)
|
||||||
|
q1 = find_median(nums[:div])
|
||||||
|
half_length = sum((div, mod))
|
||||||
|
q3 = find_median(nums[half_length:length])
|
||||||
|
return q3 - q1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
Loading…
x
Reference in New Issue
Block a user