149k views
2 votes
Write a method that starts with 2 strings. If one string is odd and the other string is even then it will place the odd string in the middle of the even string. But if both Strings are of odd length then it will concatenate the first letter of the first string with the LAST letter of the second string. And finally, if the strings are both of Even length then it will take the first half of the first string and concatenate it with the second half on the second string.

User Aloso
by
6.9k points

1 Answer

3 votes

Here is an example of a Python function that implements the logic you described:

Copy code

def string_manipulation(string1, string2):

if len(string1) % 2 != 0 and len(string2) % 2 == 0:

return string2[:len(string2)//2] + string1 + string2[len(string2)//2:]

elif len(string1) % 2 == 0 and len(string2) % 2 != 0:

return string1[:len(string1)//2] + string2 + string1[len(string1)//2:]

elif len(string1) % 2 != 0 and len(string2) % 2 != 0:

return string1[0] + string2[-1]

else:

return string1[:len(string1)//2] + string2[len(string2)//2:]

You can call this function and pass in the two strings as arguments, like this:

Copy code

string1 = "hello"

string2 = "world"

result = string_manipulation(string1, string2)

print(result)

You can also modify the function to print some debugging statements to better understand how the code is executing, or add more test cases, if you want to further debug it.

Uday Tahlan

User Cory McCarty
by
7.4k points