Challenge: Iterating Over a Custom Range
Oppgave
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 acceptstartandstopparameters and store them as attributes. - The iterator should yield the next odd number greater than or equal to
startand less thanstopeach time__next__is called. - Once all odd numbers in the range have been yielded, the iterator should raise
StopIteration.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 5
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 6.67
Challenge: Iterating Over a Custom Range
Sveip for å vise menyen
Oppgave
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 acceptstartandstopparameters and store them as attributes. - The iterator should yield the next odd number greater than or equal to
startand less thanstopeach time__next__is called. - Once all odd numbers in the range have been yielded, the iterator should raise
StopIteration.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 5
single