Created
August 8, 2022 17:02
-
-
Save ZakriaJanjua/0f96370399522a7f8cfa07ff0c9c6d01 to your computer and use it in GitHub Desktop.
Hackerrank Python(Basic) certification test question. Required to manipulate a sentence according to an algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transformSentence(sentence): | |
# Write your code here | |
words = sentence.split(' ') | |
result_arr = [] | |
for word in words: | |
if len(word) == 1: | |
result_arr.append(word) | |
continue | |
new_word = '' | |
for i in range(0, len(word)): | |
if i == 0: | |
new_word += word[i] | |
continue | |
if (ord(word[i].lower()) > ord(word[i-1].lower())): | |
new_word += word[i].upper() | |
elif(ord(word[i].lower()) < ord(word[i-1].lower())): | |
new_word += word[i].lower() | |
else: | |
new_word += word[i] | |
result_arr.append(new_word) | |
new_sentence = ' '.join(result_arr) | |
return new_sentence |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment