advent22/api/advent22_api/routers/_misc.py

27 lines
633 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()
2022-10-14 23:03:36 +00:00
async def set_length(seq: Sequence, length: int) -> list:
2022-10-14 23:20:35 +00:00
# `seq` unendlich wiederholen
2022-10-14 23:03:36 +00:00
infinite = itertools.cycle(seq)
2022-10-14 23:20:35 +00:00
# Die ersten `length` einträge nehmen
2022-10-14 23:03:36 +00:00
return list(itertools.islice(infinite, length))
2022-10-14 22:47:16 +00:00
2022-10-14 23:03:36 +00:00
async def shuffle(seq: Sequence) -> list:
2022-10-14 23:20:35 +00:00
# Zufallsgenerator
2022-10-14 23:03:36 +00:00
rnd = random.Random(await get_loesungswort())
2022-10-14 23:20:35 +00:00
# Elemente mischen
2022-10-14 22:47:16 +00:00
return rnd.sample(seq, len(seq))