Spooky Sale: 10% off Premium yearly · $118.80 for the first year · until Nov 6Get it

Deep-ML
Learn
Problems1,200+ hands-on ML problemsLearning PathA guided route through the fundamentalsLabsInteractive labs on real techniquesProjectsBuild a GPT, an RL agent, CUDA kernelsCollectionsCurated problem sets and videosMathPen-and-paper math for MLIntuitionInvent the idea behind the formula yourselfPlaylistsYour own problem lists, shareableStudy AssistantAn AI teacher that knows your progress
Interview Prep
Compete
ContestsFresh problem sets, ranked liveLeaderboardWhere you stand, globallyBuddiesSolve with a friend: streaks, a weekly board, chat
Discuss
Deep-Path
Premium
      Intuition
      6 of 102

      Average on the go

      Values arrive one at a time (star reviews on an app, sensor readings, rewards, losses) and you can’t keep them all. Write update(average, count, new_value): the new average after one more value arrives, from the old average and count, how many values that average was over. At the start nothing has arrived: average and count are both 0.

      A food app shows each place’s rating. It doesn’t keep the reviews: each one is counted into the rating and thrown away, so a card only remembers two numbers, the rating and how many reviews it has. Then a 1★ review arrives at a busy restaurant and at a brand-new one.

      Luigi’s4.3★★★★★★★★★★1,200 reviewsTaco Spot (new)5★★★★★★★★★★1 reviewAll each card has: its rating, its number of reviews, and the new 1★.What’s each new rating? ?
      Your functionupdate(average, count, new_value)
      averagethe average so far, over `count` values (both 0 at the start)e.g. 0
      counthow many values that average was over (0 at the start)e.g. 0
      new_valuethe value that just arrivede.g. 4
      returnsthe new average, without keeping the old values
      solution.pyLoading Python
      Sign in for an AI hint
      ← Average speedAs the crow flies →