Question

Write a function weave(s1, s2) that takes as inputs two strings s1 and s2 and uses...

Write a function weave(s1, s2) that takes as inputs two strings s1 and s2 and uses recursion to construct and return a new string that is formed by “weaving” together the characters in the strings s1 and s2 to create a single string. In other words, the new string should alternate characters from the two strings: the first character from s1, followed by the first character from s2, followed by the second character from s1, followed by the second character from s2, etc. If one of the strings is longer than the other, its “extra” characters – the ones with no counterparts in the shorter string – should appear immediately after the “woven” characters (if any) in the new string. >>> weave('aaaaaa', 'bbbbbb') result: 'abababababab' >>> weave('abcde', 'VWXYZ') result: 'aVbWcXdYeZ' >>> weave('aaaaaa', 'bb') # four extra 'a' characters at the end result: 'ababaaaa' >>> weave('aaaa', 'bbbbbb') # two extra 'b' characters at the end result: 'ababababbb' >>> weave('aaaa', '') # all of the 'a' characters are extra! result: 'aaaa' >>> weave('', 'bbbb') # all of the 'b' characters are extra! result: 'bbbb' >>> weave('', '') result: '' Hint: You will need more than one base case

can someone please explain it step by step .

Homework Answers

Answer #1
def weave(s1, s2):
    if len(s1) > 0 and len(s2) > 0:  # if both s1 and s2 are non-empty
        # then weave a letter from s1 and a letter from s2 and recursively weave rest of letters from s1 and s2
        return s1[0] + s2[0] + weave(s1[1:], s2[1:])
    elif len(s1) > 0:  # if only s1 is not empty
        return s1  # then return s1
    else:  # if only s2 is not empty
        return s2  # then return s2


print(weave('aaaaaa', 'bbbbbb'))
print(weave('abcde', 'VWXYZ'))
print(weave('aaaaaa', 'bb'))
print(weave('aaaa', 'bbbbbb'))
print(weave('aaaa', ''))
print(weave('', 'bbbb'))
print(weave('', ''))

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
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
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
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list strings and the second...
Write a Python function that takes two parameters: the first a list strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False.
Write a java program "without using Strings using loops and methods "that lets the user to...
Write a java program "without using Strings using loops and methods "that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result...
Using Python, Write a function to return all the common characters in two input strings. The...
Using Python, Write a function to return all the common characters in two input strings. The return must be a list of characters and only unique characters will be returned. For example, given 'abcdefg' and 'define', the function will return ['d', 'e', 'f'] (note e only appears once). (Hint: Use the in operator.) Following is what I've done so far. When I enter 'abcdefg' for the first string, and 'define' for the second string, I get ['d','e','f'], which is correct....
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT