…
…
From four weeks of daily step data, write functions that find goal days, the weekly average, the best day, and the longest streak of goal days; draw a bar chart with `#` characters and tie everything into one monthly report.
In the first year of university, Alex set a goal: 8000 steps every day. The step counter on the phone has been counting for four weeks, but the app only shows the current day. Alex put all the data into a list and wants a program that answers these questions: How many days did I reach the goal? Am I improving from week to week? Which day was my best? What is the longest run of goal days in a row?
At the end of the task, your program will print this report:
OutputALEX'S STEP REPORT (goal: 8000 steps)
--------------------------------------------------------
week 1 | avg 7113 | 4/7 days on goal | best Sat (9760)
week 2 | avg 6364 | 2/7 days on goal | best Sat (9410)
week 3 | avg 8111 | 4/7 days on goal | best Tue (10480)
week 4 | avg 9520 | 6/7 days on goal | best Sat (12240)
--------------------------------------------------------
Month: 217760 steps | avg 7777 | longest streak 4 daysAlong the way you will also draw a bar chart from # characters:
OutputMon ######## 8120 ✓
Fri ####### 7470
Sat ############ 12240 ✓Lessons used: 3 (//, round), 4 (multiplying a string by a number), 7 (if/else), 10 (for and range), 12 (counter, total, and largest patterns), 13 (nested loops), 14-16 (functions, return, parameters with default values, splitting a program into parts), 22 (alignment with f-strings), 23-25 (lists, append, sum/len/max, index, nested lists).
How to work: The seven cells build on each other: in each cell you write a function, the next cells use it, and the last cell ties everything into one report. In each cell the comments tell you what to do, and you write the code where it says ___. Run the cell, compare your output with the "Show expected output" box, then press Check. Check also tries your functions on other lists besides Alex's data (an empty list, a day exactly equal to the goal, a streak that runs to the end of the list); being right only on this data is not enough.
What you'll do
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Alex sent you the four weeks of step counter data from the phone: each element of the weeks list is one week, with seven days from Monday to Sunday (a nested list, lesson 25). The daily goal is 8000 steps.
The function goal_day_count(steps, goal) should count the days in a list whose value is at least the goal and return the count (the counter pattern, lesson 12). The function should look at the goal parameter, not the GOAL constant, so it also works with a different goal.
The loop below it is ready: for each week it calls the function and prints one line.
This step's code cell is for members.Sign up free
What Alex really wants to know is whether things improve from week to week. average(steps) should round the average of a list of days to an integer with round and return it. The list summaries sum and len (lesson 24) give the average in one line.
Then, inside the loop, find each week's average with this function and assign it to the name
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
avgThis step's code cell is for members.Sign up free
max gives the largest value, but Alex is asking which day it was. best_day(steps) should return the index of the day with the most steps (0 = Monday). If two days are tied, the first one wins.
You have two ways: find the value with max and ask for its position with list.index(...) (lesson 24), or build the largest pattern (lesson 12) with an index: keep the index of the best so far and update it when you see a bigger one. The print line uses the index in the days list.
This step's code cell is for members.Sign up free
Looking week by week is useful, but questions like streaks go beyond one week: a streak that runs from Sunday into Monday must not break. So collect the 28 days of the four weeks, in order, into a single list: all_days.
You can flatten a nested list with nested loops (lesson 13) and append, or by adding each week with extend (lesson 24). Then apply the functions you wrote to the whole month this time: total, average, and number of days on goal. This is the payoff of writing functions: you do not rewrite the same code.
This step's code cell is for members.Sign up free
Step counter apps love streaks: how many days in a row did you reach the goal? One day below the goal resets the streak to zero.
Write two functions:
longest_streak(steps, goal): the number of days in the longest streak in the month. You need two accumulators: the current streak and the longest streak so far (lesson 12).current_streak(steps, goal): the length of the streak that is still going at the end of the list. If the last day is below the goal, it is 0.Example: for [9000, 9000, 100, 9000] the longest streak is 2 and the current streak is 1.
This step's code cell is for members.Sign up free
When numbers are listed one under another, the difference is hard to see; a bar chart shows it at once. You will draw it with # characters, without a chart library: multiplying a string by a number repeats it ("#" * 3 → "###", lesson 4).
bar(steps, scale=1000) should return a string with one # for every scale steps: 8120 steps → 8 # characters. scale is a parameter with a default value (lesson 15); if it is not given, it is 1000.
chart_line(day, steps, goal) should return one line of the chart: the day name, the bar aligned to 13 characters with :<13 (lesson 22), and the number of steps; if the steps reached the goal, add ✓ at the end. The loop below it draws week 4:
OutputMon ######## 8120 ✓
Fri ####### 7470This step's code cell is for members.Sign up free
In the last step you put the parts together. week_report(number, steps, goal) should return a summary line for one week (not print it; the calling code does the printing):
Outputweek 1 | avg 7113 | 4/7 days on goal | best Sat (9760)For each part of this line you have already written a function: average, goal_day_count, best_day. Now comes the payoff of splitting a program into parts (lesson 16): the report function is a few lines of glue.
Add the month summary to the end of the report: total steps, daily average, and longest streak. Alex wants to run the program on a friend's data too; Check will try your function with another week as well.
This step's code cell is for members.Sign up free