4

For this problem, I am given strings ThatAreLikeThis where there are no spaces between words and the 1st letter of each word is capitalized. My task is to lowercase each capital letter and add spaces between words. The following is my code. What I'm doing there is using a while loop nested inside a for-loop. I've turned the string into a list and check if the capital letter is the 1st letter or not. If so, all I do is make the letter lowercase and if it isn't the first letter, I do the same thing but insert a space before it.

def amendTheSentence(s):
    s_list = list(s)

    for i in range(len(s_list)):

        while(s_list[i].isupper()):
            if (i == 0):
                s_list[i].lower()
            else:
                s_list.insert(i-1, " ")
                s_list[i].lower()

    return ''.join(s_list)

However, for the test case, this is the behavior:

Input:  s:        "CodesignalIsAwesome"
Output:           undefined
Expected Output:  "codesignal is awesome"
Console Output:   Empty
Onur-Andros Ozbek
  • 2,998
  • 2
  • 29
  • 78
  • Adding or removing items from a list while iterating over it is almost always a bug. Make a copy of it with `[:]`, or better yet, build a result list and join it into a string at the end. – ggorlen Mar 31 '20 at 05:20
  • Also you must call the `isupper` method instead of `while(s_list[i].isupper):` – Abdul Niyas P M Mar 31 '20 at 05:21
  • Does this answer your question? [Why does Python skip elements when I modify a list while iterating over it?](https://stackoverflow.com/questions/742371/why-does-python-skip-elements-when-i-modify-a-list-while-iterating-over-it) – ggorlen Mar 31 '20 at 05:22
  • @AbdulNiyasPM isn't that what I'm doing? – Onur-Andros Ozbek Mar 31 '20 at 05:22
  • @ggorlen No it does not. Also this is a code challenge. I want to practise solving such problems without unnecessarily occupying space. – Onur-Andros Ozbek Mar 31 '20 at 05:23

3 Answers3

4

You can use re.sub for this:

re.sub(r'(?<!\b)([A-Z])', ' \\1', s)

Code:

import re

def amendTheSentence(s):
    return re.sub(r'(?<!\b)([A-Z])', ' \\1', s).lower()

On run:

>>> amendTheSentence('GoForPhone')
go for phone
Austin
  • 25,759
  • 4
  • 25
  • 48
1

Try this:

def amendTheSentence(s):
    start = 0
    string = ""
    for i in range(1, len(s)):
        if s[i].isupper():
            string += (s[start:i] + " ")
            start = i

    string += s[start:]
    return string.lower()


print(amendTheSentence("CodesignalIsAwesome"))
print(amendTheSentence("ThatAreLikeThis"))

Output:

codesignal is awesome
that are like this
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53
1
def amendTheSentence(s):
    new_sentence=''
    for char in s:
        if char.isupper():
            new_sentence=new_sentence + ' ' + char.lower() 
        else:
            new_sentence=new_sentence + char
    return new_sentence  

new_sentence=amendTheSentence("CodesignalIsAwesome") 
print (new_sentence)

result is  codesignal is awesome
Gerry P
  • 7,662
  • 3
  • 10
  • 20