Python Coding Problem: Creating Your Own Iterators

preview_player
Показать описание
In this Python Coding Problem, we will be creating our own iterators from scratch. First, we will create an iterator using a class. Then we will create an iterator with the same functionality using a generator. If you haven't watched the tutorial video on Iterators and Iterables then I would suggest watching that first...

Iterators and Iterables Tutorial:

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:

#Python
Рекомендации по теме
Комментарии
Автор

There are usually multiple solutions to problems like these. If your solution is different than the ones I gave here then it is not necessarily wrong. For example, it would have been possible to use a try/except block in the next method as well. If you have different solutions that work then I would love to see them.

Also, quick correction: At 3:05 I said "class attribute" when really I meant "instance attribute".

coreyms
Автор

Corey, I am just amazed at how you deliver a complex concept so easily - I actually meant the word "deliver". You actually do that so good brother. Lucky to have an instructor like you on YouTube. Thank you so much for your great stuff.

umaraftab
Автор

HI Corey, these coding challenges are a great idea - great way to get us thinkin'.
I hope you make these a regular feature : )

geoptus
Автор

Dude you MUST keep this up! We seriously need examples on how to use all of the functions and statements etc, we've learned into solving common problems. So thank you for this🙏.

apollossevere
Автор

Coding challenges are even better than the tutorials

Skaxarrat
Автор

Hi Corey,
Please keep posting coding problems. These are great for practicing and solidifying one's understanding of the concepts you teach.
Is there a reason you store both the original string and the list in the class object? Wouldn't the memory requirements be more? It seems to me that storing only a list would be sufficient (at least my solution works with just a list).

deepdabbler
Автор

These videos are better than theoritical concepts. It gives us insight into real world problems. Keep up the good work.

tradingguru
Автор

def my_sentence_gen(str):
words=str.split()
index=0
while index<len(words):
i=index
index+=1
yield words[i]

iriswang
Автор

Please, continue to make this type of 'coding challenge' videos. They' re essentials in my opinion.

jipeejoce
Автор

Thanks so much for effort you are the best. This is my solution for generator
def sentence(sentence):
words = sentence.split()
index = 0
while index < len(words):
yield words[index]
index += 1

mohamedgomaa
Автор

Didn't think you could change creating an instance so did

def __iter__(self):
while self.index<len(self.words):
yield self.words[self.index]
self.index +=1

Just as an exercise really as then you don't need to override __next__. These videos are great by-the-way!

amolparnaik
Автор

Hi Corey.. These coding problems are great. learnt a lot. Please continue them.

manjusvijaykumar
Автор

A video tutorial series on 'Data structures and Algorithms' would be great. I know this might take up a lot of time, but almost every interview has questions from DSA. Your tutorial series on Django helped me a lot at my current project. Thanks.

prateekjha
Автор

Exercises like this one help me learn quicker. Your comment about using generator more frequently also provides a valuable reference as I learn Python. I'd like to see more coding exercises please.

andymao
Автор

Simply the best Python channel on youtube :D Thanks Corey!

riskzerobeatz
Автор

Genuine Query: Wouldn't using sentence.split() defeat the whole purpose of using a generator in the first place?

Per my understanding, generators are used because of two main reasons. First, they do not require additional storage. By using the split() method, I would argue that an array is being created that occupies O(n) space where n is the no. of words. Second, generators are better because they eagerly return/yield results when the actual computation is lengthy. By using split, we are already traversing the entire sentence. Please clarify how this implementation that uses generators, is helpful.

Corey, if you are reading this, I can't thank you enough for your videos! They have helped me immensely and I often frequent them when I need to jog my memory on a particular topic.

Thanks!

manavshah
Автор

Hi Corey,

I thoroughly enjoyed this coding problem. I found it to be challenging but not overwhelming and the content in the previous tutorial video was just helpful enough. You're the best teacher I've ever had in any subject. Thank you much!

mylesmontclair
Автор

Coding problem seems to be very interesting like all videos of yours. Please continue this series.

ghanteyyy
Автор

I just want to save I love your calm voice and easy-to-understand explanation, no 'uhh' or 'err' in your explanation that can interrupt my focus while listening to your explanation <3

RealToriMan
Автор

I definitely enjoyed this format! Splitting theory and practice into two videos makes it very easy and convenient to revisit the desired part. Even though I like finding a solution on my own to coding challenges, I really like to compare it to what an experienced programmer would do to find out where improvements could be made. Also, on more complex problems, it really helps to see if the structure of the program is correct and if something is missing (exception, test case, etc.). All in all, I love all your videos and this one makes no exception, except on StopIteration ;).

slavoie