2

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.

kashow
  • 23
  • 3

2 Answers2

1

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

Julio Porto
  • 113
  • 8
  • Wow, this worked - thanks! I'm not completely sure why, but I'm quite new at this so I need to study the filter function a bit. That link is solid too, I'm going to look into that more. – kashow Oct 22 '21 at 07:07
  • 1
    filter(function, iterable) Filter: Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed. – Julio Porto Oct 22 '21 at 07:50
  • 1
    In this case filter removes empty elements (None) in your list like the empty space " " after your first word. – Julio Porto Oct 22 '21 at 07:54
  • 1
    Please upvote/mark the response as correct. Thanks! – Julio Porto Oct 22 '21 at 07:54
  • Once I have enough rep. points I will upvote you ! Since I just joined this website it won't let me quite yet. :/ – kashow Oct 24 '21 at 20:15
1

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
Sprizgola
  • 416
  • 2
  • 10
  • Hmm, do I have to import something to be able to do this? When I changed it to elif it popped back with an error: ``` elif i == len(Camel): # if this is last instance, ^ SyntaxError: invalid syntax ``` – kashow Oct 22 '21 at 07:00
  • 1
    @kashow I am pretty sure that you can search on internet what SyntaxError is, besides it is self-explanatory – Matiiss Oct 22 '21 at 14:58
  • 1
    Ok, I'm silly and just realized that for elif to work the previous if statement has to be immediate, as you have changed it. Thanks!! This works great too. – kashow Oct 28 '21 at 00:34