Back to Freecodecamp

Challenge 175: Digital Detox

curriculum/challenges/english/blocks/daily-coding-challenges-python/69738771fb5a7b8b24cca2a1.md

latest3.3 KB
Original Source

--description--

Given an array of your login logs, determine whether you have met your digital detox goal.

Each log is a string in the format "YYYY-MM-DD HH:mm:ss".

You have met your digital detox goal if both of the following statements are true:

  • You logged in no more than once within any four-hour period.
  • You logged in no more than 2 times on any single day.

--hints--

digital_detox(["2026-02-01 08:00:00", "2026-02-01 12:30:00"]) should return True.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(digital_detox(["2026-02-01 08:00:00", "2026-02-01 12:30:00"]), True)`)
}})

digital_detox(["2026-02-01 04:00:00", "2026-02-01 07:30:00"]) should return False.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(digital_detox(["2026-02-01 04:00:00", "2026-02-01 07:30:00"]), False)`)
}})

digital_detox(["2026-01-31 08:21:30", "2026-01-31 14:30:00", "2026-02-01 08:00:00", "2026-02-01 12:30:00"]) should return True.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(digital_detox(["2026-01-31 08:21:30", "2026-01-31 14:30:00", "2026-02-01 08:00:00", "2026-02-01 12:30:00"]), True)`)
}})

digital_detox(["2026-01-31 10:40:21", "2026-01-31 15:19:41", "2026-01-31 21:49:50", "2026-02-01 09:30:00"]) should return False.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(digital_detox(["2026-01-31 10:40:21", "2026-01-31 15:19:41", "2026-01-31 21:49:50", "2026-02-01 09:30:00"]), False)`)
}})

digital_detox(["2026-02-05 10:00:00", "2026-02-01 09:00:00", "2026-02-03 22:15:00", "2026-02-02 12:10:00", "2026-02-02 07:15:00", "2026-02-04 09:45:00", "2026-02-01 16:50:00", "2026-02-03 09:30:00"]) should return True.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(digital_detox(["2026-02-05 10:00:00", "2026-02-01 09:00:00", "2026-02-03 22:15:00", "2026-02-02 12:10:00", "2026-02-02 07:15:00", "2026-02-04 09:45:00", "2026-02-01 16:50:00", "2026-02-03 09:30:00"]), True)`)
}})

digital_detox(["2026-02-05 10:00:00", "2026-02-01 09:00:00", "2026-02-03 22:15:00", "2026-02-02 12:10:00", "2026-02-02 07:15:00", "2026-02-04 01:45:00", "2026-02-01 16:50:00", "2026-02-03 09:30:00"]) should return False.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(digital_detox(["2026-02-05 10:00:00", "2026-02-01 09:00:00", "2026-02-03 22:15:00", "2026-02-02 12:10:00", "2026-02-02 07:15:00", "2026-02-04 01:45:00", "2026-02-01 16:50:00", "2026-02-03 09:30:00"]), False)`)
}})

--seed--

--seed-contents--

py
def digital_detox(logs):

    return logs

--solutions--

py
from datetime import datetime

def digital_detox(logs):
    if not logs:
        return True

    times = sorted(
        datetime.strptime(log, "%Y-%m-%d %H:%M:%S")
        for log in logs
    )

    FOUR_HOURS = 4 * 60 * 60

    for i in range(1, len(times)):
        diff = (times[i] - times[i - 1]).total_seconds()
        if diff < FOUR_HOURS:
            return False

    daily_counts = {}
    for time in times:
        day = time.date()
        daily_counts[day] = daily_counts.get(day, 0) + 1
        if daily_counts[day] > 2:
            return False

    return True