simfile-scripts/docs/get-the-hardest-chart.py
2022-08-20 18:02:25 -07:00

25 lines
711 B
Python

from typing import Optional, Sequence
from simfile.types import Chart
# Imperative version
def get_hardest_chart(charts) -> Optional[Chart]:
hardest_chart: Optional[Chart] = None
hardest_meter: Optional[int] = None
for chart in charts:
# Remember to convert `meter` to an integer for comparisons
meter = int(chart.meter or "1")
if hardest_meter is None or meter > hardest_meter:
hardest_chart = chart
hardest_meter = meter
return hardest_chart
# One-liner version
def get_hardest_chart(charts: Sequence[Chart]) -> Optional[Chart]:
return max(
charts,
key=lambda chart: int(chart.meter or "1"),
default=None,
)