Categories
Array Coderbyte Data Structures and Algorithms Python String manipulation

[Coderbyte] – Different Cases [Easy]

I was surprised to see that some of my solutions are in top solutions on Coderbyte. The answer to this challenge is the 16th solution in the top 18 solutions. Coming back to the problem, here is what we have to do:

Have the function DifferentCases(str) take the str parameter being passed and return it in upper camel case format where the first letter of each word is capitalized. The string will only contain letters and some combination of delimiter punctuation characters separating each word. 

For example: if str is “Daniel LikeS-coding” then your program should return the string DanielLikesCoding.

FIRST STEP – CREATE TWO EMPTY VARIABLES

The first step is to create two variables that are assigned an empty string and an empty list.

The first variable newStr will be assigned the string argument formatted – without any special characters. The second variable finalWord will be assigned the words with capital letters, that forms the final string when joined. 

def DifferentCases(str): 
    newStr = ''
    finalWord = []

SECOND STEP – REMOVE ALL SPECIAL CHARACTERS

The second step is to take the string passed as an argument and remove all the special characters from it. Strings are immutable, so the variable created previously is assigned the new formatted string without the special characters.

def DifferentCases(str): 
    newStr = ''
    finalWord = []
    
    for char in str:
        if char.isalpha():
            newStr += char
        else:
            newStr += ' '

To do so, I am looping over each character from the string and check if it’s a letter. In Python, we can check if a character is a letter by making use of the isalpha() method. This method returns true if the character is a letter, and false otherwise.

If the character from the string is a special character, a new space is added instead. 

For instance, if the string passed as an argument is “Catalin-LikeS & Coding”, the code above assigns “Catalin LikeS Coding” to the newStr variable.

THIRD STEP – CAPITALIZE THE FIRST LETTER OF EACH WORD AND TRANSFORM THE REST OF THE WORD TO LOWERCASE

Now that the string does not contain special strings anymore, we need to capitalise the first letter of each word. We also need to transform the rest of the word to lowercase.

As I have said previously, the newStr variable is assigned the string “Catalin LikeS Coding”. To manipulate the words and to remove the space between the words, I am splitting the string. That means, “Catalin LikeS Coding” becomes [‘Catalin’, ‘LikeS’, ‘Coding’].

    for word in newStr.split():
        word = word.replace(word[0], word[0].upper())
        word = word.replace(word[1:len(word)], word[1:len(word)].lower())
        finalWord.append(word)

For each word in the list, word.replace(word[0], word[0].upper()) replaces the first letter of the word with a capital letter.

For each word in the list, word.replace(word[1:len(word)], word[1:len(word)].lower()) replaces the rest of the word to lowercase letters.

Finally, the new modified word is added to the finalWord list.

FOURTH STEP – RETURN THE NEW FORMATTED STRING

The only thing left is to return the new string. To do so, I am making use of the join() method which returns a string concatenated with the elements of the list.

    return ''.join(finalWord)

The returned string is “CatalinLikesCoding”.

FULL SOLUTION

The full solution is below. You can run the code by clicking on this link (Repl.it repository).

def DifferentCases(str): 
    newStr = ''
    finalWord = []
    
    for char in str:
        if char.isalpha():
            newStr += char
        else:
            newStr += ' '
            
    for word in newStr.split():
        word = word.replace(word[0], word[0].upper())
        word = word.replace(word[1:len(word)], word[1:len(word)].lower())
        finalWord.append(word)
        
    return ''.join(finalWord)
Advertisements

Leave a Reply