We are given a string without any spaces in it.
The string follows camel case convention. This means the first word is not capitalized; however, every following word’s first letter is capitalized.
For example:
the_string = 'thisIsAnExample'
Our goal is to determine how many words are in the string, then print that number out.
For example:
input = 'thisIsAnExample'
output = 4
There are four words in the given string.
I want to hold my phrase in a list of the individual words. First, I will create an empty list.
I will then need to create an empty string that will serve as a temporary container.
I should be able to loop through the string by each individual character. I can check if the character is upper or lower case. If it’s lower case, add it to the temporary string. If it’s upper case, add the temporary string to the list of words AND reset the temporary string to be the new uppercase character.
Finally I should be able to return the length of the list. This will represent the number of words in the original string.
variable the_words = []
variable temp_word = ""
for loop -> loop through each letter in the string
if letter is lowercase
add the letter to the variable temp_word
else
add temp_word to the_words
clear out temp_word and set it to the letter
print(len(the_words))
the_words = []
temp_word = ""
for letter in s:
if letter.islower():
temp_word += letter
else:
phrase.append(temp_word)
temp_word = letter
print(len(phrase))
Let’s test it out.
We can see in this string the first four letters are all lower case (“t”, “h”, “i”, “s”), they will satisfy the if condition, and will be added to temp_word.
On the fifth letter (“I”) the if statement is not true, so the else statement is executed. This adds the temp_word (“this”) to the_words. It then overwrites temp_word to be just the current letter (“I”).
Current Variables:
the_words = ["this"]
temp_word = "I"
The next letter is lowercase (“s”) and is therefore added to temp_word.
The next letter is uppercase (“A”), thus temp_word is added to the_words, and temp_word is set to the current letter (“A”).
Current Variables:
the_words = ["this", "Is"]
temp_word = "A"
The next letter is lowercase (“n”) and is added to temp_word.
The next letter is uppercase (“E”), thus temp_word is added to the_words, and temp_word is set to the current letter (“E”).
Current Variables:
the_words = ["this", "Is", "An"]
temp_word = "E"
The remaining letters are all lowercase and are added to temp_word.
the_words = ["this", "Is", "An"]
temp_word = "Example"
Finally, the last line runs, printing out the length of the_words.
Output: 3
Oops! I made a small mistake. I forgot to add the last word to the_words, and so calling len(the_words) results in the incorrect answer. I can fix this by adding temp_word to the_words after the for loop.
the_words = []
temp_word = ""
for letter in s:
if letter.islower():
temp_word += letter
else:
phrase.append(temp_word)
temp_word = letter
the_words.append(temp_word)
print(len(phrase))
This will change our list the_words and allow the length function to return the correct value:
the_words = ["this", "Is", "An", "Example"]
temp_word = "Example"
Resulting in this:
Output: 4
Another Successful Live Coding session! We employed all the steps and came up with a solution to the problem!