import re from typing import Optional import math def format(minutes: int): if minutes >= 60 * 24: return '' + str(math.floor(minutes / 60 / 24)) + 'd' elif minutes >= 60: return '' + str(math.floor(minutes / 60)) + 'h' elif minutes > 0: return '' + str(minutes) + 'm' else: return '' def parse(duration: str) -> Optional[int]: duration = duration.strip() if duration: result = re.match('^(\d+)([mhd])$', duration.strip()) if result: n = int(result.group(1)) unit = result.group(2) if unit == 'm': return n elif unit == 'h': return n * 60 elif unit == 'd': return n * 60 * 24 else: return None else: return 0