Skip to content

Commit 93be74c

Browse files
Add Black formatting and pre-commit hooks to repository
Introduce Black code formatter for consistent styling across the codebase. Add GitHub Actions workflow for format validation and a pre-commit configuration for local enforcement. Update all Python files to adhere to Black formatting standards.
1 parent a02255f commit 93be74c

34 files changed

+203
-96
lines changed

.github/workflows/format-check.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Format Check
2+
on: [push, pull_request]
3+
jobs:
4+
format-check:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v3
8+
- name: Set up Python
9+
uses: actions/setup-python@v3
10+
with:
11+
python-version: 3.9
12+
- name: Install Black
13+
run: pip install black
14+
- name: Run Black
15+
run: black --check .

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 23.9.1 # Use the latest stable version
4+
hooks:
5+
- id: black

Arrays & Hashing/contains_duplicate.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
import unittest
44
from typing import List
55

6+
67
def has_duplicate(nums: List[int]) -> bool:
7-
seen = set()
8+
seen = set()
9+
10+
for num in nums:
11+
if num in seen:
12+
return True
813

9-
for num in nums:
10-
if num in seen:
11-
return True
14+
seen.add(num)
1215

13-
seen.add(num)
16+
return False
1417

15-
return False
1618

1719
class Test(unittest.TestCase):
1820
def test_has_duplicate(self):

Arrays & Hashing/encode_and_decode_strings.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import unittest
44
from typing import List
55

6+
67
def encode(words: List[str]) -> str:
78
encoded_words = []
89

910
for word in words:
1011
encoded_word = str(len(word)) + "#" + word
1112
encoded_words.append(encoded_word)
1213

13-
return ' '.join(encoded_words)
14+
return " ".join(encoded_words)
15+
1416

1517
def decode(encoded_words: str) -> List[str]:
1618
n = len(encoded_words)
@@ -20,7 +22,7 @@ def decode(encoded_words: str) -> List[str]:
2022
while i < n:
2123
j = i
2224
# Find the position of the next '#'
23-
while j < n and encoded_words[j] != '#':
25+
while j < n and encoded_words[j] != "#":
2426
j += 1
2527

2628
# If no '#' is found, break the loop to avoid infinite loop
@@ -39,7 +41,9 @@ def decode(encoded_words: str) -> List[str]:
3941

4042
# Check if the end of the word is within bounds
4143
if j > n:
42-
raise ValueError(f"Word length {length} is out of bounds for the string: {encoded_words[i:]}")
44+
raise ValueError(
45+
f"Word length {length} is out of bounds for the string: {encoded_words[i:]}"
46+
)
4347

4448
decoded_word = encoded_words[i:j]
4549
decoded_words.append(decoded_word)
@@ -49,16 +53,16 @@ def decode(encoded_words: str) -> List[str]:
4953

5054
return decoded_words
5155

56+
5257
class Test(unittest.TestCase):
5358
def setUp(self):
54-
self.example1 = ["neet","code","love","you"]
55-
self.example2 = ["we","say",":","yes"]
59+
self.example1 = ["neet", "code", "love", "you"]
60+
self.example2 = ["we", "say", ":", "yes"]
5661

57-
self.expected_encoding1 = '4#neet 4#code 4#love 3#you'
58-
self.expected_encoding2 = '2#we 3#say 1#: 3#yes'
62+
self.expected_encoding1 = "4#neet 4#code 4#love 3#you"
63+
self.expected_encoding2 = "2#we 3#say 1#: 3#yes"
5964

6065
def test_encode(self):
61-
6266
self.assertEqual(encode(self.example1), self.expected_encoding1)
6367
self.assertEqual(encode(self.example2), self.expected_encoding2)
6468

Arrays & Hashing/group_anagrams.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
from typing import List
55
from collections import defaultdict
66

7+
78
def group_anagrams(words: List[str]) -> List[List[str]]:
89
anagram_groups = defaultdict(list)
910

1011
for word in words:
11-
sorted_word = ''.join(sorted(word))
12+
sorted_word = "".join(sorted(word))
1213

1314
if sorted_word in anagram_groups:
1415
anagram_groups[sorted_word].append(word)
@@ -18,6 +19,7 @@ def group_anagrams(words: List[str]) -> List[List[str]]:
1819

1920
return [group for group in anagram_groups.values()]
2021

