HackerRank Python (Basic) certification
String Transformation
A string question with one trap: comparing letters with < and > in Python compares character codes, and every uppercase letter has a smaller code than every lowercase one.
What the question asks
You get a sentence of words separated by spaces. Each word is made of English letters, upper or lower case. Build a new version of every word like this:
- The first letter of the word stays exactly as it is.
- Every later letter is compared with the letter just before it, by position in the alphabet:
- if the earlier letter comes first in the alphabet, the current letter becomes uppercase;
- if the current letter comes first, it becomes lowercase;
- if they are the same letter, the current letter is left alone.
Return the new words joined by spaces.
Function to write
def transformSentence(sentence):
# return a string
...The stub reads the sentence, calls your function and writes what you return.
Solution
def transformSentence(sentence):
new_words = []
for word in sentence.split(" "):
if not word:
new_words.append(word)
continue
new_word = word[0]
for i in range(1, len(word)):
prev = word[i - 1].lower()
curr = word[i].lower()
if prev < curr:
new_word += word[i].upper()
elif prev > curr:
new_word += word[i].lower()
else:
new_word += word[i]
new_words.append(new_word)
return " ".join(new_words)How it works
split(" ")gives the words. Each word is handled on its own, so the last letter of one word is never compared with the first letter of the next.new_wordstarts with the first letter, unchanged.- For every later position, both letters are lowercased before comparing. That makes the comparison about the alphabet, not about case.
- The new letter is built from the original
word[i]. When the two letters are the same, the original is kept with its original case. - The
if not wordcheck only matters if the input ever has two spaces in a row. It keeps the spacing the same instead of crashing onword[0].
Mistakes that fail the hidden tests
- Comparing without lowercasing.
'O' < 'c'isTruein Python, becauseord('O')is 79 andord('c')is 99. So “coOL” would be handled as if O came before c in the alphabet. Compare.lower()versions. - Lowercasing the letter when the two are equal. The rule says leave it unchanged. In “coOL”, the second O follows an o and must stay uppercase.
- Changing the first letter. The first letter of each word keeps its case, whatever it is. There is nothing before it to compare with.
- Running one loop over the whole sentence. If you walk the sentence character by character, the space and the next word’s first letter get mixed into the comparisons unless you handle them carefully. Splitting into words first avoids this.
Try it on sample data
print(transformSentence("coOL dog"))
print(transformSentence("Hello World"))Output from my run:
cOOl dOg
HeLlO WoRld
Walking through “coOL”: c stays. o comes after c, so it becomes O. The next O is the same letter as the o before it, so it stays O. L comes before O, so it becomes l. Result: “cOOl”.