Question

Python Practice Sample: Write a function shuffle (s1, s2) which creates a new string formed by...

Python Practice Sample:

Write a function shuffle (s1, s2) which creates a new string formed by “shuffling” the letters of two strings s1 and s2. If the strings are not of equal lengths, concatenate the remaining letters of the longer string to the result. Your program should work with the code below:

s1 = "abcd"

s2 = "1234"

s3 = "abcdefg"

s4 = "123456"

print(f"{s1:10s}\t{s2:10s}\t{shuffle (s1,s2)}")

print(f"{s3:10s}\t{s2:10s}\t{shuffle (s3,s2)}")

print(f"{s1:10s}\t{s4:10s}\t{shuffle (s1,s4)}")

Output:

abcd 1234 a1b2c3d4

abcdefg 1234 a1b2c3d4efg

abcd 123456 a1b2c3d456

Homework Answers

Answer #1
def shuffle(s1, s2):
    result = ''
    i = 0
    while i < len(s1) or i < len(s2):
        if i < len(s1):
            result += s1[i]
        if i < len(s2):
            result += s2[i]
        i += 1
    return result


# Testing the function here. ignore/remove the code below if not required
s1 = "abcd"
s2 = "1234"
s3 = "abcdefg"
s4 = "123456"
print(f"{s1:10s}\t{s2:10s}\t{shuffle(s1, s2)}")
print(f"{s3:10s}\t{s2:10s}\t{shuffle(s3, s2)}")
print(f"{s1:10s}\t{s4:10s}\t{shuffle(s1, s4)}")

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT