advent22/api/advent22_api/routers/_misc.py

21 lines
511 B
Python
Raw Normal View History

2022-10-14 22:47:16 +00:00
import itertools
import random
from typing import Sequence
from ..dav_common import dav_get_file
async def get_loesungswort() -> str:
fp = await dav_get_file("loesungswort.txt")
return fp.read().decode("utf8").strip()
async def shuffle(seq: Sequence, length: int | None = None) -> list:
rnd = random.Random(await get_loesungswort())
if length is not None:
infinity = itertools.cycle(seq)
seq = list(itertools.islice(infinity, length))
return rnd.sample(seq, len(seq))