a quiz | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

a quiz

Your job is to write 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. Examples: foo -> foo1 foobar23 -> foobar24 foo0042 -> foo0043 foo9 -> foo10 foo099 -> foo100 Attention: If the number has leading zeros the amount of digits should be considered.

21st Oct 2018, 3:46 AM
Liu zirui
Liu zirui - avatar
4 Answers
+ 4
def increment_string(str_): num = 0 for i, s in enumerate(str_): if s.isdigit(): num = str_[i:] s = str_[:i] break if num == 0: return str_ + '1' else: return s + str(int(num)+1).zfill(len(num)) #'abc'.zfill(4) = 0abc print(increment_string('foo099'))
21st Oct 2018, 10:14 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 2
Are you having some issue with that task? Liu zirui
21st Oct 2018, 6:54 AM
Lord Krishna
Lord Krishna - avatar
0
def increment_string(str): a =[] for i in reversed(str): while i.isdigit(): a.append(int(i)) break return(a)
21st Oct 2018, 3:49 AM
Liu zirui
Liu zirui - avatar
0
the uncompleted work
21st Oct 2018, 3:49 AM
Liu zirui
Liu zirui - avatar