22+
2123
class Test(unittest.TestCase):
2224
def assertEqualAnagramGroups(self, result, expected):
2325
# Sort each group and the outer list
@@ -30,7 +32,7 @@ def assertEqualAnagramGroups(self, result, expected):
3032
def test_group_anagrams(self):
3133
self.assertEqualAnagramGroups(
3234
group_anagrams(["act", "pots", "tops", "cat", "stop", "hat"]),
33-
[["hat"], ["act", "cat"], ["stop", "pots", "tops"]]
35+
[["hat"], ["act", "cat"], ["stop", "pots", "tops"]],
3436
)
3537
self.assertEqualAnagramGroups(group_anagrams(["x"]), [["x"]])
3638
self.assertEqualAnagramGroups(group_anagrams([""]), [[""]])

Arrays & Hashing/longest_consecutive_sequence.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import unittest
44
from typing import List
55

6+
67
def longest_consecutive(nums: List[int]) -> int:
78
nums_set = set(nums)
89
longest = 0
910

1011
for num in nums_set:
11-
if (num - 1) not in nums_set: # Are we are the start of a sequence?
12+
if (num - 1) not in nums_set: # Are we are the start of a sequence?
1213
length = 1
1314

1415
while (num + length) in nums_set:
@@ -18,6 +19,7 @@ def longest_consecutive(nums: List[int]) -> int:
1819

1920
return longest
2021

22+
2123
class Test(unittest.TestCase):
2224
def test_longest_consecutive(self):
2325
self.assertEqual(longest_consecutive([2, 20, 4, 10, 3, 4, 5]), 4)

Arrays & Hashing/product_of_array_except_self.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from typing import List
55

6+
67
def product_except_self(nums: List[int]) -> List[int]:
78
n = len(nums)
89

@@ -20,6 +21,7 @@ def product_except_self(nums: List[int]) -> List[int]:
2021

2122
return result
2223

24+
2325
class Test(unittest.TestCase):
2426
def test_product_except_self(self):
2527
self.assertEqual(product_except_self([1, 2, 4, 6]), [48, 24, 12, 8])

Arrays & Hashing/top_k_frequent_elements.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# # Use heapq.nlargest to get the k elements with the highest counts
1515
# return heapq.nlargest(k, counts.keys(), key=counts.get)
1616

17+
1718
def top_k_frequent(nums: List[int], k: int) -> List[int]:
1819
n = len(nums)
1920

@@ -25,7 +26,9 @@ def top_k_frequent(nums: List[int], k: int) -> List[int]:
2526
min_heap = []
2627

2728
for num, count in counts.items():
28-
heapq.heappush(min_heap, (-count, num)) # negate the count to convert min heap to max heap
29+
heapq.heappush(
30+
min_heap, (-count, num)
31+
) # negate the count to convert min heap to max heap
2932

3033
result = []
3134

@@ -35,6 +38,7 @@ def top_k_frequent(nums: List[int], k: int) -> List[int]:
3538

3639
return result
3740

41+
3842
class Test(unittest.TestCase):
3943
def test_top_k_frequent(self):
4044
self.assertEqual(top_k_frequent([1, 2, 2, 3, 3, 3], 2), [3, 2])

Arrays & Hashing/two_sum.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
from typing import List
55
from collections import defaultdict
66

7+
78
def two_sum(nums: List[int], target) -> List[int]:
89
complements = defaultdict(int)
910

1011
for i, num in enumerate(nums):
1112
complement = target - num
1213

1314
if complement in complements:
14-
return [
15-
complements[complement], i
16-
]
15+
return [complements[complement], i]
1716

1817
complements[num] = i
1918

2019
return []
2120

21+
2222
class Test(unittest.TestCase):
2323
def test_two_sum(self):
2424
self.assertEqual(two_sum([3, 4, 5, 6], 7), [0, 1])

Arrays & Hashing/vaild_anagram.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import unittest
44

5+
56
def is_anagram(word_one: str, word_two: str) -> bool:
67
n = len(word_one)
78
m = len(word_two)
@@ -29,6 +30,7 @@ def is_anagram(word_one: str, word_two: str) -> bool:
2930

3031
return True
3132

33+
3234
class Test(unittest.TestCase):
3335
def test_is_anagram(self):
3436
self.assertTrue(is_anagram("racecar", "carrace"))

0 commit comments

Comments
 (0)