Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Iterating Over a Custom Range | Understanding Iterators
Efficient Data Handling in Python

bookChallenge: Iterating Over a Custom Range

Aufgabe

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.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookChallenge: Iterating Over a Custom Range

Swipe um das Menü anzuzeigen

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
single

single

some-alt