mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-04 16:57:32 +08:00
psf/black code formatting (#1277)
This commit is contained in:

committed by
Christian Clauss

parent
07f04a2e55
commit
9eac17a408
@ -28,15 +28,15 @@ def euclidean_distance_sqr(point1, point2):
|
||||
return (point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2
|
||||
|
||||
|
||||
def column_based_sort(array, column = 0):
|
||||
def column_based_sort(array, column=0):
|
||||
"""
|
||||
>>> column_based_sort([(5, 1), (4, 2), (3, 0)], 1)
|
||||
[(3, 0), (5, 1), (4, 2)]
|
||||
"""
|
||||
return sorted(array, key = lambda x: x[column])
|
||||
return sorted(array, key=lambda x: x[column])
|
||||
|
||||
|
||||
def dis_between_closest_pair(points, points_counts, min_dis = float("inf")):
|
||||
def dis_between_closest_pair(points, points_counts, min_dis=float("inf")):
|
||||
"""
|
||||
brute force approach to find distance between closest pair points
|
||||
|
||||
@ -52,14 +52,14 @@ def dis_between_closest_pair(points, points_counts, min_dis = float("inf")):
|
||||
"""
|
||||
|
||||
for i in range(points_counts - 1):
|
||||
for j in range(i+1, points_counts):
|
||||
for j in range(i + 1, points_counts):
|
||||
current_dis = euclidean_distance_sqr(points[i], points[j])
|
||||
if current_dis < min_dis:
|
||||
min_dis = current_dis
|
||||
return min_dis
|
||||
|
||||
|
||||
def dis_between_closest_in_strip(points, points_counts, min_dis = float("inf")):
|
||||
def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")):
|
||||
"""
|
||||
closest pair of points in strip
|
||||
|
||||
@ -74,7 +74,7 @@ def dis_between_closest_in_strip(points, points_counts, min_dis = float("inf")):
|
||||
"""
|
||||
|
||||
for i in range(min(6, points_counts - 1), points_counts):
|
||||
for j in range(max(0, i-6), i):
|
||||
for j in range(max(0, i - 6), i):
|
||||
current_dis = euclidean_distance_sqr(points[i], points[j])
|
||||
if current_dis < min_dis:
|
||||
min_dis = current_dis
|
||||
@ -99,13 +99,13 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
|
||||
return dis_between_closest_pair(points_sorted_on_x, points_counts)
|
||||
|
||||
# recursion
|
||||
mid = points_counts//2
|
||||
closest_in_left = closest_pair_of_points_sqr(points_sorted_on_x,
|
||||
points_sorted_on_y[:mid],
|
||||
mid)
|
||||
closest_in_right = closest_pair_of_points_sqr(points_sorted_on_y,
|
||||
points_sorted_on_y[mid:],
|
||||
points_counts - mid)
|
||||
mid = points_counts // 2
|
||||
closest_in_left = closest_pair_of_points_sqr(
|
||||
points_sorted_on_x, points_sorted_on_y[:mid], mid
|
||||
)
|
||||
closest_in_right = closest_pair_of_points_sqr(
|
||||
points_sorted_on_y, points_sorted_on_y[mid:], points_counts - mid
|
||||
)
|
||||
closest_pair_dis = min(closest_in_left, closest_in_right)
|
||||
|
||||
"""
|
||||
@ -118,8 +118,9 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
|
||||
if abs(point[0] - points_sorted_on_x[mid][0]) < closest_pair_dis:
|
||||
cross_strip.append(point)
|
||||
|
||||
closest_in_strip = dis_between_closest_in_strip(cross_strip,
|
||||
len(cross_strip), closest_pair_dis)
|
||||
closest_in_strip = dis_between_closest_in_strip(
|
||||
cross_strip, len(cross_strip), closest_pair_dis
|
||||
)
|
||||
return min(closest_pair_dis, closest_in_strip)
|
||||
|
||||
|
||||
@ -128,11 +129,13 @@ def closest_pair_of_points(points, points_counts):
|
||||
>>> closest_pair_of_points([(2, 3), (12, 30)], len([(2, 3), (12, 30)]))
|
||||
28.792360097775937
|
||||
"""
|
||||
points_sorted_on_x = column_based_sort(points, column = 0)
|
||||
points_sorted_on_y = column_based_sort(points, column = 1)
|
||||
return (closest_pair_of_points_sqr(points_sorted_on_x,
|
||||
points_sorted_on_y,
|
||||
points_counts)) ** 0.5
|
||||
points_sorted_on_x = column_based_sort(points, column=0)
|
||||
points_sorted_on_y = column_based_sort(points, column=1)
|
||||
return (
|
||||
closest_pair_of_points_sqr(
|
||||
points_sorted_on_x, points_sorted_on_y, points_counts
|
||||
)
|
||||
) ** 0.5
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -1,4 +1,5 @@
|
||||
from numbers import Number
|
||||
|
||||
"""
|
||||
The convex hull problem is problem of finding all the vertices of convex polygon, P of
|
||||
a set of points in a plane such that all the points are either on the vertices of P or
|
||||
@ -47,8 +48,10 @@ class Point:
|
||||
try:
|
||||
x, y = float(x), float(y)
|
||||
except ValueError as e:
|
||||
e.args = ("x and y must be both numeric types "
|
||||
"but got {}, {} instead".format(type(x), type(y)), )
|
||||
e.args = (
|
||||
"x and y must be both numeric types "
|
||||
"but got {}, {} instead".format(type(x), type(y)),
|
||||
)
|
||||
raise
|
||||
|
||||
self.x = x
|
||||
@ -85,7 +88,7 @@ class Point:
|
||||
return False
|
||||
|
||||
def __repr__(self):
|
||||
return "({}, {})".format(self.x, self.y)
|
||||
return "({}, {})".format(self.x, self.y)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.x)
|
||||
@ -132,8 +135,10 @@ def _construct_points(list_of_tuples):
|
||||
try:
|
||||
points.append(Point(p[0], p[1]))
|
||||
except (IndexError, TypeError):
|
||||
print("Ignoring deformed point {}. All points"
|
||||
" must have at least 2 coordinates.".format(p))
|
||||
print(
|
||||
"Ignoring deformed point {}. All points"
|
||||
" must have at least 2 coordinates.".format(p)
|
||||
)
|
||||
return points
|
||||
|
||||
|
||||
@ -189,12 +194,15 @@ def _validate_input(points):
|
||||
if isinstance(points[0], (list, tuple)):
|
||||
points = _construct_points(points)
|
||||
else:
|
||||
raise ValueError("Expecting an iterable of type Point, list or tuple. "
|
||||
"Found objects of type {} instead"
|
||||
.format(type(points[0])))
|
||||
raise ValueError(
|
||||
"Expecting an iterable of type Point, list or tuple. "
|
||||
"Found objects of type {} instead".format(type(points[0]))
|
||||
)
|
||||
elif not hasattr(points, "__iter__"):
|
||||
raise ValueError("Expecting an iterable object "
|
||||
"but got an non-iterable type {}".format(points))
|
||||
raise ValueError(
|
||||
"Expecting an iterable object "
|
||||
"but got an non-iterable type {}".format(points)
|
||||
)
|
||||
except TypeError as e:
|
||||
print("Expecting an iterable of type Point, list or tuple.")
|
||||
raise
|
||||
@ -277,7 +285,7 @@ def convex_hull_bf(points):
|
||||
n = len(points)
|
||||
convex_set = set()
|
||||
|
||||
for i in range(n-1):
|
||||
for i in range(n - 1):
|
||||
for j in range(i + 1, n):
|
||||
points_left_of_ij = points_right_of_ij = False
|
||||
ij_part_of_convex_hull = True
|
||||
@ -353,13 +361,13 @@ def convex_hull_recursive(points):
|
||||
# convex hull
|
||||
|
||||
left_most_point = points[0]
|
||||
right_most_point = points[n-1]
|
||||
right_most_point = points[n - 1]
|
||||
|
||||
convex_set = {left_most_point, right_most_point}
|
||||
upperhull = []
|
||||
lowerhull = []
|
||||
|
||||
for i in range(1, n-1):
|
||||
for i in range(1, n - 1):
|
||||
det = _det(left_most_point, right_most_point, points[i])
|
||||
|
||||
if det > 0:
|
||||
@ -394,7 +402,7 @@ def _construct_hull(points, left, right, convex_set):
|
||||
"""
|
||||
if points:
|
||||
extreme_point = None
|
||||
extreme_point_distance = float('-inf')
|
||||
extreme_point_distance = float("-inf")
|
||||
candidate_points = []
|
||||
|
||||
for p in points:
|
||||
@ -414,8 +422,18 @@ def _construct_hull(points, left, right, convex_set):
|
||||
|
||||
|
||||
def main():
|
||||
points = [(0, 3), (2, 2), (1, 1), (2, 1), (3, 0),
|
||||
(0, 0), (3, 3), (2, -1), (2, -4), (1, -3)]
|
||||
points = [
|
||||
(0, 3),
|
||||
(2, 2),
|
||||
(1, 1),
|
||||
(2, 1),
|
||||
(3, 0),
|
||||
(0, 0),
|
||||
(3, 3),
|
||||
(2, -1),
|
||||
(2, -4),
|
||||
(1, -3),
|
||||
]
|
||||
# the convex set of points is
|
||||
# [(0, 0), (0, 3), (1, -3), (2, -4), (3, 0), (3, 3)]
|
||||
results_recursive = convex_hull_recursive(points)
|
||||
@ -425,5 +443,5 @@ def main():
|
||||
print(results_bf)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
@ -38,7 +38,7 @@ def count_inversions_bf(arr):
|
||||
num_inversions = 0
|
||||
n = len(arr)
|
||||
|
||||
for i in range(n-1):
|
||||
for i in range(n - 1):
|
||||
for j in range(i + 1, n):
|
||||
if arr[i] > arr[j]:
|
||||
num_inversions += 1
|
||||
@ -73,7 +73,7 @@ def count_inversions_recursive(arr):
|
||||
if len(arr) <= 1:
|
||||
return arr, 0
|
||||
else:
|
||||
mid = len(arr)//2
|
||||
mid = len(arr) // 2
|
||||
P = arr[0:mid]
|
||||
Q = arr[mid:]
|
||||
|
||||
@ -119,7 +119,7 @@ def _count_cross_inversions(P, Q):
|
||||
# if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P)
|
||||
# These are all inversions. The claim emerges from the
|
||||
# property that P is sorted.
|
||||
num_inversion += (len(P) - i)
|
||||
num_inversion += len(P) - i
|
||||
R.append(Q[j])
|
||||
j += 1
|
||||
else:
|
||||
@ -127,9 +127,9 @@ def _count_cross_inversions(P, Q):
|
||||
i += 1
|
||||
|
||||
if i < len(P):
|
||||
R.extend(P[i:])
|
||||
R.extend(P[i:])
|
||||
else:
|
||||
R.extend(Q[j:])
|
||||
R.extend(Q[j:])
|
||||
|
||||
return R, num_inversion
|
||||
|
||||
@ -166,6 +166,4 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
main()
|
||||
|
@ -40,8 +40,8 @@ def max_cross_array_sum(array, left, mid, right):
|
||||
|
||||
"""
|
||||
|
||||
max_sum_of_left = max_sum_from_start(array[left:mid+1][::-1])
|
||||
max_sum_of_right = max_sum_from_start(array[mid+1: right+1])
|
||||
max_sum_of_left = max_sum_from_start(array[left : mid + 1][::-1])
|
||||
max_sum_of_right = max_sum_from_start(array[mid + 1 : right + 1])
|
||||
return max_sum_of_left + max_sum_of_right
|
||||
|
||||
|
||||
@ -60,7 +60,7 @@ def max_subarray_sum(array, left, right):
|
||||
# base case: array has only one element
|
||||
if left == right:
|
||||
return array[right]
|
||||
|
||||
|
||||
# Recursion
|
||||
mid = (left + right) // 2
|
||||
left_half_sum = max_subarray_sum(array, left, mid)
|
||||
@ -71,5 +71,6 @@ def max_subarray_sum(array, left, right):
|
||||
|
||||
array = [-2, -5, 6, -2, -3, 1, 5, -6]
|
||||
array_length = len(array)
|
||||
print("Maximum sum of contiguous subarray:", max_subarray_sum(array, 0, array_length - 1))
|
||||
|
||||
print(
|
||||
"Maximum sum of contiguous subarray:", max_subarray_sum(array, 0, array_length - 1)
|
||||
)
|
||||
|
Reference in New Issue
Block a user