def reverse_sentence(s: str) -> str: """ Given a sentence , we define a word within to be a continuous sequence of characters in that starts with a capital letter and ends before the next capital letter in the string or at the end of the string, whichever comes first. A word can include a mixture of punctuation and spaces. This means that in the string 'ATest string!', there are in fact only two words: 'A' and 'Test string!'. Again, keep in mind that words start with a capital letter and continue until the next capital letter or the end of the string, which is why we consider 'Test string!' as one word. This function will reverse each word found in the string, and return a new string with the reversed words, as illustrated in the doctest below. >>> reverse_sentence('watchNow') 'watchwoN' >>> reverse_sentence('hot') 'hot' """ do it on python, and do not use any list, import or split, or append or join.
def reverse_sentence(s: str) -> str:
"""
Given a sentence <s>, we define a word within <s> to be a continuous
sequence of characters in <s> that starts with a capital letter and
ends before the next capital letter in the string or at the end of
the string, whichever comes first. A word can include a mixture of
punctuation and spaces.
This means that in the string 'ATest string!', there are in fact only two
words: 'A' and 'Test string!'. Again, keep in mind that words start with a
capital letter and continue until the next capital letter or the end of the
string, which is why we consider 'Test string!' as one word.
This function will reverse each word found in the string, and return a new
string with the reversed words, as illustrated in the doctest below.
>>> reverse_sentence('watchNow')
'watchwoN'
>>> reverse_sentence('hot')
'hot'
"""
do it on python, and do not use any list, import or split, or append or join.
Algorithm:
- Start
- Implement a method reverse_sentence() which a string as argument
- Inside the method, Initialize index to -1
- Declare 2 empty strings newString and s1
- Iterate through the loop from j=0 to j=len(a)-1
- If s[j] is uppercase letter, then check if len(s1)>0 or not. If yes, then append reverse of s1 to newString
- Else set index to j and add s[j] to s1
- If letter is not upper case and index is -1, then add s[j] to newString
- Else add s[j] to s1
- After completion of iteration, add reverse of s1 to newString
- Return newString
- Inside the main method, call the reverse_sentence() function and print returned value
- Stop
Step by step
Solved in 4 steps with 2 images