Exercise: Generators
Questions for: Generators
How does the
itertools.compress() function differ from itertools.filterfalse() when used with generators?
A:
They are equivalent in functionality
B:
itertools.compress() filters elements based on a true condition, while itertools.filterfalse() filters based on a false condition
C:
itertools.filterfalse() filters elements based on a true condition, while itertools.compress() filters based on a false condition
D:
itertools.compress() raises a ValueError if the selectors iterable is shorter than the data iterable
Answer: B
itertools.compress() filters elements from the data iterable based on corresponding truth values in the selectors iterable. itertools.filterfalse() filters elements based on a false condition.
How does the
itertools.dropwhile() function differ from itertools.filterfalse() when used with generators?
A:
They are equivalent in functionality
B:
itertools.dropwhile() skips elements until a specified condition becomes false, while itertools.filterfalse() filters elements based on a false condition
C:
itertools.filterfalse() skips elements until a specified condition becomes false, while itertools.dropwhile() filters elements based on a false condition
D:
itertools.dropwhile() and itertools.filterfalse() both raise a ValueError
Answer: B
itertools.dropwhile() skips elements until a specified condition becomes false, while itertools.filterfalse() filters elements based on a false condition.Discuss About this Question.
What is the purpose of the
generator.send(value) method when used with generators?
A:
It sends a value to the generator and resumes its execution
B:
It stops the generator and raises a
StopIteration exception
C:
It raises a
GeneratorExit exception
D:
It has no effect on the running generator
Answer: A
generator.send(value) sends a value into the generator, which is received as the result of the current yield expression, and resumes the generator's execution.
-PROGCODE-
def my_generator():
result = yield
print(f"Received value: {result}")
gen = my_generator()
next(gen) # Start the generator
gen.send(42) # Output: Received value: 42
-PROGCODE-END-Discuss About this Question.
How does the
itertools.groupby() function differ from collections.Counter() when used with generators?
A:
They are equivalent in functionality
B:
itertools.groupby() groups consecutive elements based on a key function, while collections.Counter() counts occurrences of each element
C:
collections.Counter() groups consecutive elements based on a key function, while itertools.groupby() counts occurrences of each element
D:
itertools.groupby() and collections.Counter() both raise a ValueError
Answer: B
itertools.groupby() groups consecutive elements based on a key function, creating subgroups. collections.Counter() counts the occurrences of each element in the entire iterable.Discuss About this Question.
How does the
itertools.cycle() function differ from using itertools.repeat() with a specified count when used with generators?
A:
They are equivalent in functionality
B:
itertools.cycle() repeats the elements of a generator indefinitely, while itertools.repeat() stops after a specified count
C:
itertools.repeat() repeats the elements of a generator indefinitely, while itertools.cycle() stops after a specified count
D:
itertools.cycle() raises a ValueError when used with generators
Answer: B
itertools.cycle() repeats the elements of a generator indefinitely, while itertools.repeat() stops after a specified count.
-PROGCODE-
import itertools
gen = (1, 2, 3)
cycle_iterable = itertools.cycle(gen)
repeat_iterable = itertools.repeat(gen, times=3)
print(list(itertools.islice(cycle_iterable, 10))) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
print(list(repeat_iterable)) # Output: [(1, 2, 3), (1, 2, 3), (1, 2, 3)]
-PROGCODE-END-Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.