mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
add alternative_string_arrange method (#4595)
* add alternative_string_arrange method * fix issue * fix one more issue * changed the variable name li to output_list
This commit is contained in:
parent
5957eabd3e
commit
f432bc76a6
31
strings/alternative_string_arrange.py
Normal file
31
strings/alternative_string_arrange.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
def alternative_string_arrange(first_str: str, second_str: str) -> str:
|
||||||
|
"""
|
||||||
|
Return the alternative arrangements of the two strings.
|
||||||
|
:param first_str:
|
||||||
|
:param second_str:
|
||||||
|
:return: String
|
||||||
|
>>> alternative_string_arrange("ABCD", "XY")
|
||||||
|
'AXBYCD'
|
||||||
|
>>> alternative_string_arrange("XY", "ABCD")
|
||||||
|
'XAYBCD'
|
||||||
|
>>> alternative_string_arrange("AB", "XYZ")
|
||||||
|
'AXBYZ'
|
||||||
|
>>> alternative_string_arrange("ABC", "")
|
||||||
|
'ABC'
|
||||||
|
"""
|
||||||
|
first_str_length: int = len(first_str)
|
||||||
|
second_str_length: int = len(second_str)
|
||||||
|
abs_length: int = (
|
||||||
|
first_str_length if first_str_length > second_str_length else second_str_length
|
||||||
|
)
|
||||||
|
output_list: list = []
|
||||||
|
for char_count in range(abs_length):
|
||||||
|
if char_count < first_str_length:
|
||||||
|
output_list.append(first_str[char_count])
|
||||||
|
if char_count < second_str_length:
|
||||||
|
output_list.append(second_str[char_count])
|
||||||
|
return "".join(output_list)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(alternative_string_arrange("AB", "XYZ"), end=" ")
|
Loading…
Reference in New Issue
Block a user