# Kyu 4
"""
Question:
Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, 
in a human-friendly way. The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, 
the duration is expressed as a combination of years, days, hours, minutes and seconds.

It is much easier to understand with an example:
format_duration(62)    # returns "1 minute and 2 seconds"
format_duration(3662)  # returns "1 hour, 1 minute and 2 seconds"


"""
# Method 1
def format_duration(seconds):
    if seconds == 0:
        return "now"
    time = []
    for divisor, unit in zip(
        [31536000, 86400, 3600, 60, 1], ["year", "day", "hour", "minute", "second"]
    ):
        value = seconds // divisor
        seconds = seconds % divisor
        if value == 1:
            time.append(f"{value} {unit}")
        elif value > 1:
            time.append(f"{value} {unit}s")
    if len(time) > 1:
        return ", ".join(time[:-1]) + " and " + time[-1]
    return time[0]


# Method 2
def format_duration(seconds):
    if seconds == 0:
        return "now"

    y, d, h = 3600 * 24 * 365, 3600 * 24, 3600
    year = seconds // y
    day = (seconds % y) // d
    hour = (seconds % d) // h
    minute = (seconds % h) // 60
    sec = seconds % 60
    readable = [
        adjustS(year, "year"),
        adjustS(day, "day"),
        adjustS(hour, "hour"),
        adjustS(minute, "minute"),
        adjustS(sec, "second"),
    ]
    readable = list(filter(None, readable))
    if len(readable) > 1:
        return ", ".join(readable[:-1]) + " and " + readable[-1]
    return readable[0]


def adjustS(val, unit):
    if val == 0:
        return ""
    if val == 1:
        return f"{val} {unit}"
    return f"{val} {unit}s"


print(format_duration(86462))
