Algorithms in Python: Recursion -- Count Consonants in String

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

Given a string, calculate the number of consonants present using recursion.

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:
Рекомендации по теме
Комментарии
Автор

your videos are awesome by the way, very simple and crystal clear to understand :)

DilipKumar-iccg
Автор

Thanks! I'm glad you are using Linux Mint and vim. How long have you've been working with python?

inteliconn
Автор

thank you very much sir, now i understand recursive functions

joorah
Автор

1) is the below a correct recursive solution?
2) Also, what would the runtime be? I'm thinking O(n)?
Thank you, super helpful vid and playlist!!

def cc2(s):
if s == '':
return 0
if s[0].lower() in vowels or not s[0].isalpha():
return cc2(s[1:])
return 1 + cc2(s[1:])

elisad
Автор

Hi. One thing is not exactly clear for me: how does the slicing work? We ask for string[1:] i.e. for the slice from the second character in the string till the end of the string. And how the heck, does the index "climb" up the string when it is a constant: 1 and not an incremented value?
Let me be precise: string[1:] (which I don't get) and not str[i +1:] (which would make a perfect sens for me).

kudlok
Автор

You Sir, have saved my life. I'm new to computer science. Thank you very much. Please how can I support?

BigHny
Автор

one question, it may be stupid or I'm missing something,
Q. The validation for empty string, if the passed string is null it should return 0. So, during the iteration where the function is called recursively there is a stage when the passed string has nothing, why doesn't it return 0?

DilipKumar-iccg
Автор

vowels = "aeiou"
def find_consonant(data):
count = 0
for x in data.lower():
if x not in vowels and x.isalpha():
count += 1
return count

footballnationcr
Автор

How does this line of code work Plz reply. Thank you sir

khaihoang
Автор

for c in input_string is more elegant than using an index in my opinion

schogaia
Автор

1) is the below a correct recursive solution?
2) Also, what would the runtime be? I'm thinking O(n)?
Thank you, super helpful vid and playlist!!

def cc2(s):
if s == '':
return 0
if s[0].lower() in vowels or not s[0].isalpha():
return cc2(s[1:])
return 1 + cc2(s[1:])

elisad
welcome to shbcf.ru