Skip to content

Commit 30a0163

Browse files
Refactor variable names for clarity in encoding and anagram modules
Renamed variables in `encode_and_decode_strings.py` and `valid_anagram.py` to improve code readability and enhance semantic clarity. These updates ensure more intuitive understanding of function arguments and internal logic. No functional changes were introduced.
1 parent 143e49c commit 30a0163

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

Arrays & Hashing/encode_and_decode_strings.py

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

6-
def encode(strs: List[str]) -> str:
6+
def encode(words: List[str]) -> str:
77
encoded_words = []
88

9-
for word in strs:
9+
for word in words:
1010
encoded_word = str(len(word)) + "#" + word
1111
encoded_words.append(encoded_word)
1212

1313
return ' '.join(encoded_words)
1414

15-
def decode(s: str) -> List[str]:
16-
n = len(s)
15+
def decode(encoded_words: str) -> List[str]:
16+
n = len(encoded_words)
1717
decoded_words = []
1818
i = 0
1919

2020
while i < n:
2121
j = i
2222
# Find the position of the next '#'
23-
while j < n and s[j] != '#':
23+
while j < n and encoded_words[j] != '#':
2424
j += 1
2525

2626
# If no '#' is found, break the loop to avoid infinite loop
@@ -29,19 +29,19 @@ def decode(s: str) -> List[str]:
2929

3030
# Try to convert the substring to an integer
3131
try:
32-
length = int(s[i:j])
32+
length = int(encoded_words[i:j])
3333
except ValueError:
34-
raise ValueError(f"Invalid length found: {s[i:j]}")
34+
raise ValueError(f"Invalid length found: {encoded_words[i:j]}")
3535

3636
# Move i to the start of the actual word
3737
i = j + 1
3838
j = i + length
3939

4040
# Check if the end of the word is within bounds
4141
if j > n:
42-
raise ValueError(f"Word length {length} is out of bounds for the string: {s[i:]}")
42+
raise ValueError(f"Word length {length} is out of bounds for the string: {encoded_words[i:]}")
4343

44-
decoded_word = s[i:j]
44+
decoded_word = encoded_words[i:j]
4545
decoded_words.append(decoded_word)
4646

4747
# Move i to the start of the next length

Arrays & Hashing/vaild_anagram.py

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

33
import unittest
44

5-
def is_anagram(s: str, t: str) -> bool:
6-
n = len(s)
7-
m = len(t)
5+
def is_anagram(word_one: str, word_two: str) -> bool:
6+
n = len(word_one)
7+
m = len(word_two)
88

99
if n != m:
1010
return False
1111

1212
seen = {}
1313

14-
for char in s:
14+
for char in word_one:
1515
if char in seen:
1616
seen[char] += 1
1717
else:
1818
seen[char] = 1
1919

20-
for char in t:
20+
for char in word_two:
2121
if char in seen:
2222
seen[char] -= 1
2323
else:

0 commit comments

Comments
 (0)