Skip to content

Commit 346eb5a

Browse files
Update alternative_string_arrange.py
Analysis of the Code Type Hinting: The function lacks type hints for the parameters and return type. Adding them improves readability and helps with static type checking. Variable Naming: The variable names such as abs_length are somewhat misleading. A name like max_length would more accurately describe its purpose. Output List Initialization: The use of list for output_list is good, but the name could be more descriptive, such as result_chars. Loop Condition: The loop iterates based on the maximum length of the two strings, which is correct, but using a single loop for the two strings may make the code clearer and more efficient. Docstring: The docstring is clear but could use additional details on the behavior when one string is shorter than the other.
1 parent ebbdc71 commit 346eb5a

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

Diff for: strings/alternative_string_arrange.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
def alternative_string_arrange(first_str: str, second_str: str) -> str:
22
"""
33
Return the alternative arrangements of the two strings.
4-
:param first_str:
5-
:param second_str:
6-
:return: String
4+
5+
This function alternates characters from two input strings. If one string
6+
is longer, the remaining characters will be appended to the end of the
7+
resulting string.
8+
9+
:param first_str: The first string to arrange.
10+
:param second_str: The second string to arrange.
11+
:return: A new string with alternating characters from the input strings.
12+
713
>>> alternative_string_arrange("ABCD", "XY")
814
'AXBYCD'
915
>>> alternative_string_arrange("XY", "ABCD")
@@ -13,18 +19,16 @@ def alternative_string_arrange(first_str: str, second_str: str) -> str:
1319
>>> alternative_string_arrange("ABC", "")
1420
'ABC'
1521
"""
16-
first_str_length: int = len(first_str)
17-
second_str_length: int = len(second_str)
18-
abs_length: int = (
19-
first_str_length if first_str_length > second_str_length else second_str_length
20-
)
21-
output_list: list = []
22-
for char_count in range(abs_length):
23-
if char_count < first_str_length:
24-
output_list.append(first_str[char_count])
25-
if char_count < second_str_length:
26-
output_list.append(second_str[char_count])
27-
return "".join(output_list)
22+
max_length: int = max(len(first_str), len(second_str))
23+
result_chars: list = []
24+
25+
for i in range(max_length):
26+
if i < len(first_str):
27+
result_chars.append(first_str[i])
28+
if i < len(second_str):
29+
result_chars.append(second_str[i])
30+
31+
return ''.join(result_chars)
2832

2933

3034
if __name__ == "__main__":

0 commit comments

Comments
 (0)