shuffle funktion

This commit is contained in:
Jörn-Michael Miehe 2022-10-14 22:47:16 +00:00
parent 7fc8f8bbc5
commit 71482983bf
2 changed files with 22 additions and 6 deletions

View file

@ -0,0 +1,20 @@
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))

View file

@ -10,21 +10,17 @@ from PIL import ImageDraw, ImageFont
from ..dav_common import dav_get_file, dav_list_files
from ._image import AdventImage
from ._misc import shuffle
router = APIRouter(prefix="/days", tags=["days"])
loesungswort = "ABCDEFGHIJKLMNOPQRSTUVWX"
async def shuffle(string: str) -> str:
rnd = random.Random(loesungswort)
return "".join(rnd.sample(string, len(string)))
@router.on_event("startup")
async def startup() -> None:
print(loesungswort)
print(await shuffle(loesungswort))
print("".join(await shuffle(loesungswort)))
@router.get("/letter/{index}")