Header Ads Widget

Python : string transformation | Hackerrank solution


 There is a sentence that consists of space-separated strings of upper and lower case English letters. Transform each string according to the given algorithm and return the new sentence.
Each string should be modified as follows:
• The first character of the string remains unchanged
• For each subsequent character, say x, consider a letter
preceding it, say y:
• If y precedes x in the English alphabet, transform x to uppercase
o If x precedes y in the English alphabet, transform x to lowercase
If x and y are equal, the letter remains unchanged


sentence = "cOOL dog"
The first letters of both words remain unchanged. Then, for the word "COOL" the first "o" is made uppercase because the letter preceding it, "c", comes earlier in the alphabet. Next, the case of the second "O" is unchanged because the letter preceding is also "o", and finally the "L" is made lowercase because the letter preceding it, "O", comes later in the alphabet. The second word, "dOg", is transformed according to the same rules. Return the resulting sentence 'cool dog'.

Function Description

Complete the function transformSentence in the editor below. The function must return a string representing the resulting sentence.

transformSentence has the following parameter(s): string sentence: the input sentence


def transformSentence(sentence):
    for i in l:
        for j in range(0,len(i)-1):
    for i in li:
        result=result+" "+i
    return (result.strip())
    # Write your code here

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    sentence = input()

    result = transformSentence(sentence)

    fptr.write(result + '\n')


Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-