0

code for a function which increments a string, to create a new string. If the string already ends with a number, the number should be incremented by 1. If the string does not end with a number the number 1 should be appended to the new string.

The output is correct but it is showing a String index out of range error. Can someone help me on where and how the string index is out of range?

test cases,expected output: (increment_string("foo"), "foo1"),(increment_string("foobar001"), "foobar002"),(increment_string("foobar1"), "foobar2"),(increment_string("foobar00"), "foobar01"),("foobar99"), "foobar100"),("foobar099"), "foobar100"),(increment_string(""), "1")

def increment_string(strng):
   if strng[-1].isdigit():
      exp_strng=strng[::-1]
      new_strng=""
      new_strng1=""
      for i in exp_strng:
         if i.isdigit():
            new_strng+=i
         else:
            break
      new_strng=new_strng[::-1]
      new_strng1=int(new_strng)+1
      new_strng1='{num:{fill}{width}}'.format(num=new_strng1, fill='0', width=len(new_strng))
      return(strng[:-len(new_strng)]+new_strng1)

   else:
      strng+="1"
      return(strng)
11
  • Huh, I am not seeing the string index out of range error. Are you sure this is the entirety of your erroneous code? Commented Jul 30, 2017 at 14:13
  • You're passing a string into new_strng1=int(new_strng)+1. Did you mean len(new_strng) + 1? Commented Jul 30, 2017 at 14:14
  • no new_strng1 is to convert string to integer then adding 1 to it @Skam Commented Jul 30, 2017 at 14:16
  • @Jerrybibo it shows error in codewars website while submitting code. all outputs are passed though. Commented Jul 30, 2017 at 14:18
  • does it provide you with the input used? if we can't reproduce, then it's hard for us to help you. ¯_(ツ)_/¯ Commented Jul 30, 2017 at 14:18

3 Answers 3

2

Since you gave us more information on the test cases given, you can bypass the edge case of an empty string by modifying the if statement:

def increment_string(strng):
   # Add it here #
   if strng == "":
      return "1"

   elif strng[-1].isdigit():
      exp_strng = strng[::-1]
      new_strng = ""
      new_strng1 = ""
      for i in exp_strng:
         if i.isdigit():
            new_strng += i
         else:
            break
      new_strng = new_strng[::-1]
      new_strng1 = int(new_strng) + 1
      new_strng1 = '{num:{fill}{width}}'.format(num=new_strng1, fill='0', width=len(new_strng))
      return strng[:-len(new_strng)] + new_strng1

   else:
      strng += "1"
      return strng
Sign up to request clarification or add additional context in comments.

Comments

2

If think this would be a better solution to your problem:

from re import search

def increment_string(s):
    number = search('\d+$', s)
    if number != None:
        number = number.group()
        first_part = s.split(number)[0]
        return first_part + str(int(number)+1)
    else:
        return s + '1'

I don't know what you want when the number is 9 though: 0 or 10. This code produces 10.

2 Comments

This does not solve the problem that OP has – the event in which an empty string "" is given as the argument.
I've edited it and now it should work perfectly, sorry for the mess.
-1

the error was caused when empty string is passed. and I resolved it by adding one more if else:(thanks to Skam)

def increment_string(strng):
if len(strng)>0:
    if strng[-1].isdigit():
        exp_strng=strng[::-1]
        new_strng=""
        new_strng=""
        for i in exp_strng:
            if i.isdigit():
                new_strng+=i
            else:
                break
        new_strng=new_strng[::-1]
        new_strng1=int(new_strng)+1
        new_strng1=str(new_strng1)
        new_strng1=new_strng1.zfill(len(new_strng))
        return(strng[:-len(new_strng)]+new_strng1)
    else:
        strng+="1"
        return(strng)
else:
    strng+="1"
    return(strng)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.