Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypy] Fix type annotations in data_structures/stacks/next_greater_element.py #5763

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix type annotations in next_greater_element.py
  • Loading branch information
dylanbuchi committed Nov 4, 2021
commit 711d78b15cc2edf428d9717f0a0723fe2f0be6d1
18 changes: 11 additions & 7 deletions data_structures/stacks/next_greater_element.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from __future__ import annotations

arr = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0]
expect = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]


def next_greatest_element_slow(arr: list) -> list:
def next_greatest_element_slow(arr: list[float]) -> list[float]:
"""
Get the Next Greatest Element (NGE) for all elements in a list.
Maximum element present after the current one which is also greater than the
current one.
>>> next_greatest_element_slow(arr) == expect
True
"""

result = []

for i in range(0, len(arr), 1):
next = -1
next: float = -1
for j in range(i + 1, len(arr), 1):
if arr[i] < arr[j]:
next = arr[j]
Expand All @@ -21,7 +25,7 @@ def next_greatest_element_slow(arr: list) -> list:
return result


def next_greatest_element_fast(arr: list) -> list:
def next_greatest_element_fast(arr: list[float]) -> list[float]:
"""
Like next_greatest_element_slow() but changes the loops to use
enumerate() instead of range(len()) for the outer loop and
Expand All @@ -31,7 +35,7 @@ def next_greatest_element_fast(arr: list) -> list:
"""
result = []
for i, outer in enumerate(arr):
next = -1
next: float = -1
for inner in arr[i + 1 :]:
if outer < inner:
next = inner
Expand All @@ -40,7 +44,7 @@ def next_greatest_element_fast(arr: list) -> list:
return result


def next_greatest_element(arr: list) -> list:
def next_greatest_element(arr: list[float]) -> list[float]:
"""
Get the Next Greatest Element (NGE) for all elements in a list.
Maximum element present after the current one which is also greater than the
Expand All @@ -53,8 +57,8 @@ def next_greatest_element(arr: list) -> list:
>>> next_greatest_element(arr) == expect
True
"""
stack = []
result = [-1] * len(arr)
stack: list[float] = []
result: list[float] = [-1] * len(arr)

for index in reversed(range(len(arr))):
if len(stack):
Expand Down