Skip to content

Commit f432bc7

Browse files
add alternative_string_arrange method (TheAlgorithms#4595)
* add alternative_string_arrange method * fix issue * fix one more issue * changed the variable name li to output_list
1 parent 5957eab commit f432bc7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

strings/alternative_string_arrange.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def alternative_string_arrange(first_str: str, second_str: str) -> str:
2+
"""
3+
Return the alternative arrangements of the two strings.
4+
:param first_str:
5+
:param second_str:
6+
:return: String
7+
>>> alternative_string_arrange("ABCD", "XY")
8+
'AXBYCD'
9+
>>> alternative_string_arrange("XY", "ABCD")
10+
'XAYBCD'
11+
>>> alternative_string_arrange("AB", "XYZ")
12+
'AXBYZ'
13+
>>> alternative_string_arrange("ABC", "")
14+
'ABC'
15+
"""
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)
28+
29+
30+
if __name__ == "__main__":
31+
print(alternative_string_arrange("AB", "XYZ"), end=" ")

0 commit comments

Comments
 (0)