chop string via division

This commit is contained in:
Stefan Harmuth 2025-12-02 09:30:40 +01:00
parent 105ab06cd9
commit 587fee299f

View File

@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Iterable from collections.abc import Iterable
from math import ceil
class Integer(int): class Integer(int):
@ -33,6 +34,19 @@ class String(str):
def __getitem__(self, item) -> String: def __getitem__(self, item) -> String:
return String(super().__getitem__(item)) 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): class List(list):
def moving_window(self, size: int = 2) -> Iterable[List]: def moving_window(self, size: int = 2) -> Iterable[List]: