mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-11-23 00:03:07 +00:00
shuffle funktion
This commit is contained in:
parent
7fc8f8bbc5
commit
71482983bf
2 changed files with 22 additions and 6 deletions
20
api/advent22_api/routers/_misc.py
Normal file
20
api/advent22_api/routers/_misc.py
Normal 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))
|
|
@ -10,21 +10,17 @@ from PIL import ImageDraw, ImageFont
|
||||||
|
|
||||||
from ..dav_common import dav_get_file, dav_list_files
|
from ..dav_common import dav_get_file, dav_list_files
|
||||||
from ._image import AdventImage
|
from ._image import AdventImage
|
||||||
|
from ._misc import shuffle
|
||||||
|
|
||||||
router = APIRouter(prefix="/days", tags=["days"])
|
router = APIRouter(prefix="/days", tags=["days"])
|
||||||
|
|
||||||
loesungswort = "ABCDEFGHIJKLMNOPQRSTUVWX"
|
loesungswort = "ABCDEFGHIJKLMNOPQRSTUVWX"
|
||||||
|
|
||||||
|
|
||||||
async def shuffle(string: str) -> str:
|
|
||||||
rnd = random.Random(loesungswort)
|
|
||||||
return "".join(rnd.sample(string, len(string)))
|
|
||||||
|
|
||||||
|
|
||||||
@router.on_event("startup")
|
@router.on_event("startup")
|
||||||
async def startup() -> None:
|
async def startup() -> None:
|
||||||
print(loesungswort)
|
print(loesungswort)
|
||||||
print(await shuffle(loesungswort))
|
print("".join(await shuffle(loesungswort)))
|
||||||
|
|
||||||
|
|
||||||
@router.get("/letter/{index}")
|
@router.get("/letter/{index}")
|
||||||
|
|
Loading…
Reference in a new issue