Question

In: Computer Science

write a python code that Returns a string composed of characters drawn, in strict alternation, from...

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"

Solutions

Expert Solution

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


Related Solutions

8.Write in R code. a)stimulate a string of 10,000 characters drawn uniformly and independently for the...
8.Write in R code. a)stimulate a string of 10,000 characters drawn uniformly and independently for the set {A,C,G,T} [HINT: sample] b) create a frequency table of the string [HINT:table] c) write a function to create a contingency table of adjacent k-tuples. For example, with k=3 and with the string "CAGACAAAAC", you would want to produce the following table: [ ONLY USE FOR LOOPS AND PASTE (,collapse=""), DO NOT USE EMBED, SUBSTR, or DO.CALL] AAA AAC ACA AGA CAA CAG GAC...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced.
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced. Grouping characters are ( and ), [ and ], and { and }. I got the basic idea for this problem from HackerRank. It’s also a very common interview question.*******************8Example output**************Enter the expression:{[()]} {[()]}is balancedEnter the expression: {()()} {()()}is balancedEnter the expression: {([[])} {([[])}is not balancedEnter the expression: {([)]} {([)]}is not balanced
/**    * Returns the string formed by alternating the case of the characters in   ...
/**    * Returns the string formed by alternating the case of the characters in    * the specified string. The first character in the returned string is in    * lowercase, the second character is in uppercase, the third character is    * in lowercase, the fourth character is in uppercase, and so on.    * Examples:    *    * <ul>    * <li><code>alternatingCaps("a")</code> returns <code>"a"</code>    * <li><code>alternatingCaps("ab")</code> returns <code>"aB"</code>    * <li><code>alternatingCaps("abc")</code> returns <code>"aBc"</code>    *...
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone...
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone number, e.g., 800-MYPYTHON, and prints the all numeric equivalent, 800-69798466. You should implement this using a loop construct to process each character from left to right. Build a new string that is the all numeric equivalent and then print the string.
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT