#!/usr/bin/env python3 # -*- coding: utf-8 -*- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # IMPORTS # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ from local.maths import *; from local.typing import *; from code.core.log import *; from code.algorithms.methods import *; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # GLOBAL VARIABLES/CONSTANTS # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # CHECKS # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def preChecks(L: List[int], **_): assert len(L) > 0, 'Liste darf nicht leer sein.'; return; def postChecks(L: List[int], **_): # TODO return; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ALGORITHM max sub sum # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @algorithmInfos(name='MaxSubSum (Maximale Teilsumme)', outputnames=('maxSum', 'index_from', 'index_to'), checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks) def MaxSubSum(L: List[float]) -> Tuple[float, int, int]: ''' Inputs: L = Liste von Zahlen Outputs: - maxSum = Wert der maximalen Summe einer Teilliste aufeinanderfolgender Elemente - u, v = Indexes so dass die Teilliste [L[u], L[u+1], ..., L[v]] die maximale Summe aufweist ''' maxSum: float = 0; u: int = 0; v: int = 0; for i in range(len(L)): for j in range(i+1, len(L)): thisSum = 0; # NOTE: Schleibe über Indexes von von i bis j for k in range(i, j+1): AddToCounter(); thisSum += L[k] if thisSum > maxSum: logDebug('max Teilsumme aktualisiert: Summe L[i] von i={i} bis {j} = {value}'.format( i = i, j = j, value = thisSum )); maxSum, u, v = thisSum, i, j; return maxSum, u, v;