from __future__ import annotations import types from typing import List, Generator class Prefix: # FIXME ‹list_in› should be iterable, not list def __init__( self, list_in: List[ int ] ) -> None: self.slice = 0 self.list = list_in self.lenlist = len( list_in ) def __iter__( self ) -> Prefix: return self def __next__( self ) -> List[ int ]: slice_ = self.slice self.slice += 1 if slice_ > self.lenlist: raise StopIteration return self.list[ 0 : slice_ ] class Sum: def __init__( self, list_in: List[ int ] ) -> None: self.prefix = Prefix( list_in ) next( self.prefix ) def __iter__( self ) -> Sum: return self def __next__( self ) -> int: return sum( next( self.prefix ) ) def prefixes( list_in: List[ int ] ) -> Prefix: return Prefix( list_in ) def prefix_sum( list_in: List[ int ] ) -> Sum: return Sum( list_in ) import run_tests