Recursion - I want to count how many times digits 3 and 4 appear

Asked

Viewed 311 times

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')
  • Doubt: The solution must necessarily use recursion or you thought it would be the best way?

  • It is an exercise that must use recursion

1 answer

3


Problem

Your code has some logic problems. Follow the table test for input 23334:

  1. The function count is called with x = '23334';
  2. The value of y is defined as 0;
  3. Check the position 0 of x. Fake, move on, move on;
  4. Check if in position 0 there is a 3. False, move on;
  5. Returns the value of count('3334');
  6. The function count is called with x = '3334';
  7. The value of y is defined as 0;
  8. Check the position 0 of x. Fake, move on, move on;
  9. Check if at position 0 there is a 3. True, increment y (y = 1);
  10. Returns the value of count('334');
  11. The function count is called with x = '334';
  12. The value of y is defined as 0;
  13. Check the position 0 of x. Fake, move on, move on;
  14. Check if at position 0 there is a 3. True, increment y (y = 1);
  15. Returns the value of count('34');
  16. The function count is called with x = '34';
  17. The value of y is defined as 0;
  18. Check the position 0 of x. Fake, move on, move on;
  19. Check if at position 0 there is a 3. True, increment y (y = 1);
  20. Returns the value of count('4');
  21. The function count is called with x = '4';
  22. The value of y is defined as 0;
  23. Check the position 0 of x. Fake, move on, move on;
  24. Check if in position 0 there is a 3. False, move on;
  25. Returns the value of count('');
  26. The function count is called with x = '';
  27. The value of y is defined as 0;
  28. Check the position 0 of x. Error! No position 0 in x;
  29. Disregarding the error: Print y (y = 0);
  30. Returns a null value for item 25;
  31. From item 25 is returned null to item 20;
  32. From item 20 is returned null to item 15;
  33. From item 15 is returned null to item 10;
  34. From item 10 is returned null to item 5;
  35. From item 5 is returned null to item 1;

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.

Solution

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.


Solution without using recursiveness

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

You are not signed in. Login or sign up in order to post.