Python Hello I am trying to learn basic recursive function on Python and I am do
ID: 3902317 • Letter: P
Question
Python
Hello I am trying to learn basic recursive function on Python and I am doing some of few exercises for practice. I am trying to make a function that returns the number that numbers in the list are strictly increasing using recursive, but counting algorithm is problem here is my code so far...
def count_increasing_number(string, number=0):
#If string lenth is 1, answer is 1
counter = 1
#Base condition
if(number > int(string)):
return 0
for i in range(1, len(string)):
#If first part is greater than previous number
if int(string[:i]) > number:
#If last part is empty, increment ans
res_string = string[i:]
if len(res_string) == 0:
counter += 1
#Proceed with the remaining part
else:
number = int(string[:i])
counter += count_increasing_number(res_string, number)
return counter
print(count_increasing_number('123'))
print(count_increasing_number('19325'))
print(count_increasing_number('1111'))
print(count_increasing_number('10203'))
I want to get
3
4
2
4
which are
[1,2,3],[1,23], [123]
[1,9, 325], [1,9325], [19, 325], [19325]
[1,111], [1111]
[1, 02, 03], [1, 0203], [10, 203], [10203]
but the output of my code are
3
4
3
4
There seems to be a problem with the counting algorithm, but I don't know where it's wrong.
If possible, could you let it print a list of numbers as well as counter values?
for example)
[1,2,3],[1,23], [123]
[1,9, 325], [1,9325], [19, 325], [19325]
[1,111], [1111], [????]
[1, 02, 03], [1, 0203], [10, 203], [10203]
I want to find out the error and correct it, but I don't know how to get a list of numbers out, so it's hard to find out what went wrong. I know this is really stupid question, but I cannot find the solution. Please help me....
Explanation / Answer
The code is perfect except one minor change. I have corrected it.
#Base condition
if(number >=int(string)):
return 0
It was before "if(number >int(string)):"
Because number = 0 when it call function count_increasing_number() and int(string) will never be less than zero. So I put '>=' which gives correct result.
The corrected code:
def count_increasing_number(string, number=0):
#If string lenth is 1, answer is 1
counter = 1
#Base condition
if(number >= int(string)): # Modification done here
return 0
for i in range(1, len(string)):
#If first part is greater than previous number
if int(string[:i]) > number:
# print("string",int(string[:i]))
#If last part is empty, increment ans
res_string = string[i:]
# print("res_string",res_string)
if len(res_string) == 0:
counter += 1
#Proceed with the remaining part
else:
number = int(string[:i])
counter += count_increasing_number(res_string, number)
# print("else part",counter)
return counter
print(count_increasing_number('123'))
print(count_increasing_number('19325'))
print(count_increasing_number('1111'))
print(count_increasing_number('10203'))
The output is:
3
4
2
4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.