Longest Consecutive Sequence LeetCode problem by Sarthak950 · Pull Request #9686 · TheAlgorithms/Python · GitHub
Skip to content

Longest Consecutive Sequence LeetCode problem - #9686

Open
Sarthak950 wants to merge 20 commits into
TheAlgorithms:masterfrom
Sarthak950:master
Open

Sarthak950 wants to merge 20 commits into
TheAlgorithms:masterfrom
Sarthak950:master

Conversation

@Sarthak950

Copy link
Copy Markdown

add a well documented solution with approach for the leet code problem Longest consecutive Sequence Leet code

Comment thread data_structures/arrays/longest_consecutive_sequence.py Outdated
removed the class from the solution

Co-authored-by: Chris O <46587501+ChrisO345@users.noreply.github.com>

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: data_structures/arrays/longest_consecutive_sequence.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 10:5.
parser error: error at 9:4: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~

        # Create a map of all numbers in the array to their index
    ^

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Oct 5, 2023

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.



def longestConsecutiveSequence(self, nums: List[int]) -> int:
# Create a map of all numbers in the array to their index

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: data_structures/arrays/longest_consecutive_sequence.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 10:5.
parser error: error at 9:4: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~

        # Create a map of all numbers in the array to their index
    ^

@algorithms-keeper algorithms-keeper Bot added the require tests Tests [doctest/unittest/pytest] are required label Oct 5, 2023

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.


"""

def longestConsecutiveSequence(self, nums: List[int]) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/longest_consecutive_sequence.py, please provide doctest for the function longestConsecutiveSequence

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: longestConsecutiveSequence

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

"""


def longest_consecutive_sequence(self, nums: List[int]) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/longest_consecutive_sequence.py, please provide doctest for the function longest_consecutive_sequence

"""


def longest_consecutive_sequence(self, nums: List[int]) -> int:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""


def longest_consecutive_sequence(self, nums: List[int]) -> int:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def longest_consecutive_sequence(self, nums: List[int]) -> int:
def longest_consecutive_sequence(nums: list[int]) -> int:

Longest Consecutive Sequence Problem from LeetCode

"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from collections import defaultdict


2. Then, we loop through the numbers, and for each number n, we check if it's already in seen list.
If so, we skip it. Otherwise, we start counting the length of the consecutive sequence starting from n.
This is done by checking if n+1 is in the dictionary. If so, we increment the counter, and set the seen status of n to 1.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure all lines are less than 88 characters long

@algorithms-keeper algorithms-keeper Bot removed the require tests Tests [doctest/unittest/pytest] are required label Oct 5, 2023

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.


"""
//can u add
def longest_consecutive_sequence(nums):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: data_structures/arrays/longest_consecutive_sequence.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 8:3.
parser error: error at 7:2: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~

def longest_consecutive_sequence(nums):
  ^

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread data_structures/arrays/longest_consecutive_sequence.py
@algorithms-keeper algorithms-keeper Bot added the require type hints https://docs.python.org/3/library/typing.html label Oct 5, 2023

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

date: 4 OCT 2023
Longest Consecutive Sequence Problem from LeetCode
"""
def longest_consecutive_sequence(nums):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: longest_consecutive_sequence. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: nums

@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Oct 5, 2023
@Sarthak950
Sarthak950 requested a review from chriso345 October 5, 2023 09:28

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

"""


def longest_consecutive_sequence(nums) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: nums

@algorithms-keeper algorithms-keeper Bot removed the require type hints https://docs.python.org/3/library/typing.html label Oct 5, 2023
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Oct 5, 2023
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Oct 5, 2023
Comment on lines +10 to +28
"""
Finds the length of the longest consecutive sequence in a list of numbers.

Args:
nums (List[int]): A list of integers.

Returns:
int: The length of the longest consecutive sequence.

Examples:
>>> longest_consecutive_sequence([100, 4, 200, 1, 3, 2])
4
>>> longest_consecutive_sequence([1, 2, 3, 4, 5])
5
>>> longest_consecutive_sequence([5, 4, 3, 2, 1])
5
>>> longest_consecutive_sequence([])
0
"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
Finds the length of the longest consecutive sequence in a list of numbers.
Args:
nums (List[int]): A list of integers.
Returns:
int: The length of the longest consecutive sequence.
Examples:
>>> longest_consecutive_sequence([100, 4, 200, 1, 3, 2])
4
>>> longest_consecutive_sequence([1, 2, 3, 4, 5])
5
>>> longest_consecutive_sequence([5, 4, 3, 2, 1])
5
>>> longest_consecutive_sequence([])
0
"""
"""
Finds the length of the longest consecutive sequence in a list of numbers.
nums: A list of integers.
int: The length of the longest consecutive sequence.
>>> longest_consecutive_sequence([100, 4, 200, 1, 3, 2])
4
>>> longest_consecutive_sequence([1, 2, 3, 4, 5])
5
>>> longest_consecutive_sequence([5, 4, 3, 2, 1])
5
>>> longest_consecutive_sequence([])
0
"""

Please read CONTRIBUTING.md to see how to indent docstrings.

Finds the length of the longest consecutive sequence in a list of numbers.

Args:
nums (List[int]): A list of integers.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refrain from declaring types within a docstring.

@Sarthak950
Sarthak950 requested a review from chriso345 October 6, 2023 01:46
@Sarthak950

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants