chop string via division
This commit is contained in:
parent
105ab06cd9
commit
587fee299f
@ -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]:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user