From 414e84b65e2dcb9e147aabe0432e9cb2c6296e4c Mon Sep 17 00:00:00 2001 From: jonat Date: Thu, 13 Jul 2023 09:58:11 +0300 Subject: [PATCH 1/3] Move firstMissingPositive to new format --- ProblemsByTags.json | 4 +++ README.md | 2 +- Solutions/F/FirstMissingPositive.py | 55 +++++++++++++++++++++++++++++ Solutions/F/__init__.py | 0 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 Solutions/F/FirstMissingPositive.py create mode 100644 Solutions/F/__init__.py diff --git a/ProblemsByTags.json b/ProblemsByTags.json index c3263bc..4893f22 100644 --- a/ProblemsByTags.json +++ b/ProblemsByTags.json @@ -7,6 +7,9 @@ "ThreeSum", "TopKFrequentNums" ], + "Extreme": [ + "FirstMissingPositive" + ], "Hard": [ "Codec", "LongestPalindrome", @@ -49,6 +52,7 @@ "ThreeSum" ], "Sorting": [ + "FirstMissingPositive", "SortColors" ], "Strings": [ diff --git a/README.md b/README.md index af95ba9..744ab6d 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Please make sure to replace everything need to be replaced. Add a few test cases, make sure they're running smoothly. -Run `.\build.ps1`. +Run `python ProblemsByTags.py`. If everything is OK, push it and open a PR. ## Troubleshooting diff --git a/Solutions/F/FirstMissingPositive.py b/Solutions/F/FirstMissingPositive.py new file mode 100644 index 0000000..da68697 --- /dev/null +++ b/Solutions/F/FirstMissingPositive.py @@ -0,0 +1,55 @@ +import unittest +from Tags import * + +""" +Author: Jonathan Joseph +Problem Description/link: replace with relevant link or problem description +""" + + +class Solution(object): + def getTags(self): + tags = [Difficulty.Extreme, Topic.Sorting] + return tags + + def firstMissingPositive(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + + if not nums: + return 1 + + ptr = 0 + junk = len(nums) + + while ptr < junk: + if nums[ptr] == ptr + 1: + ptr += 1 + continue + + if nums[ptr] <= 0 or nums[ptr] >= junk or nums[ptr] == nums[nums[ptr] - 1]: + junk -= 1 + nums[ptr], nums[junk] = nums[junk], nums[ptr] + continue + + temp = nums[nums[ptr] - 1] + nums[nums[ptr] - 1] = nums[ptr] + nums[ptr] = temp + + for i in range(len(nums)): + if nums[i] != i + 1: + return i + 1 + + return len(nums) + 1 + + +class test_firstMissingPositive(unittest.TestCase): + def test_1(self): + self.assertEqual(2, Solution().firstMissingPositive([3, 3, 1, 4, 0])) + self.assertEqual(1, Solution().firstMissingPositive([3, -7, 2, 4, 0])) + self.assertEqual(6, Solution().firstMissingPositive([1, 2, 3, 5, 4])) + self.assertEqual(1, Solution().firstMissingPositive([-7])) + self.assertEqual(1, Solution().firstMissingPositive([])) + self.assertEqual(2, Solution().firstMissingPositive([1, 1, 1, 1, 1, 1, 1, 1])) diff --git a/Solutions/F/__init__.py b/Solutions/F/__init__.py new file mode 100644 index 0000000..e69de29 From ed4d74bc60c81aaa7f233bc53efb9398bfc1e465 Mon Sep 17 00:00:00 2001 From: jonat Date: Fri, 5 Jan 2024 10:34:49 +0200 Subject: [PATCH 2/3] tests + ZigZag --- .github/workflows/build-and-test.yml | 26 +++++++++++++++ Solutions/Z/ZigzagConversion.py | 50 ++++++++++++++++++++++++++++ Solutions/Z/__init__.py | 0 3 files changed, 76 insertions(+) create mode 100644 .github/workflows/build-and-test.yml create mode 100644 Solutions/Z/ZigzagConversion.py create mode 100644 Solutions/Z/__init__.py diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml new file mode 100644 index 0000000..f31e3f5 --- /dev/null +++ b/.github/workflows/build-and-test.yml @@ -0,0 +1,26 @@ +name: Python Tests + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + python-version: [3.8, 3.9] + + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Run tests + run: | + python -m unittest discover -s ./Tests diff --git a/Solutions/Z/ZigzagConversion.py b/Solutions/Z/ZigzagConversion.py new file mode 100644 index 0000000..6fce30d --- /dev/null +++ b/Solutions/Z/ZigzagConversion.py @@ -0,0 +1,50 @@ +import unittest +from Tags import * +""" +Author: Jonathan Joseph +Problem Description/link: replace with relevant link or problem description +""" + +class Solution(object): + def getTags(self): + tags = [Difficulty.Hard, Topic.Strings] + return tags + + def convert(self, s, numRows): + """ + :type s: str + :type numRows: int + :rtype: str + """ + if numRows == 1: + return s + + res = '' + delta = (2*(numRows-1)) + + for r in range(numRows): + if r == 0 or r == numRows - 1: + i = r + while i < len(s): + res += s[i] + i += delta + + else: + indices = [r, delta-r] + i = 0 + while indices[i] < len(s): + res += s[indices[i]] + indices[i] += delta + i = ((i+1) % len(indices)) + + return res + + + +class test_zigzagConversion(unittest.TestCase): + def test_1(self): + self.assertEqual(Solution().convert("PAYPALISHIRING", 3), "PAHNAPLSIIGYIR") + self.assertEqual(Solution().convert("PAYPALISHIRING", 4), "PINALSIGYAHRPI") + self.assertEqual(Solution().convert("PAYPALISHIRING", 1), "PAYPALISHIRING") + self.assertEqual(Solution().convert("ABCD", 3), "ABDC") + self.assertEqual(Solution().convert("a", 7), "a") diff --git a/Solutions/Z/__init__.py b/Solutions/Z/__init__.py new file mode 100644 index 0000000..e69de29 From dddb88c7d59481e94eb52a56f84c0b2608a91af6 Mon Sep 17 00:00:00 2001 From: jonat Date: Fri, 5 Jan 2024 10:37:01 +0200 Subject: [PATCH 3/3] yml fix --- .github/workflows/build-and-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index f31e3f5..3e7f117 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -23,4 +23,4 @@ jobs: python-version: ${{ matrix.python-version }} - name: Run tests run: | - python -m unittest discover -s ./Tests + python -m unittest discover