Skip to content

Commit f17e982

Browse files
cclaussgithub-actions
and
github-actions
authored
psf/black changes to next_greater_element.py (TheAlgorithms#1817)
* psf/black changes to next_greater_element.py * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 96df906 commit f17e982

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed
+42-30
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
arr = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0]
2+
expect = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]
23

3-
def next_greatest_element_slow(arr):
4+
5+
def next_greatest_element_slow(arr: list) -> list:
46
"""
5-
Function to get Next Greatest Element (NGE) pair for all elements of list
6-
Maximum element present afterwards the current one which is also greater than current one
7-
>>> next_greatest_element_slow(arr)
8-
[-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]
7+
Get the Next Greatest Element (NGE) for all elements in a list.
8+
Maximum element present after the current one which is also greater than the
9+
current one.
10+
>>> next_greatest_element_slow(arr) == expect
11+
True
912
"""
1013
result = []
1114
for i in range(0, len(arr), 1):
@@ -18,39 +21,40 @@ def next_greatest_element_slow(arr):
1821
return result
1922

2023

21-
def next_greatest_element_fast(arr):
24+
def next_greatest_element_fast(arr: list) -> list:
2225
"""
2326
Like next_greatest_element_slow() but changes the loops to use
2427
enumerate() instead of range(len()) for the outer loop and
2528
for in a slice of arr for the inner loop.
26-
>>> next_greatest_element_fast(arr)
27-
[-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]
29+
>>> next_greatest_element_fast(arr) == expect
30+
True
2831
"""
2932
result = []
3033
for i, outer in enumerate(arr):
3134
next = -1
32-
for inner in arr[i + 1:]:
35+
for inner in arr[i + 1 :]:
3336
if outer < inner:
3437
next = inner
3538
break
3639
result.append(next)
3740
return result
3841

3942

40-
def next_greatest_element(arr):
43+
def next_greatest_element(arr: list) -> list:
4144
"""
42-
Function to get Next Greatest Element (NGE) pair for all elements of list
43-
Maximum element present afterwards the current one which is also greater than current one
44-
45-
Naive way to solve this is to take two loops and check for the next bigger number but that will make the
46-
time complexity as O(n^2). The better way to solve this would be to use a stack to keep track of maximum
47-
number givig a linear time complex solution.
48-
49-
>>> next_greatest_element(arr)
50-
[-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]
45+
Get the Next Greatest Element (NGE) for all elements in a list.
46+
Maximum element present after the current one which is also greater than the
47+
current one.
48+
49+
A naive way to solve this is to take two loops and check for the next bigger
50+
number but that will make the time complexity as O(n^2). The better way to solve
51+
this would be to use a stack to keep track of maximum number giving a linear time
52+
solution.
53+
>>> next_greatest_element(arr) == expect
54+
True
5155
"""
52-
stack = []
53-
result = [-1]*len(arr)
56+
stack = []
57+
result = [-1] * len(arr)
5458

5559
for index in reversed(range(len(arr))):
5660
if len(stack):
@@ -63,7 +67,7 @@ def next_greatest_element(arr):
6367
result[index] = stack[-1]
6468

6569
stack.append(arr[index])
66-
70+
6771
return result
6872

6973

@@ -76,11 +80,19 @@ def next_greatest_element(arr):
7680
print(next_greatest_element_fast(arr))
7781
print(next_greatest_element(arr))
7882

79-
setup = ("from __main__ import arr, next_greatest_element_slow, "
80-
"next_greatest_element_fast, next_greatest_element")
81-
print("next_greatest_element_slow():",
82-
timeit("next_greatest_element_slow(arr)", setup=setup))
83-
print("next_greatest_element_fast():",
84-
timeit("next_greatest_element_fast(arr)", setup=setup))
85-
print(" next_greatest_element():",
86-
timeit("next_greatest_element(arr)", setup=setup))
83+
setup = (
84+
"from __main__ import arr, next_greatest_element_slow, "
85+
"next_greatest_element_fast, next_greatest_element"
86+
)
87+
print(
88+
"next_greatest_element_slow():",
89+
timeit("next_greatest_element_slow(arr)", setup=setup),
90+
)
91+
print(
92+
"next_greatest_element_fast():",
93+
timeit("next_greatest_element_fast(arr)", setup=setup),
94+
)
95+
print(
96+
" next_greatest_element():",
97+
timeit("next_greatest_element(arr)", setup=setup),
98+
)

maths/allocation_number.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@ def allocation_num(number_of_bytes: int, partitions: int) -> List[str]:
3838
ValueError: partitions must be a positive number!
3939
"""
4040
if partitions <= 0:
41-
raise ValueError('partitions must be a positive number!')
41+
raise ValueError("partitions must be a positive number!")
4242
if partitions >= number_of_bytes:
43-
raise ValueError('partitions can not >= number_of_bytes!')
43+
raise ValueError("partitions can not >= number_of_bytes!")
4444
bytes_per_partition = number_of_bytes // partitions
45-
allocation_list = [f'0-{bytes_per_partition}']
45+
allocation_list = [f"0-{bytes_per_partition}"]
4646
for i in range(1, partitions - 1):
47-
length = f'{bytes_per_partition * i + 1}-{bytes_per_partition * (i + 1)}'
47+
length = f"{bytes_per_partition * i + 1}-{bytes_per_partition * (i + 1)}"
4848
allocation_list.append(length)
49-
allocation_list.append(f'{(bytes_per_partition * (partitions - 1)) + 1}-'
50-
f'{number_of_bytes}')
49+
allocation_list.append(
50+
f"{(bytes_per_partition * (partitions - 1)) + 1}-" f"{number_of_bytes}"
51+
)
5152
return allocation_list
5253

5354

54-
if __name__ == '__main__':
55+
if __name__ == "__main__":
5556
import doctest
57+
5658
doctest.testmod()

0 commit comments

Comments
 (0)