0
I want to count how many times the numbers 3 and 4 appear. What is going wrong?
def count(x):
y=0
if x[0]=='':
print(y)
return
if x[0]=='3':
y=y+1
return count(x[1:])
count('23334')
0
I want to count how many times the numbers 3 and 4 appear. What is going wrong?
def count(x):
y=0
if x[0]=='':
print(y)
return
if x[0]=='3':
y=y+1
return count(x[1:])
count('23334')
3
Your code has some logic problems. Follow the table test for input 23334
:
count
is called with x = '23334'
;y
is defined as 0;x
. Fake, move on, move on;count('3334')
;count
is called with x = '3334'
;y
is defined as 0;x
. Fake, move on, move on;y
(y = 1
);count('334')
;count
is called with x = '334'
;y
is defined as 0;x
. Fake, move on, move on;y
(y = 1
);count('34')
;count
is called with x = '34'
;y
is defined as 0;x
. Fake, move on, move on;y
(y = 1
);count('4')
;count
is called with x = '4'
;y
is defined as 0;x
. Fake, move on, move on;count('')
;count
is called with x = ''
;y
is defined as 0;x
. Error! No position 0 in x
;y
(y = 0
);In short: your code gives error and you omitted this information in the question.
Indexerror: string index out of range
The same is explained in item 28 of the table test.
And even if being disregarded the error, its function would print on the screen the value 9, returning a null value.
First, we will prevent the error from happening. To check whether the value of x
is not null, just do if x: ...
. This way, our function will be:
def count(x):
y = 0
if x:
... # Implementado posteriormente
return y
For if x
is null, returns the value of y
, which will be 0. Now, within the if
, we guarantee that x
is not null and there is the 0 position. We can check if it has the 3 digit:
def count(x):
y = 0
if x:
if x[0] == '3':
y += 1
... # Implementado posteriormente
return y
If the digit is 3, we increment the value of y
in 1, but regardless of whether or not the digit 3 is the value of y
should be incremented according to the amount of digits 3 in the rest of the string. This was your other error: you did not accumulate the current value of y
with the value that is returned from the next function calls. For this we do:
def count(x):
y = 0
if x:
if x[0] == '3':
y += 1
y += count(x[1:])
return y
And this way our function is already complete and functional. If we call it passing as parameter the value '23334'
, the answer will be 3.
See working on Repl.it.
If you want to count both digits 3 and digits 4, you can do:
def count(x):
y = 0
if x:
if x[0] in ('3', '4'):
y += 1
y += count(x[1:])
return y
print(count("23334"))
See working on Repl.it.
The table test of this my solution I leave as activity for you to do.
For a solution without recursive use, simply use the method count
of the object string:
>>> print("23334".count("3"))
3
>>> print("23334".count("4"))
1
Browser other questions tagged python-3.x
You are not signed in. Login or sign up in order to post.
Doubt: The solution must necessarily use recursion or you thought it would be the best way?
– Woss
It is an exercise that must use recursion
– Yonack