Question

In: Computer Science

The use of list or any other data type not allowed only string and string methods...

The use of list or any other data type not allowed only string and string methods .Hint: Depending on how you plan to solve this problem, accumulator variable initialized as an empty string may help in this question. Write a function called weaveop, that takes a single string parameter (s) and returns a string. This function considers every pair of consecutive characters in s. It returns a string with the letters o and p inserted between every pair of consecutive characters of s, as follows. If the first character in the pair is uppercase, it inserts an uppercase O. If however, it is lowercase, it inserts the lowercase o. If the second character is uppercase, it inserts an uppercase P. If however, it is lowercase, it inserts the lowercase p. If at least one of the character is not a letter in the alphabet, it does not insert anything between that pair. Finally, if s has one or less characters, the function returns the same string as s. Do dir(str) and check out methods isalpha (by typing help(str.isalpha) in Python shell), and isupper

>>> weaveop("aa") 'aopa'

>>> weaveop("aB") 'aoPB'

>>> weaveop("ooo") 'oopoopo'

>>> weaveop("ax1") 'aopx1'

>>> weaveop("abcdef22") 'aopbopcopdopeopf22'

>>> weaveop("abcdef22x") 'aopbopcopdopeopf22x'

>>> weaveop("aBCdef22x") 'aoPBOPCOpdopeopf22x'

>>> weaveop("x") 'x'

>>> weaveop("123456") '123456'

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

def weaveop(string):
    weaved_string = ''
    for i in range(0, len(string) - 1):
        weaved_string += string[i]
        if string[i].isalpha() and string[i + 1].isalpha():
            if string[i].isupper():
                weaved_string += 'O'
            else:
                weaved_string += 'o'
        if string[i + 1].isalpha() and string[i].isalpha():
            if string[i + 1].isupper():
                weaved_string += 'P'
            else:
                weaved_string += 'p'
    weaved_string += string[-1]
    return weaved_string


print(weaveop("aa") == 'aopa')
print(weaveop("aB") == 'aoPB')
print(weaveop("ooo") == 'oopoopo')
print(weaveop("ax1") == 'aopx1')
print(weaveop("abcdef22") == 'aopbopcopdopeopf22')
print(weaveop("abcdef22x") == 'aopbopcopdopeopf22x')
print(weaveop("aBCdef22x") == 'aoPBOPCOpdopeopf22x')
print(weaveop("x") == 'x')
print(weaveop("123456") == '123456')

==================================================================


Related Solutions

Suppose that a “word” is any string of six letters. Repeated letters are allowed. For our...
Suppose that a “word” is any string of six letters. Repeated letters are allowed. For our purposes, vowels are the letters a, e, i, o, and u. a) How many words are there? b) How many words begin with a vowel? c) How many words begin with a vowel and end with a vowel? d) How many words have no vowels? e) How many words have exactly one vowel? A professor teaching a Discrete Math course gives a multiple choice...
Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:
IN JAVA Array Operations Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods: getTotal: This method should accept a one-dimensional array as its argument and return the total of the values in the array. getAverage: This method should accept a one-dimensional array as its argument and return the average of the values in the array. getHighest: This method should accept a...
Choose the degree AND the type of elasticity list for each event below: Please use ONLY...
Choose the degree AND the type of elasticity list for each event below: Please use ONLY lower case letters as your answers. Degree Type a) perfectly elastic f) price elasticity of demand b) elastic g) price elasticity of supply c) unit elastic h) cross elasticity (substitute) d) inelastic i) cross elasticity (complement) e) perfectly inelastic j) income elasticity (normal) k) income elasticity (inferior) Part 1. Egg prices change by -6 % causing the demand for toast to change by 3...
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
(Use the String Class to Solve This (and only the iostream, string and cctype libraries)) Write...
(Use the String Class to Solve This (and only the iostream, string and cctype libraries)) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or...
a. Define a function less of type (String, List) -> List so that less(e, L) is...
a. Define a function less of type (String, List) -> List so that less(e, L) is a list of all the strings in L that are shorter than e. b. Define a function more of type (String, List) -> List so that more(e, L) is a list of all the strings in L that are longer than e. c. Replace the above functions with a function compare of type (String, List, (String, String) -> Boolean) such that depending on the...
C++ ONLY! Implement the find function for the List class. It takes a string as an...
C++ ONLY! Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator. #include <iostream> #include <cstddef> #include <string> using Item = std::string; class List { private: class ListNode { public: Item item; ListNode * next; ListNode(Item i, ListNode *n=nullptr) { item = i; next = n; } };    ListNode * head; ListNode * tail;    public: class...
A is allowed to select any date of the year other than December 31. B may...
A is allowed to select any date of the year other than December 31. B may then select any date later in the same month or the same day of any later month. For example, if A selects June 16, then B may choose any later date in June or the 16th of any month from July to December. Using the same rule with regard to the date that B has selected, A must select a new date, and so...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes as input a matrix, and as output returns true if at least one entry in the matrix is strictly greater than 8 and returns false otherwise.
What are methods firms use to diversify risk? What are the methods individuals use? What type...
What are methods firms use to diversify risk? What are the methods individuals use? What type of risk taker would you say you are and why? How would time impact the risk you or a firm are willing to take?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT