From cc600857f317bb44c1fb1942f87ad30e309c8d78 Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Wed, 4 Oct 2023 15:48:38 +0530 Subject: [PATCH 01/19] Longest Consecutive Sequence Leet code Prooblem in python --- .../arrays/longest_consecutive_sequence.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 data_structures/arrays/longest_consecutive_sequence.py diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py new file mode 100644 index 000000000000..7de615ffcd77 --- /dev/null +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -0,0 +1,53 @@ +''' +author: Sarthak Sharma https://github.com/Sarthak950 https://sarthak950.netlify.app +date: 4 OCT 2023 +Longest Consecutive Sequence Problem from LeetCode + +''' + +class Solution: + def longestConsecutiveSequence(self, nums: List[int]) -> int: + # Create a map of all numbers in the array to their index + nmap = defaultdict(int) + for i in range(len(nums)): + if nums[i] not in nmap: nmap[nums[i]] = i + + # Create a seen array to keep track of whether a number has been seen before + seen, ans = [0] * len(nums), 0 + + # Iterate through each number in the array + for n in nums: + # Set curr to the current number, and count to 1 + curr, count = n, 1 + + # If we've already seen this number, skip it + if seen[nmap[n]]: continue + + # Otherwise, iterate through all consecutive numbers after curr + while curr+1 in nmap: + # Increment curr + curr += 1 + + # Check if we've seen this number before + ix = nmap[curr] + if seen[ix]: + # If we have, add it to the count and break out of the loop + count += seen[ix] + break + else: + # Otherwise, add it to the seen array and increment the count + seen[ix] = 1 + count += 1 + + # Add the count to the seen array and update the answer + seen[nmap[n]], ans = count, max(ans, count) + + # Return the answer + return ans + +''' +Idea +1. First, we put all the numbers into a dictionary, and note the index of each number. This is to make sure the lookup time when we check if a number is in the list is O(1). We also initialize a seen list for later use. + +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. Otherwise, we set the seen status of n to 1, and break the loop. This is because if n+1 is not in the dictionary, then the consecutive sequence starting from n is over. And we don't need to count the length of the consecutive sequence starting from n+1, since we will eventually count it when we reach n+1. +''' From 66aa0a67c48c3406b81f5181dfcf8acae4fb227f Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Wed, 4 Oct 2023 15:54:28 +0530 Subject: [PATCH 02/19] change the formatting in the explanation --- .../arrays/longest_consecutive_sequence.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index 7de615ffcd77..d37f43e10648 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -45,9 +45,18 @@ def longestConsecutiveSequence(self, nums: List[int]) -> int: # Return the answer return ans + ''' Idea -1. First, we put all the numbers into a dictionary, and note the index of each number. This is to make sure the lookup time when we check if a number is in the list is O(1). We also initialize a seen list for later use. +1. First, we put all the numbers into a dictionary, and note the index of each number. + This is to make sure the lookup time when we check if a number is in the list is O(1). + We also initialize a seen list for later use. + +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. + Otherwise, we set the seen status of n to 1, and break the loop. + This is because if n+1 is not in the dictionary, then the consecutive sequence starting from n is over. + And we don't need to count the length of the consecutive sequence starting from n+1, since we will eventually count it when we reach n+1. -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. Otherwise, we set the seen status of n to 1, and break the loop. This is because if n+1 is not in the dictionary, then the consecutive sequence starting from n is over. And we don't need to count the length of the consecutive sequence starting from n+1, since we will eventually count it when we reach n+1. ''' From 2e406a0b4a8380696ce593f8c307b2fa4c6d5d21 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:36:45 +0000 Subject: [PATCH 03/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../arrays/longest_consecutive_sequence.py | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index d37f43e10648..a0effa8022c3 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -1,33 +1,36 @@ -''' +""" author: Sarthak Sharma https://github.com/Sarthak950 https://sarthak950.netlify.app date: 4 OCT 2023 Longest Consecutive Sequence Problem from LeetCode -''' +""" + class Solution: def longestConsecutiveSequence(self, nums: List[int]) -> int: # Create a map of all numbers in the array to their index nmap = defaultdict(int) for i in range(len(nums)): - if nums[i] not in nmap: nmap[nums[i]] = i - + if nums[i] not in nmap: + nmap[nums[i]] = i + # Create a seen array to keep track of whether a number has been seen before seen, ans = [0] * len(nums), 0 - + # Iterate through each number in the array for n in nums: # Set curr to the current number, and count to 1 curr, count = n, 1 - + # If we've already seen this number, skip it - if seen[nmap[n]]: continue - + if seen[nmap[n]]: + continue + # Otherwise, iterate through all consecutive numbers after curr - while curr+1 in nmap: + while curr + 1 in nmap: # Increment curr curr += 1 - + # Check if we've seen this number before ix = nmap[curr] if seen[ix]: @@ -38,25 +41,25 @@ def longestConsecutiveSequence(self, nums: List[int]) -> int: # Otherwise, add it to the seen array and increment the count seen[ix] = 1 count += 1 - + # Add the count to the seen array and update the answer seen[nmap[n]], ans = count, max(ans, count) - + # Return the answer return ans - - -''' + + +""" Idea -1. First, we put all the numbers into a dictionary, and note the index of each number. - This is to make sure the lookup time when we check if a number is in the list is O(1). +1. First, we put all the numbers into a dictionary, and note the index of each number. + This is to make sure the lookup time when we check if a number is in the list is O(1). We also initialize a seen list for later use. 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. - Otherwise, we set the seen status of n to 1, and break the loop. - This is because if n+1 is not in the dictionary, then the consecutive sequence starting from n is over. + 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. + Otherwise, we set the seen status of n to 1, and break the loop. + This is because if n+1 is not in the dictionary, then the consecutive sequence starting from n is over. And we don't need to count the length of the consecutive sequence starting from n+1, since we will eventually count it when we reach n+1. -''' +""" From dcf7fa0fb8bc36698b78422aa01c1f91445ac1b3 Mon Sep 17 00:00:00 2001 From: Sarthak <93645760+Sarthak950@users.noreply.github.com> Date: Thu, 5 Oct 2023 11:53:53 +0530 Subject: [PATCH 04/19] Update data_structures/arrays/longest_consecutive_sequence.py removed the class from the solution Co-authored-by: Chris O <46587501+ChrisO345@users.noreply.github.com> --- data_structures/arrays/longest_consecutive_sequence.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index a0effa8022c3..cd5abd53bc2d 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -6,7 +6,6 @@ """ -class Solution: def longestConsecutiveSequence(self, nums: List[int]) -> int: # Create a map of all numbers in the array to their index nmap = defaultdict(int) From 5baa5000d6c5d4e8f519e73d50a837ac4b872065 Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Thu, 5 Oct 2023 13:50:09 +0530 Subject: [PATCH 05/19] removed the function from the class and fixed the indentation #9686 --- .../arrays/longest_consecutive_sequence.py | 74 +++++++++---------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index cd5abd53bc2d..497962e77bcc 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -5,47 +5,39 @@ """ - - def longestConsecutiveSequence(self, nums: List[int]) -> int: - # Create a map of all numbers in the array to their index - nmap = defaultdict(int) - for i in range(len(nums)): - if nums[i] not in nmap: - nmap[nums[i]] = i - - # Create a seen array to keep track of whether a number has been seen before - seen, ans = [0] * len(nums), 0 - - # Iterate through each number in the array - for n in nums: - # Set curr to the current number, and count to 1 - curr, count = n, 1 - - # If we've already seen this number, skip it - if seen[nmap[n]]: - continue - - # Otherwise, iterate through all consecutive numbers after curr - while curr + 1 in nmap: - # Increment curr - curr += 1 - - # Check if we've seen this number before - ix = nmap[curr] - if seen[ix]: - # If we have, add it to the count and break out of the loop - count += seen[ix] - break - else: - # Otherwise, add it to the seen array and increment the count - seen[ix] = 1 - count += 1 - - # Add the count to the seen array and update the answer - seen[nmap[n]], ans = count, max(ans, count) - - # Return the answer - return ans +def longestConsecutiveSequence(self, nums: List[int]) -> int: + # Create a map of all numbers in the array to their index + nmap = defaultdict(int) + for i in range(len(nums)): + if nums[i] not in nmap: + nmap[nums[i]] = i + # Create a seen array to keep track of whether a number has been seen before + seen, ans = [0] * len(nums), 0 + # Iterate through each number in the array + for n in nums: + # Set curr to the current number, and count to 1 + curr, count = n, 1 + # If we've already seen this number, skip it + if seen[nmap[n]]: + continue + # Otherwise, iterate through all consecutive numbers after curr + while curr + 1 in nmap: + # Increment curr + curr += 1 + # Check if we've seen this number before + ix = nmap[curr] + if seen[ix]: + # If we have, add it to the count and break out of the loop + count += seen[ix] + break + else: + # Otherwise, add it to the seen array and increment the count + seen[ix] = 1 + count += 1 + # Add the count to the seen array and update the answer + seen[nmap[n]], ans = count, max(ans, count) + # Return the answer + return ans """ From 74840186b6aa7dfcf268b0ada2306747a67634f4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 08:22:01 +0000 Subject: [PATCH 06/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/longest_consecutive_sequence.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index 497962e77bcc..ea84a0769955 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -5,6 +5,7 @@ """ + def longestConsecutiveSequence(self, nums: List[int]) -> int: # Create a map of all numbers in the array to their index nmap = defaultdict(int) From 0a5eb1b87b857b1d04a4994fce6939218313baeb Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Thu, 5 Oct 2023 14:01:13 +0530 Subject: [PATCH 07/19] converted function name from CamelCase to snake_case --- data_structures/arrays/longest_consecutive_sequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index ea84a0769955..080d64fcd1f7 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -6,7 +6,7 @@ """ -def longestConsecutiveSequence(self, nums: List[int]) -> int: +def longest_consecutive_sequence(self, nums: List[int]) -> int: # Create a map of all numbers in the array to their index nmap = defaultdict(int) for i in range(len(nums)): From 92c70f5391ffb4b2fb60ce9fc58fb07ff22f6695 Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Thu, 5 Oct 2023 14:30:44 +0530 Subject: [PATCH 08/19] added the docktest for the solution --- .../arrays/longest_consecutive_sequence.py | 93 ++++++++++--------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index 080d64fcd1f7..45ea5b7628aa 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -4,54 +4,55 @@ Longest Consecutive Sequence Problem from LeetCode """ +//can u add +def longest_consecutive_sequence(nums): + + """ + Finds the length of the longest consecutive sequence in a list of numbers. + Args: + nums (List[int]): A list of integers. -def longest_consecutive_sequence(self, nums: List[int]) -> int: - # Create a map of all numbers in the array to their index - nmap = defaultdict(int) - for i in range(len(nums)): - if nums[i] not in nmap: - nmap[nums[i]] = i - # Create a seen array to keep track of whether a number has been seen before - seen, ans = [0] * len(nums), 0 - # Iterate through each number in the array - for n in nums: - # Set curr to the current number, and count to 1 - curr, count = n, 1 - # If we've already seen this number, skip it - if seen[nmap[n]]: - continue - # Otherwise, iterate through all consecutive numbers after curr - while curr + 1 in nmap: - # Increment curr - curr += 1 - # Check if we've seen this number before - ix = nmap[curr] - if seen[ix]: - # If we have, add it to the count and break out of the loop - count += seen[ix] - break - else: - # Otherwise, add it to the seen array and increment the count - seen[ix] = 1 - count += 1 - # Add the count to the seen array and update the answer - seen[nmap[n]], ans = count, max(ans, count) - # Return the answer - return ans + 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 + """ + + if not nums: + return 0 -""" -Idea -1. First, we put all the numbers into a dictionary, and note the index of each number. - This is to make sure the lookup time when we check if a number is in the list is O(1). - We also initialize a seen list for later use. - -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. - Otherwise, we set the seen status of n to 1, and break the loop. - This is because if n+1 is not in the dictionary, then the consecutive sequence starting from n is over. - And we don't need to count the length of the consecutive sequence starting from n+1, since we will eventually count it when we reach n+1. + # Create a set of all the numbers in the list + num_set = set(nums) + longest_sequence = 0 -""" + # Iterate over the set of numbers + for num in num_set: + # If the number that is one less than the current number is not in the set, then this is the beginning of a sequence + if num - 1 not in num_set: # Start of a potential sequence + # Store the current number in a variable and initiate a sequence length counter + current_num = num + current_sequence = 1 + + # While the next number is in the set, increment the current number and the sequence counter + while current_num + 1 in num_set: + current_num += 1 + current_sequence += 1 + + # Update the longest sequence if the current sequence is longer + longest_sequence = max(longest_sequence, current_sequence) + + return longest_sequence + +''' +This code takes in a list of numbers and returns the length of the longest sequence of consecutive numbers in the list. +For example, if the list is [1, 3, 2, 4, 5, 6, 7], the function will return 5, since the longest sequence of consecutive numbers is [3, 4, 5, 6, 7]. +''' From 4db58418153b021eabc27e44cb4d7719b0a4c727 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 09:01:37 +0000 Subject: [PATCH 09/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/longest_consecutive_sequence.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index 45ea5b7628aa..77c98229bd89 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -4,9 +4,9 @@ Longest Consecutive Sequence Problem from LeetCode """ -//can u add +//can u add def longest_consecutive_sequence(nums): - + """ Finds the length of the longest consecutive sequence in a list of numbers. @@ -26,7 +26,7 @@ def longest_consecutive_sequence(nums): >>> longest_consecutive_sequence([]) 0 """ - + if not nums: return 0 From ca07301966cf9a058b05fb71d45948bae259ba17 Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Thu, 5 Oct 2023 14:36:57 +0530 Subject: [PATCH 10/19] removed an accidental error --- data_structures/arrays/longest_consecutive_sequence.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index 77c98229bd89..bba5657fab86 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -4,9 +4,8 @@ Longest Consecutive Sequence Problem from LeetCode """ -//can u add def longest_consecutive_sequence(nums): - + """ Finds the length of the longest consecutive sequence in a list of numbers. @@ -26,7 +25,7 @@ def longest_consecutive_sequence(nums): >>> longest_consecutive_sequence([]) 0 """ - + if not nums: return 0 @@ -56,3 +55,4 @@ def longest_consecutive_sequence(nums): This code takes in a list of numbers and returns the length of the longest sequence of consecutive numbers in the list. For example, if the list is [1, 3, 2, 4, 5, 6, 7], the function will return 5, since the longest sequence of consecutive numbers is [3, 4, 5, 6, 7]. ''' +''' From 708966e34928c1d6c4e417b27574174e09cdf8be Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 09:08:14 +0000 Subject: [PATCH 11/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/longest_consecutive_sequence.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index bba5657fab86..953cd7c04f2c 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -5,7 +5,7 @@ """ def longest_consecutive_sequence(nums): - + """ Finds the length of the longest consecutive sequence in a list of numbers. @@ -25,7 +25,7 @@ def longest_consecutive_sequence(nums): >>> longest_consecutive_sequence([]) 0 """ - + if not nums: return 0 From d10a45f187c138ae4dd8e4f0537c7b77deb66d45 Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Thu, 5 Oct 2023 14:43:52 +0530 Subject: [PATCH 12/19] Solved some linter errors --- .../arrays/longest_consecutive_sequence.py | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index 953cd7c04f2c..2ebfc7b664fe 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -1,11 +1,10 @@ """ -author: Sarthak Sharma https://github.com/Sarthak950 https://sarthak950.netlify.app +author: Sarthak Sharma https://github.com/Sarthak950 +Website: https://sarthak950.netlify.app date: 4 OCT 2023 Longest Consecutive Sequence Problem from LeetCode - """ def longest_consecutive_sequence(nums): - """ Finds the length of the longest consecutive sequence in a list of numbers. @@ -25,7 +24,6 @@ def longest_consecutive_sequence(nums): >>> longest_consecutive_sequence([]) 0 """ - if not nums: return 0 @@ -35,13 +33,16 @@ def longest_consecutive_sequence(nums): # Iterate over the set of numbers for num in num_set: - # If the number that is one less than the current number is not in the set, then this is the beginning of a sequence + # If the number that is one less than the current number is not in the set, + # then this is the beginning of a sequence if num - 1 not in num_set: # Start of a potential sequence - # Store the current number in a variable and initiate a sequence length counter + # Store the current number in a variable + # and initiate a sequence length counter current_num = num current_sequence = 1 - # While the next number is in the set, increment the current number and the sequence counter + # While the next number is in the set, + # increment the current number and the sequence counter while current_num + 1 in num_set: current_num += 1 current_sequence += 1 @@ -50,9 +51,9 @@ def longest_consecutive_sequence(nums): longest_sequence = max(longest_sequence, current_sequence) return longest_sequence - -''' -This code takes in a list of numbers and returns the length of the longest sequence of consecutive numbers in the list. -For example, if the list is [1, 3, 2, 4, 5, 6, 7], the function will return 5, since the longest sequence of consecutive numbers is [3, 4, 5, 6, 7]. -''' ''' +This code takes in a list of numbers and returns the length of +the longest sequence of consecutive numbers in the list. +For example, if the list is [1, 3, 2, 4, 5, 6, 7], the function will return 5, +since the longest sequence of consecutive numbers is [3, 4, 5, 6, 7]. +''' \ No newline at end of file From 9d2e88e91f61190242888396860d3d9b81a18ece Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 09:14:53 +0000 Subject: [PATCH 13/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../arrays/longest_consecutive_sequence.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index 2ebfc7b664fe..74a1bc709c9a 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -1,9 +1,11 @@ """ -author: Sarthak Sharma https://github.com/Sarthak950 +author: Sarthak Sharma https://github.com/Sarthak950 Website: https://sarthak950.netlify.app date: 4 OCT 2023 Longest Consecutive Sequence Problem from LeetCode """ + + def longest_consecutive_sequence(nums): """ Finds the length of the longest consecutive sequence in a list of numbers. @@ -36,12 +38,12 @@ def longest_consecutive_sequence(nums): # If the number that is one less than the current number is not in the set, # then this is the beginning of a sequence if num - 1 not in num_set: # Start of a potential sequence - # Store the current number in a variable + # Store the current number in a variable # and initiate a sequence length counter current_num = num current_sequence = 1 - # While the next number is in the set, + # While the next number is in the set, # increment the current number and the sequence counter while current_num + 1 in num_set: current_num += 1 @@ -51,9 +53,11 @@ def longest_consecutive_sequence(nums): longest_sequence = max(longest_sequence, current_sequence) return longest_sequence -''' -This code takes in a list of numbers and returns the length of + + +""" +This code takes in a list of numbers and returns the length of the longest sequence of consecutive numbers in the list. -For example, if the list is [1, 3, 2, 4, 5, 6, 7], the function will return 5, +For example, if the list is [1, 3, 2, 4, 5, 6, 7], the function will return 5, since the longest sequence of consecutive numbers is [3, 4, 5, 6, 7]. -''' \ No newline at end of file +""" From 56fbcbeccfb03c3b6772babf162cc4d95f9e3eb1 Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Thu, 5 Oct 2023 15:01:59 +0530 Subject: [PATCH 14/19] added the return type --- data_structures/arrays/longest_consecutive_sequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index 74a1bc709c9a..70b794867003 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -6,7 +6,7 @@ """ -def longest_consecutive_sequence(nums): +def longest_consecutive_sequence(nums) -> int: """ Finds the length of the longest consecutive sequence in a list of numbers. From 37ec51afd95ca4e513f228840068dd2aa99ff969 Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Thu, 5 Oct 2023 15:05:20 +0530 Subject: [PATCH 15/19] argument hint --- data_structures/arrays/longest_consecutive_sequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index 70b794867003..b552c3b984a3 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -6,7 +6,7 @@ """ -def longest_consecutive_sequence(nums) -> int: +def longest_consecutive_sequence(nums: List[int]) -> int: """ Finds the length of the longest consecutive sequence in a list of numbers. From ca3192f095a54c5dd6f6e0b865c0c50f38aded53 Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Thu, 5 Oct 2023 15:07:02 +0530 Subject: [PATCH 16/19] List - list --- data_structures/arrays/longest_consecutive_sequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index b552c3b984a3..dc37e328bd1c 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -6,7 +6,7 @@ """ -def longest_consecutive_sequence(nums: List[int]) -> int: +def longest_consecutive_sequence(nums:list[int]) -> int: """ Finds the length of the longest consecutive sequence in a list of numbers. From fae996ca8de4660927659da9ca5e4cea9f9d5622 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 09:37:43 +0000 Subject: [PATCH 17/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/longest_consecutive_sequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index dc37e328bd1c..c49b6c7c19eb 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -6,7 +6,7 @@ """ -def longest_consecutive_sequence(nums:list[int]) -> int: +def longest_consecutive_sequence(nums: list[int]) -> int: """ Finds the length of the longest consecutive sequence in a list of numbers. From afaa185f4bd57879dbc8244ee43a7147a4644543 Mon Sep 17 00:00:00 2001 From: Sarthak950 <panditsamsar950@gmail.com> Date: Fri, 6 Oct 2023 01:45:25 +0530 Subject: [PATCH 18/19] fixed the indentation in the doctests string --- .../arrays/longest_consecutive_sequence.py | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index c49b6c7c19eb..c6a7e0aef10a 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -6,25 +6,19 @@ """ -def longest_consecutive_sequence(nums: list[int]) -> int: +def longest_consecutive_sequence(nums:list[int]) -> int: """ 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 + >>> 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 """ if not nums: return 0 From f5fa31264cdad17080643d23782793eddf75ddde Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 01:46:34 +0000 Subject: [PATCH 19/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/longest_consecutive_sequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/longest_consecutive_sequence.py b/data_structures/arrays/longest_consecutive_sequence.py index c6a7e0aef10a..7a856ef123a4 100644 --- a/data_structures/arrays/longest_consecutive_sequence.py +++ b/data_structures/arrays/longest_consecutive_sequence.py @@ -6,7 +6,7 @@ """ -def longest_consecutive_sequence(nums:list[int]) -> int: +def longest_consecutive_sequence(nums: list[int]) -> int: """ Finds the length of the longest consecutive sequence in a list of numbers.