mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Remove duplicate implementation of median of two arrays algorithm (#11420)
* Remove duplicate implementation of median of two arrays algorithm Remove maths/median_of_two_arrays.py because the repo has two implementations of this algorithm, with data_structures/arrays/median_two_array.py being the other. Even though maths/median_of_two_arrays.py is the older implementation, the newer implementation is better documented, has better error handling, and is already located in a more appropriate directory. * updating DIRECTORY.md --------- Co-authored-by: tianyizheng02 <tianyizheng02@users.noreply.github.com>
This commit is contained in:
parent
70bd06db46
commit
723cf9c428
|
@ -661,7 +661,6 @@
|
|||
* [Manhattan Distance](maths/manhattan_distance.py)
|
||||
* [Matrix Exponentiation](maths/matrix_exponentiation.py)
|
||||
* [Max Sum Sliding Window](maths/max_sum_sliding_window.py)
|
||||
* [Median Of Two Arrays](maths/median_of_two_arrays.py)
|
||||
* [Minkowski Distance](maths/minkowski_distance.py)
|
||||
* [Mobius Function](maths/mobius_function.py)
|
||||
* [Modular Division](maths/modular_division.py)
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
from __future__ import annotations
|
||||
|
||||
|
||||
def median_of_two_arrays(nums1: list[float], nums2: list[float]) -> float:
|
||||
"""
|
||||
>>> median_of_two_arrays([1, 2], [3])
|
||||
2
|
||||
>>> median_of_two_arrays([0, -1.1], [2.5, 1])
|
||||
0.5
|
||||
>>> median_of_two_arrays([], [2.5, 1])
|
||||
1.75
|
||||
>>> median_of_two_arrays([], [0])
|
||||
0
|
||||
>>> median_of_two_arrays([], [])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: list index out of range
|
||||
"""
|
||||
all_numbers = sorted(nums1 + nums2)
|
||||
div, mod = divmod(len(all_numbers), 2)
|
||||
if mod == 1:
|
||||
return all_numbers[div]
|
||||
else:
|
||||
return (all_numbers[div] + all_numbers[div - 1]) / 2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
array_1 = [float(x) for x in input("Enter the elements of first array: ").split()]
|
||||
array_2 = [float(x) for x in input("Enter the elements of second array: ").split()]
|
||||
print(f"The median of two arrays is: {median_of_two_arrays(array_1, array_2)}")
|
Loading…
Reference in New Issue
Block a user