I have this script:
def number_of_occurences(c, message):
position = message.find(c)
if position == -1:
return 0
else:
if len(message[position:]) == 0:
return position
else:
return position + number_of_occurences(c, message[position:])
number_of_occurences('a', 'azertya')
But when I run it I get this error:
Traceback (most recent call last):
File "frequency_analysis.py", line 31, in <module>
number_of_occurences('a', 'azertya')
File "file_name.py", line 29, in number_of_occurences
return position + number_of_occurences(c, message[position:])
...
...
...
File "file_name.py", line 29, in number_of_occurences
return position + number_of_occurences(c, message[position:])
RuntimeError: maximum recursion depth exceeded
And I know about this similar question but it didn't help, it took longer time but gives the same error for this:
sys.setrecursionlimit(10000)
And this:
sys.setrecursionlimit(30000)
But for this:
sys.setrecursionlimit(50000)
it gives this error:
Segmentation fault (core dumped)
What am I doing wrong here? thanks in advance.
update:
Thanks to @abarnet here is the correct code:
def number_of_occurences(c, message):
position = message.find(c)
nbr = 0.0
if position == -1:
return 0
else:
nbr += 1
if len(message[position:]) == 0:
return nbr
else:
return nbr + number_of_occurences(c, message[position + 1:])