From 68211cac0e1cebbf936a97a3f4c6991b2a047017 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:39:46 +0000 Subject: [PATCH 01/13] Adds Longest Arithmetic Subsequence --- .../longest_arithmetic_subsequence.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 dynamic_programming/longest_arithmetic_subsequence.py diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py new file mode 100644 index 000000000000..f82aa886d210 --- /dev/null +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -0,0 +1,66 @@ +from typing import List +""" +Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length of the longest arithmetic subsequence in nums. + +Note that: +- A subsequence is an array that can be derived from another array by deleting some or no elements + without changing the order of the remaining elements. +- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1). +""" + +def longest_arithmetic_subsequence(nums: List[int]) -> int: + """ + Finds the length of the longest arithmetic subsequence in a given array of integers. + + Parameters + ---------- + nums : List[int] + The input array of integers. + + Returns + ------- + int + The length of the longest arithmetic subsequence. + + Examples + -------- + >>> longest_arithmetic_subsequence([3, 6, 9, 12]) + 4 + >>> longest_arithmetic_subsequence([9, 4, 7, 2, 10]) + 3 + >>> longest_arithmetic_subsequence([20, 1, 15, 3, 10, 5, 8]) + 4 + >>> longest_arithmetic_subsequence([]) # Empty array + 0 + >>> longest_arithmetic_subsequence(None) # Null array + Traceback (most recent call last): + ... + ValueError: Input array cannot be None + """ + if nums is None: + raise ValueError("Input array cannot be None") + + if len(nums) <= 1: + return len(nums) + + dp = [{} for _ in range(len(nums))] + max_length = 2 + + for i in range(len(nums)): + for j in range(i): + diff = nums[i] - nums[j] + dp[i][diff] = dp[j].get(diff, 1) + 1 + max_length = max(max_length, dp[i][diff]) + + return max_length + + +if __name__ == "__main__": + import doctest + doctest.testmod() + # sample test case + nums = [3, 6, 9, 12] + expected_length = 4 + + result = longest_arithmetic_subsequence(nums) + print("Length of longest arithmetic subsequence:", result) From 44d659123601a09f5973e48d999f3e74c5609970 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:43:22 +0000 Subject: [PATCH 02/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_arithmetic_subsequence.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index f82aa886d210..31d6572abe70 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -1,4 +1,5 @@ from typing import List + """ Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length of the longest arithmetic subsequence in nums. @@ -8,6 +9,7 @@ - A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1). """ + def longest_arithmetic_subsequence(nums: List[int]) -> int: """ Finds the length of the longest arithmetic subsequence in a given array of integers. @@ -39,7 +41,7 @@ def longest_arithmetic_subsequence(nums: List[int]) -> int: """ if nums is None: raise ValueError("Input array cannot be None") - + if len(nums) <= 1: return len(nums) @@ -57,6 +59,7 @@ def longest_arithmetic_subsequence(nums: List[int]) -> int: if __name__ == "__main__": import doctest + doctest.testmod() # sample test case nums = [3, 6, 9, 12] From 92d21729a29867f6227a46891cfbbfd0ddce9b28 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:47:00 +0000 Subject: [PATCH 03/13] ruff check fixes --- .../longest_arithmetic_subsequence.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index f82aa886d210..4a19af5043bd 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -1,4 +1,3 @@ -from typing import List """ Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length of the longest arithmetic subsequence in nums. @@ -8,13 +7,13 @@ - A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1). """ -def longest_arithmetic_subsequence(nums: List[int]) -> int: +def longest_arithmetic_subsequence(nums: list[int]) -> int: """ Finds the length of the longest arithmetic subsequence in a given array of integers. Parameters ---------- - nums : List[int] + nums : list[int] The input array of integers. Returns @@ -40,8 +39,11 @@ def longest_arithmetic_subsequence(nums: List[int]) -> int: if nums is None: raise ValueError("Input array cannot be None") - if len(nums) <= 1: - return len(nums) + if len(nums) == 0: + return 0 + + if len(nums) == 1: + return 1 dp = [{} for _ in range(len(nums))] max_length = 2 From fa1633bfe61fd048cdff5da86be3f72aaf5e2bbb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:54:17 +0000 Subject: [PATCH 04/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_arithmetic_subsequence.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index 4a19af5043bd..bfe24fc4a0ca 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -7,6 +7,7 @@ - A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1). """ + def longest_arithmetic_subsequence(nums: list[int]) -> int: """ Finds the length of the longest arithmetic subsequence in a given array of integers. @@ -38,7 +39,7 @@ def longest_arithmetic_subsequence(nums: list[int]) -> int: """ if nums is None: raise ValueError("Input array cannot be None") - + if len(nums) == 0: return 0 @@ -59,6 +60,7 @@ def longest_arithmetic_subsequence(nums: list[int]) -> int: if __name__ == "__main__": import doctest + doctest.testmod() # sample test case nums = [3, 6, 9, 12] From 427b1ca6855ffff0f07a1d320a54b8e7b513a654 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:56:50 +0000 Subject: [PATCH 05/13] ruff check fixes --- .../longest_arithmetic_subsequence.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index 4a19af5043bd..74cb11b0b2bb 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -1,10 +1,12 @@ """ -Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length of the longest arithmetic subsequence in nums. +Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length +of the longest arithmetic subsequence in nums. Note that: -- A subsequence is an array that can be derived from another array by deleting some or no elements - without changing the order of the remaining elements. -- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1). +- A subsequence is an array that can be derived from another array by deleting some or no + elements without changing the order of the remaining elements. +- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < + seq.length - 1). """ def longest_arithmetic_subsequence(nums: list[int]) -> int: @@ -60,7 +62,7 @@ def longest_arithmetic_subsequence(nums: list[int]) -> int: if __name__ == "__main__": import doctest doctest.testmod() - # sample test case + # Sample test case nums = [3, 6, 9, 12] expected_length = 4 From bdbe42ae229490de07ee7f912c25fb028cea2914 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:58:21 +0000 Subject: [PATCH 06/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_arithmetic_subsequence.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index 781af9ef6a97..c1c54bda1728 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -1,11 +1,11 @@ """ -Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length +Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length of the longest arithmetic subsequence in nums. Note that: -- A subsequence is an array that can be derived from another array by deleting some or no +- A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. -- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < +- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1). """ From 3198708845c232f2d872642ac8f889e5cb5ac617 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:02:25 +0000 Subject: [PATCH 07/13] ruff fixes --- dynamic_programming/longest_arithmetic_subsequence.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index 781af9ef6a97..c1c54bda1728 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -1,11 +1,11 @@ """ -Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length +Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length of the longest arithmetic subsequence in nums. Note that: -- A subsequence is an array that can be derived from another array by deleting some or no +- A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. -- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < +- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1). """ From 506161a4cf3f878bcc7fb9a5b42af0e5d917af6a Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:06:25 +0000 Subject: [PATCH 08/13] line length shortened --- .../longest_arithmetic_subsequence.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index c1c54bda1728..dfa061f18ecc 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -1,12 +1,12 @@ """ -Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length -of the longest arithmetic subsequence in nums. +Longest Arithmetic Subsequence Problem: Given an array nums of integers, +return the length of the longest arithmetic subsequence in nums. Note that: -- A subsequence is an array that can be derived from another array by deleting some or no - elements without changing the order of the remaining elements. -- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < - seq.length - 1). +- A subsequence is an array that can be derived from another array by +deleting some or no elements without changing the order of the remaining elements. +- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value +(for 0 <= i < seq.length - 1). """ From 4bd4d1cff6239939880e769df9132bbb1cd76053 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:07:35 +0000 Subject: [PATCH 09/13] removed trailing spaces --- dynamic_programming/longest_arithmetic_subsequence.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index dfa061f18ecc..6b6987103aed 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -1,11 +1,11 @@ """ -Longest Arithmetic Subsequence Problem: Given an array nums of integers, +Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length of the longest arithmetic subsequence in nums. Note that: -- A subsequence is an array that can be derived from another array by +- A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. -- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value +- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1). """ From 2883a97b31bf31492b013fde9cc04a65a2a8688d Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:11:48 +0000 Subject: [PATCH 10/13] dp type specified --- dynamic_programming/longest_arithmetic_subsequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index 6b6987103aed..f7d0160bf57e 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -48,7 +48,7 @@ def longest_arithmetic_subsequence(nums: list[int]) -> int: if len(nums) == 1: return 1 - dp = [{} for _ in range(len(nums))] + ddp: list[dict[int, int]] = [{} for _ in range(len(nums))] max_length = 2 for i in range(len(nums)): From 3f9bc29569d69af9bd7457056e1c816be855db29 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:12:53 +0000 Subject: [PATCH 11/13] fixes --- dynamic_programming/longest_arithmetic_subsequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index f7d0160bf57e..f09d1abddb50 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -48,7 +48,7 @@ def longest_arithmetic_subsequence(nums: list[int]) -> int: if len(nums) == 1: return 1 - ddp: list[dict[int, int]] = [{} for _ in range(len(nums))] + dp: list[dict[int, int]] = [{} for _ in range(len(nums))] max_length = 2 for i in range(len(nums)): From c30da6502b927ab6a4dd45bfc8c679e157a8bcf9 Mon Sep 17 00:00:00 2001 From: Tejaswi Tyagi <98461855+tejaswi0910@users.noreply.github.com> Date: Fri, 4 Oct 2024 15:13:09 +0000 Subject: [PATCH 12/13] Added testcases --- .../longest_arithmetic_subsequence.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index f09d1abddb50..785f5d455e90 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -64,9 +64,21 @@ def longest_arithmetic_subsequence(nums: list[int]) -> int: import doctest doctest.testmod() - # Sample test case - nums = [3, 6, 9, 12] - expected_length = 4 - result = longest_arithmetic_subsequence(nums) - print("Length of longest arithmetic subsequence:", result) + # test cases + test_cases = [ + ([3, 6, 9, 12], 4), + ([3, 6, 9, 12, 15], 5), + ([1, 7, 10, 13, 14, 19], 4), + ([1, 2, 3, 4], 4), + ([], 0), + ([10], 1), + ([9, 4, 7, 2, 10], 3), + ([1, 2, 2, 2, 2, 5], 4), + ] + + for nums, expected_length in test_cases: + result = longest_arithmetic_subsequence(nums) + print(f"Test case {nums}: Expected {expected_length}, Got {result}") + assert result == expected_length, f"Test failed for input {nums}" + From 27f1e89792a7a54dfd3cb482801c2e3c44c34b57 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Oct 2024 15:13:47 +0000 Subject: [PATCH 13/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_arithmetic_subsequence.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dynamic_programming/longest_arithmetic_subsequence.py b/dynamic_programming/longest_arithmetic_subsequence.py index 785f5d455e90..9dd28d850801 100644 --- a/dynamic_programming/longest_arithmetic_subsequence.py +++ b/dynamic_programming/longest_arithmetic_subsequence.py @@ -67,7 +67,7 @@ def longest_arithmetic_subsequence(nums: list[int]) -> int: # test cases test_cases = [ - ([3, 6, 9, 12], 4), + ([3, 6, 9, 12], 4), ([3, 6, 9, 12, 15], 5), ([1, 7, 10, 13, 14, 19], 4), ([1, 2, 3, 4], 4), @@ -81,4 +81,3 @@ def longest_arithmetic_subsequence(nums: list[int]) -> int: result = longest_arithmetic_subsequence(nums) print(f"Test case {nums}: Expected {expected_length}, Got {result}") assert result == expected_length, f"Test failed for input {nums}" -