From 587fee299f3678aa71f4a395474a277b00673822 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 2 Dec 2025 09:30:40 +0100 Subject: [PATCH] chop string via division --- src/tools/types.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/tools/types.py b/src/tools/types.py index ddd56ba..6fa50a5 100644 --- a/src/tools/types.py +++ b/src/tools/types.py @@ -1,5 +1,6 @@ from __future__ import annotations from collections.abc import Iterable +from math import ceil class Integer(int): @@ -33,6 +34,19 @@ class String(str): def __getitem__(self, item) -> String: return String(super().__getitem__(item)) + def __floordiv__(self, other: int) -> list[String]: + d = ceil(len(self) / other) + return [self[i * d:i * d + d] for i in range(other)] + + + def __truediv__(self, other: int) -> list[String]: + if len(self) % other != 0: + raise ValueError("String length is Not a multiple of {}".format(other)) + + d = len(self) // other + return [self[i * d:i * d + d] for i in range(other)] + + class List(list): def moving_window(self, size: int = 2) -> Iterable[List]: