beginner coder here and would really appreciate some insight on this. Trying to create code to break instances of CamelCase - for example, gottaRunToTheStore, helloWorld, etc. and add space between the main words in the input string.
So my code below takes a string input and should separate the inner words, returning a new string with spaces in between the CamelCase instances. (so "breakCamelCase" should return "break Camel Case")
def solution(s):
ltstring = s # assign input string 's' to ltstring, so if there's no CamelCase, we'll just return this string at end
Camel = []
for i in range(len(s)): # for every character in string 's', identifying index where Camel Case occurs, adding it to list "Camel"
j = i 1 # prepping to not do last one
if (j) < len(s): # as long as next character index is still within string 's'
if s[j].isupper(): # if that next character in 's' is uppercase
Camel.append(j) # add index for that CamelCase instance to list Camel
else:
pass
else:
pass
new_list = [] # list for strings of separated 's' at CamelCase's
if Camel == []: # if nothing was added to "Camel" return original string - line 26
pass
else:
for i in range((len(Camel) 1)): # if there's CamelCase instances, add ind. words to a new_list
if i == 0: # if this is the first instance,
new_list = [s[0:Camel[0]]] # add string from first character to character before CamelCase
last_one = len(Camel) - 1 # integer value of index for last Camel Case location
if i == len(Camel): # if this is last instance,
new_list = [s[Camel[last_one]:]] # add string from last character onwards
else: # otherwise
new_list = [s[Camel[i-1]:Camel[i]]] # add string from previous char. index to current char. index
ltstring = " ".join(new_list)
return ltstring
However, when I run my tests on this code, I receive an extra space after the first CamelCase instance when there should only be one space.. but the following instances are just fine, simply the first one is a doublespace. Here's the example outputs.
print(solution("breakCamelCase"))
print(solution("eatingChickenSandwichToday"))
"break Camel Case"
"eating Chicken Sandwich Today"
I appreciate anyone helping out on this! I feel like I'm missing one little syntax issue or something small, I'm not sure.
CodePudding user response:
Add this line to remove the extra spaces:
new_list = list(filter(None, new_list))
ltstring = " ".join(new_list)
And here you can find other approaches
CodePudding user response:
Change the if-else statement inside the second for loop:
def solution(s):
ltstring = s
Camel = []
for i in range(len(s)):
j = i 1
if (j) < len(s):
if s[j].isupper():
Camel.append(j)
else:
pass
else:
pass
new_list = []
if Camel == []:
pass
else:
for i in range((len(Camel) 1)):
last_one = len(Camel) - 1
if i == 0:
new_list = [s[0:Camel[0]]]
# Modified if into elif
elif i == len(Camel):
new_list = [s[Camel[last_one]:]]
else:
new_list = [s[Camel[i-1]:Camel[i]]]
ltstring = " ".join(new_list)
return ltstring
Output:
print(solution("breakCamelCase"))
print(solution("eatingChickenSandwichToday"))
>>> break Camel Case
>>> eating Chicken Sandwich Today