avatar Bite 139. Calculate a coding streak in days

In this Bite you are going to calculate the amount of days you are on a coding streak (consecutive days coded. You are presented with this DB table like output of date/activity/count:

data = """
    +------------+------------+---------+
    | date       | activity   | count   |
    |------------+------------+---------|
    | 2018-11-11 | pcc        | 1       |
    | 2018-11-10 | 100d       | 1       |
    | 2018-11-09 | 100d       | 2       |
    | 2018-10-23 | pcc        | 1       |
    | 2018-10-15 | pcc        | 1       |
    | 2018-10-05 | bite       | 1       |
    | 2018-09-21 | bite       | 4       |
    | 2018-09-18 | bite       | 2       |
    | 2018-09-18 | bite       | 4       |
    +------------+------------+---------+
"""

Here is what you need to do:

  • Complete extract_dates to extract date (not datetime) objects.
  • Complete calculate_streak that takes those date objects and returns the streak in (int) days.
  • Calculate back from TODAY that is set to date(2018, 11, 12). Note that a streak can be > 0 if today or yesterday is in the date range. This is because we don't assume today to be over yet, so if yesterday was coded that is a valid continuation of a streak. If today was coded that counts towards the streak too of course.

Have fun and keep calm and code in Python. Join our Slack (via Settings) to learn and share more Python with other passionate Pythonistas.

Login and get coding
go back Advanced level
Bitecoin 4X

127 out of 131 users completed this Bite.
Will you be Pythonista #128 to crack this Bite?
Resolution time: ~65 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 6.0 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help