HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Calculating how many Pomodori fit into a period of time

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
howperiodintotimecalculatingpomodorimanyfit

Problem

I've written some code to calculate how many Pomodori can fit into a period of time, including long and short breaks. I'm looking for alternative solutions because what I have at the moment doesn't feel right. I think I'm missing something.

A Pomodoro is an interval of time 25 minutes long and after each one there should be a short 5 minute break. But every 4x Pomodori should be followed by a long 15 minutes break. So we have the following:

╔═══════╦════════╦═════════════╗
║ Time  ║ Length ║ Activity    ║
╠═══════╬════════╬═════════════╣
║ 12:00 ║ 25:00  ║ Pomodoro    ║
║ 12:25 ║ 05:00  ║ Short Break ║
║ 12:30 ║ 25:00  ║ Pomodoro    ║
║ 12:55 ║ 05:00  ║ Short Break ║
║ 13:00 ║ 25:00  ║ Pomodoro    ║
║ 13:25 ║ 05:00  ║ Short Break ║
║ 13:30 ║ 25:00  ║ Pomodoro    ║
║ 13:55 ║ 15:00  ║ Long Break  ║
║ 14:10 ║ 25:00  ║ Pomodoro    ║
╚═══════╩════════╩═════════════╝


At the moment I use a loop to populate a list and then delete the last element if it's not a Pomodoro, as there's no point in ending the list of Pomodori with a break.

pomodori = []
iterations = 0
seconds = 3 * 60 * 60

while seconds > 0:
    # zero and even numbers are always Pomodori
    if iterations % 2 == 0 or iterations == 0:
        pomodori.append('pomodoro')
        seconds -= 25 * 60
    else:
        quotient, remainder = divmod(iterations+1, 4)

        # if the quotient is even and the remainder is zero, then we
        # are just after a 4x Pomodori and should add a long break
        if quotient % 2 == 0 and remainder == 0:
            pomodori.append('long-break')
            seconds -= 15 * 60
        # otherwise, we're at a short break
        else:
            pomodori.append('short-break')
            seconds -= 5 * 60

    iterations += 1

# remove breaks that are not followed by a Pomodoro
if pomodori[-1] != 'pomodoro':
    del pomodori[-1]


EDIT:

I've gone ahead with jonrsharpe's solution. The code is here for those interested.

Solution

I think it would be simpler just to apply some ingenuity and partially unroll the loop. While it's possible to fit in a long cycle (pomodoro, short break, pomodoro, short break, pomodoro, short break, pomodoro, long break) with time left over, then do that. While it's possible to fit in a short cycle (pomodoro, short break) with time left over, do that.

I'd also define some constants for readability.

pomodori is not quite an appropriate name for the result, since it includes pomodori and breaks.

It would be a good habit to encapsulate your code in a function.

SECOND = SECONDS = 1
MINUTE = MINUTES = 60 * SECONDS
HOUR = HOURS = 60 * MINUTES

POMODORO = 25 * MINUTES
SHORT_BREAK = 5 * MINUTES
LONG_BREAK = 15 * MINUTES

def manage(time):
    """ Devise a Pomodoro schedule for a time span, given in seconds. """
    schedule = []
    while time > 4 * POMODORO + 3 * SHORT_BREAK + LONG_BREAK:
        schedule.extend(3 * ['pomodoro', 'short-break'] + ['pomodoro', 'long-break'])
        time -= 4 * POMODORO + 3 * SHORT_BREAK + LONG_BREAK
    while time > POMODORO + SHORT_BREAK:
        schedule.extend(['pomodoro', 'short-break'])
        time -= POMODORO + SHORT_BREAK
    # 0 < time < POMODORO + SHORT_BREAK
    schedule.append('pomodoro')
    return schedule

print(manage(3 * HOURS))

Code Snippets

SECOND = SECONDS = 1
MINUTE = MINUTES = 60 * SECONDS
HOUR = HOURS = 60 * MINUTES

POMODORO = 25 * MINUTES
SHORT_BREAK = 5 * MINUTES
LONG_BREAK = 15 * MINUTES

def manage(time):
    """ Devise a Pomodoro schedule for a time span, given in seconds. """
    schedule = []
    while time > 4 * POMODORO + 3 * SHORT_BREAK + LONG_BREAK:
        schedule.extend(3 * ['pomodoro', 'short-break'] + ['pomodoro', 'long-break'])
        time -= 4 * POMODORO + 3 * SHORT_BREAK + LONG_BREAK
    while time > POMODORO + SHORT_BREAK:
        schedule.extend(['pomodoro', 'short-break'])
        time -= POMODORO + SHORT_BREAK
    # 0 < time < POMODORO + SHORT_BREAK
    schedule.append('pomodoro')
    return schedule

print(manage(3 * HOURS))

Context

StackExchange Code Review Q#53970, answer score: 8

Revisions (0)

No revisions yet.