Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Stateful Running Average Generator | Advanced Generator Patterns and Applications
Quizzes & Challenges
Quizzes
Challenges
/
Efficient Data Handling in Python

bookChallenge: Stateful Running Average Generator

Task

Swipe to start coding

Create a generator function running_average() that maintains a running average of numbers sent to it using the send() method. Each time a new number is sent, the generator should yield the updated average.

  • Update the running total and count each time a new value is sent.
  • Compute the average as a float after each new value is received.
  • Yield the current average after processing each new value.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

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: Stateful Running Average Generator

Swipe to show menu

Task

Swipe to start coding

Create a generator function running_average() that maintains a running average of numbers sent to it using the send() method. Each time a new number is sent, the generator should yield the updated average.

  • Update the running total and count each time a new value is sent.
  • Compute the average as a float after each new value is received.
  • Yield the current average after processing each new value.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 4
single

single

some-alt