File size: 1,189 Bytes
2eaa47e
 
 
 
 
 
 
 
 
abcfc87
 
997fc26
2eaa47e
 
 
abcfc87
2eaa47e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Centralized egg timing logic for the egg timer app."""

from __future__ import annotations

from typing import Dict


# Base cook times in seconds for each doneness target.
DONENESS_BASE_SECONDS: Dict[str, int] = {
    "流心": 6.5 * 60,
    "8成熟": int(7 * 60),
    "全熟": int(10.5 * 60),
}

# Add a small penalty for each extra egg after the first.
SECONDS_PER_EXTRA_EGG = 10


def calculate_cook_seconds(egg_count: int, doneness: str) -> int:
    """Return total cook time in seconds based on eggs and doneness."""
    if egg_count < 1:
        raise ValueError("egg_count must be at least 1")

    if doneness not in DONENESS_BASE_SECONDS:
        valid = ", ".join(DONENESS_BASE_SECONDS.keys())
        raise ValueError(f"doneness must be one of: {valid}")

    base_seconds = DONENESS_BASE_SECONDS[doneness]
    extra_eggs = max(0, egg_count - 1)
    adjustment = extra_eggs * SECONDS_PER_EXTRA_EGG
    return base_seconds + adjustment


def format_mm_ss(total_seconds: int) -> str:
    """Format seconds as MM:SS string."""
    if total_seconds < 0:
        total_seconds = 0
    minutes, seconds = divmod(int(total_seconds), 60)
    return f"{minutes:02d}:{seconds:02d}"