In: Computer Science
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'
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')
==================================================================