Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Iterating Over a Custom Range | Understanding Iterators
Quizzes & Challenges
Quizzes
Challenges
/
Efficient Data Handling in Python

bookChallenge: Iterating Over a Custom Range

Завдання

Swipe to start coding

In this task, you will create a class called OddRange that acts as an iterator to yield only odd numbers between a given start and stop value, mimicking the behavior of Python's built-in range but filtering for odd numbers only.

  • The class should implement the iterator protocol by defining both __iter__ and __next__ methods.
  • The __init__ method should accept start and stop parameters and store them as attributes.
  • The iterator should yield the next odd number greater than or equal to start and less than stop each time __next__ is called.
  • Once all odd numbers in the range have been yielded, the iterator should raise StopIteration.

Рішення

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain this in simpler terms?

What are the main benefits or drawbacks?

Can you give me a real-world example?

close

bookChallenge: Iterating Over a Custom Range

Свайпніть щоб показати меню

Завдання

Swipe to start coding

In this task, you will create a class called OddRange that acts as an iterator to yield only odd numbers between a given start and stop value, mimicking the behavior of Python's built-in range but filtering for odd numbers only.

  • The class should implement the iterator protocol by defining both __iter__ and __next__ methods.
  • The __init__ method should accept start and stop parameters and store them as attributes.
  • The iterator should yield the next odd number greater than or equal to start and less than stop each time __next__ is called.
  • Once all odd numbers in the range have been yielded, the iterator should raise StopIteration.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
single

single

some-alt