Skip to content

Instantly share code, notes, and snippets.

@ZakriaJanjua
Created August 8, 2022 17:02
Show Gist options
  • Save ZakriaJanjua/0f96370399522a7f8cfa07ff0c9c6d01 to your computer and use it in GitHub Desktop.
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.
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