Algorithms in Python: Recursion -- Calculate String Length

preview_player
Показать описание
In this video, we focus on the following problem:

Given a string, calculate its length recursively.

If you are preparing for an interview or trying to understand the notion of recursion to solve a problem, I hope this lesson is helpful to you.

As always, please do not hesitate to ask questions in the comments!

The software written in this video is available at:

Do you like the development environment I'm using in this video? It's a customized version of vim that's enhanced for Python development. If you want to see how I set up my vim, I have a series on this here:

If you've found this video helpful and want to stay up-to-date with the latest videos posted on this channel, please subscribe:
Рекомендации по теме
Комментарии
Автор

In the iterative approach, why did you use len module "for in range(len(input_str)):".
we should find the length of the string without using len module right?
let me know, how can we do without using len module?

vishnunandyala
Автор

my iterative solution:

def length_of_string(s):

count = 0

for i in list(s):
count += 1
return count

theguildedcage
Автор

def calculate_len(string):
if string == " ":
return 0
return 1 + calculate_len(string[1:])

I keep getting the Max recursive depth exceeded error, i cant understand why?? The code is exact same as yours :(

[[ EDIT : OMG, all this time i was checking empty string as " " . Removing the spaces fixed the issue, how did i never notice that i was adding extra space in middle :/ ]]
[[ KEEPING THIS COMMENT BECAUSE IT MIGHT BE USEFUL TO SOMEONE ELSE ]]

webarelytravel
Автор

Would be good to have some bench marking to see the difference in times between the two functions.

trentt
Автор

I have done some recursive tutorials and written a few functions my self. But i don't understand two things:
1. in this case how is it allowed to add " + 1" which is an - int type - to a string. Yes - i understand that all the ones get saved and add up - and i understand that because i can use that technique for summing numbers etc but we are working with a string here. Suppose you replaced the "return" with "print" so that your line reads
print( 1+ ) you get and error. Not allowed to add strings to integers. So why does it work in the recursive function?
2. I have written very similar code in an edabit exercise for the same problem. if add a print statement before the return 0 i see that eventually the 0 gets returned. yet in this code or mine the length of the string is returned but not Zero. Why is the that the zero does not show up in the shell our program output when it in fact does get returned at some point?

if anyone doesnt actually know and the best you can say is: "thats just how the recursive function works" that is better than making something up.

natem
Автор

My Iterative solution,

def str_len_iter(str):
count = 0
for i in str:
if str:
count += 1
return count

samarjitdebnath
Автор

Hi,
In the Recussive approach i have a Doubt.
return 1+Recurssive_funct(Data[1:]) >>This means "ucid Programming"
How the Function will know to read the next coming character- u then c then i then d then space then P and so On ? I meant how will the Function read the next Coming Characters from the String without using any counter

Indian-fhhf
Автор

Doesn't 1+Recurssive_funct(Data[1:]) look like 1ucidProgramming?

How does the number of return grow since it is not a variable?

TheCommonCondition
Автор

using len function for calculating len is not making any sense
plz tell us some other method

ashishkhar
Автор

"find a length of the string" by using len() inside ? Doesn't make sense!

why not like this?

def sl(s, cnt=0):
if not s:
return cnt
else:
cnt += 1
return sl(s[1:], cnt)

idopshik
visit shbcf.ru