In: Computer Science
write a python code that Returns a string composed of characters
drawn, in strict alternation, from s1 and s2.
If one string is longer than the other, the excess characters are
added to the end
of the string as shown in the examples below
#Example 1 - blend("ape", "BANANA") returns "aBpAeNANA"
#Example 2 - blend("BOOT", "gold") returns "BgOoOlTd"
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
# required function taking two strings
def blend(s1, s2):
# creating a string to store the combined value
combined = ''
i = 0 # current index value
# looping as long as i is a valid index in either s1 or s2 or both
while i < len(s1) or i < len(s2):
# if i is valid in s1, appending char at index i to combined
if i < len(s1):
combined += s1[i]
# if i is valid in s2, appending char at index i to combined
if i < len(s2):
combined += s2[i]
# advancing i
i += 1
# returning combined string
return combined
# end of blend() method
# code for testing, ignore if not needed
print(blend("ape", "BANANA")) # aBpAeNANA
print(blend("BOOT", "gold")) # BgOoOlTd
#output
aBpAeNANA
BgOoOlTd
#code screenshot
