master > master: code - max teilsumme x 2 hinzugefügt
This commit is contained in:
parent
2c94de5854
commit
64df98fcf3
@ -6,3 +6,4 @@
|
|||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
from code.algorithms.search.exports import *;
|
from code.algorithms.search.exports import *;
|
||||||
|
from code.algorithms.sum.exports import *;
|
||||||
|
@ -19,6 +19,9 @@ class OneShot(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.state = True;
|
self.state = True;
|
||||||
|
|
||||||
|
def setState(self, state: bool):
|
||||||
|
self.state = state;
|
||||||
|
|
||||||
def reload(self):
|
def reload(self):
|
||||||
self.state = True;
|
self.state = True;
|
||||||
|
|
||||||
@ -56,7 +59,9 @@ def algorithmInfos(
|
|||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def func_wrapper(**inputs):
|
def func_wrapper(**inputs):
|
||||||
try:
|
try:
|
||||||
state = nonnestedRecursion.state and nonnestedAlgorithms.state;
|
state1 = nonnestedAlgorithms.state;
|
||||||
|
state2 = nonnestedRecursion.state;
|
||||||
|
state = state1 and state2;
|
||||||
if state:
|
if state:
|
||||||
# Initialisierung
|
# Initialisierung
|
||||||
DisplayStartOfAlgorithm(name.title(), **inputs);
|
DisplayStartOfAlgorithm(name.title(), **inputs);
|
||||||
@ -68,8 +73,8 @@ def algorithmInfos(
|
|||||||
nonnestedRecursion.trigger();
|
nonnestedRecursion.trigger();
|
||||||
nonnestedAlgorithms.trigger();
|
nonnestedAlgorithms.trigger();
|
||||||
outputs = func(*[], **inputs);
|
outputs = func(*[], **inputs);
|
||||||
nonnestedAlgorithms.reload();
|
nonnestedAlgorithms.state = state1;
|
||||||
nonnestedRecursion.reload();
|
nonnestedRecursion.state = state2;
|
||||||
if state:
|
if state:
|
||||||
# benenne Outputs:
|
# benenne Outputs:
|
||||||
outputs_ = outputs if isinstance(outputs, tuple) else tuple([outputs]);
|
outputs_ = outputs if isinstance(outputs, tuple) else tuple([outputs]);
|
||||||
@ -83,7 +88,8 @@ def algorithmInfos(
|
|||||||
DisplayMetrics();
|
DisplayMetrics();
|
||||||
DisplayEndOfAlgorithm(**outputsNamed);
|
DisplayEndOfAlgorithm(**outputsNamed);
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
nonnestedRecursion.reload();
|
nonnestedAlgorithms.state = state1;
|
||||||
|
nonnestedRecursion.state = state2;
|
||||||
raise e;
|
raise e;
|
||||||
return outputs;
|
return outputs;
|
||||||
return func_wrapper;
|
return func_wrapper;
|
||||||
|
@ -9,3 +9,4 @@ from code.algorithms.search.sequential import SequentialSearch;
|
|||||||
from code.algorithms.search.binary import BinarySearch;
|
from code.algorithms.search.binary import BinarySearch;
|
||||||
from code.algorithms.search.interpol import InterpolationSearch;
|
from code.algorithms.search.interpol import InterpolationSearch;
|
||||||
from code.algorithms.search.jump import JumpSearchLinear;
|
from code.algorithms.search.jump import JumpSearchLinear;
|
||||||
|
from code.algorithms.search.selection import SelectionAlgorithm;
|
||||||
|
46
code/algorithms/search/selection.py
Normal file
46
code/algorithms/search/selection.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# IMPORTS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
from local.maths import *;
|
||||||
|
from local.typing import *;
|
||||||
|
|
||||||
|
from code.core.log import *;
|
||||||
|
from code.algorithms.search.sequential import SequentialSearch;
|
||||||
|
from code.algorithms.methods import *;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# GLOBAL VARIABLES/CONSTANTS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
#
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# CHECKS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
def preChecks(L: List[int], **_):
|
||||||
|
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
|
||||||
|
assert L == list(sorted(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!';
|
||||||
|
return;
|
||||||
|
|
||||||
|
def postChecks(L: List[int], x: int, index: int, **_):
|
||||||
|
value = L[index] if index >= 0 else None;
|
||||||
|
assert value == x, 'Der Algorithmus hat versagt.';
|
||||||
|
return;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# ALGORITHM jump search
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@algorithmInfos(name='Auswahlproblem (i. Element)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
|
def SelectionAlgorithm(L: List[int], i: int) -> int:
|
||||||
|
'''
|
||||||
|
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
||||||
|
Outputs: Position des i. kleinste Element, x, in L, sonst −1. Beachte i=0 <==> Minimum.
|
||||||
|
'''
|
||||||
|
## TODO
|
||||||
|
return -1;
|
0
code/algorithms/sum/__init__.py
Normal file
0
code/algorithms/sum/__init__.py
Normal file
9
code/algorithms/sum/exports.py
Normal file
9
code/algorithms/sum/exports.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# EXPORTS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
from code.algorithms.sum.maxsubsum import MaxSubSum;
|
||||||
|
from code.algorithms.sum.maxsubsum_dc import MaxSubSumDC;
|
59
code/algorithms/sum/maxsubsum.py
Normal file
59
code/algorithms/sum/maxsubsum.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#!/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;
|
145
code/algorithms/sum/maxsubsum_dc.py
Normal file
145
code/algorithms/sum/maxsubsum_dc.py
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
#!/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 mit D & C)', outputnames=('maxSum', 'index_from', 'index_to'), checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
|
def MaxSubSumDC(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 = 0;
|
||||||
|
u = 0;
|
||||||
|
v = -1;
|
||||||
|
if len(L) == 1:
|
||||||
|
## wenn Liste aus 1 Element besteht, nicht teilen:
|
||||||
|
if L[0] > maxSum:
|
||||||
|
v = 0;
|
||||||
|
maxSum = L[0];
|
||||||
|
else:
|
||||||
|
u = math.ceil(len(L)/2);
|
||||||
|
Ll = L[:u];
|
||||||
|
Lr = L[u:];
|
||||||
|
## berechnet maximale Teilsumme der linken Hälfte:
|
||||||
|
maxSum1, u1, v1 = MaxSubSumDC(L=Ll);
|
||||||
|
## berechnet maximale Teilsumme der rechten Hälfte:
|
||||||
|
maxSum2, u2, v2 = MaxSubSumDC(L=Lr);
|
||||||
|
u2, v2 = u2 + len(Ll), v2 + len(Ll); # offsets kompensieren
|
||||||
|
## berechnet maximale Teilsumme mit Überschneidung zw. den Hälften:
|
||||||
|
maxSum3, u3, v3 = lrRandSum(Ll=Ll, Lr=Lr);
|
||||||
|
## bestimme Maximum der 3 Möglichkeiten:
|
||||||
|
maxSum = max(maxSum1, maxSum2, maxSum3);
|
||||||
|
if maxSum == maxSum1:
|
||||||
|
maxSum, u, v = maxSum1, u1, v1;
|
||||||
|
logDebug('max Teilsumme kommt in linker Partition vor: Summe L[i] von i={i} bis {j} = {value}'.format(L = L, i = u, j = v, value = maxSum));
|
||||||
|
elif maxSum == maxSum3:
|
||||||
|
maxSum, u, v = maxSum3, u3, v3;
|
||||||
|
logDebug('max Teilsumme kommt in Überschneidung vor: Summe L[i] von i={i} bis {j} = {value}'.format(L = L, i = u, j = v, value = maxSum));
|
||||||
|
else: # elif maxSum == maxSum2:
|
||||||
|
maxSum, u, v = maxSum2, u2, v2;
|
||||||
|
logDebug('max Teilsumme kommt in rechter Partition vor: Summe L[i] von i={i} bis {j} = {value}'.format(L = L, i = u, j = v, value = maxSum));
|
||||||
|
return maxSum, u, v;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# AUXILIARY METHODS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
def lrRandSum(Ll: List[float], Lr: List[float]) -> Tuple[float, int, int]:
|
||||||
|
'''
|
||||||
|
Bestimmt maximale Teilsumme einer Teiliste einer Liste,
|
||||||
|
wobei die Liste in zwei Intervalle partitioniert ist
|
||||||
|
und die Teilliste beide überschneidet.
|
||||||
|
|
||||||
|
Inputs: Ll, Lr = eine Partition einer Liste von Zahlen in zwei Intervalle
|
||||||
|
Outputs: maxSum, u=0, v
|
||||||
|
'''
|
||||||
|
maxSumL, u, _ = rRandSum(L=Ll);
|
||||||
|
maxSumR, _, v = lRandSum(L=Lr);
|
||||||
|
maxSum = maxSumL + maxSumR;
|
||||||
|
v += len(Ll) # offsets kompensieren
|
||||||
|
return maxSum, u, v;
|
||||||
|
|
||||||
|
def lRandSum(L: List[float]) -> Tuple[float, int, int]:
|
||||||
|
'''
|
||||||
|
Bestimmt maximale Teilsumme aller nicht leeren linken Segmente einer Liste.
|
||||||
|
|
||||||
|
Inputs: L = Liste von Zahlen
|
||||||
|
Outputs: maxSum, u(=0), v
|
||||||
|
'''
|
||||||
|
n = len(L);
|
||||||
|
## berechne kumulative Summen (vorwärts)
|
||||||
|
AddToCounter(n);
|
||||||
|
total = L[0];
|
||||||
|
M = [total];
|
||||||
|
for i in range(1, n):
|
||||||
|
total += L[i];
|
||||||
|
M.append(total);
|
||||||
|
## berechne maximum
|
||||||
|
AddToCounter(n);
|
||||||
|
maxSum = M[0];
|
||||||
|
u = 0;
|
||||||
|
v = 0;
|
||||||
|
for i in range(1, n):
|
||||||
|
if M[i] > maxSum:
|
||||||
|
v = i;
|
||||||
|
maxSum = M[i];
|
||||||
|
return maxSum, 0, v;
|
||||||
|
|
||||||
|
def rRandSum(L: List[float]) -> Tuple[float, int, int]:
|
||||||
|
'''
|
||||||
|
Bestimmt maximale Teilsumme aller nicht leeren rechten Segmente einer Liste.
|
||||||
|
|
||||||
|
Inputs: L = Liste von Zahlen
|
||||||
|
Outputs: maxSum, u, v(=len(L)-1)
|
||||||
|
'''
|
||||||
|
n = len(L);
|
||||||
|
## berechne kumulative Summen (rückwärts)
|
||||||
|
AddToCounter(n);
|
||||||
|
total = L[n-1];
|
||||||
|
M = [total];
|
||||||
|
for i in range(0, n-1)[::-1]:
|
||||||
|
total += L[i];
|
||||||
|
M.insert(0, total);
|
||||||
|
## berechne maximum
|
||||||
|
AddToCounter(n);
|
||||||
|
maxSum = M[n-1];
|
||||||
|
u = n-1;
|
||||||
|
v = n-1;
|
||||||
|
for i in range(0, n-1)[::-1]:
|
||||||
|
if M[i] > maxSum:
|
||||||
|
u = i;
|
||||||
|
maxSum = M[i];
|
||||||
|
return maxSum, u, v;
|
@ -6,7 +6,7 @@
|
|||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
import os;
|
import os;
|
||||||
import sys
|
import sys;
|
||||||
|
|
||||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
|
||||||
|
|
||||||
@ -53,7 +53,11 @@ def LoopThroughCases(path: str):
|
|||||||
inputs = GetAttribute(case, 'inputs', expectedtype=dict, default={});
|
inputs = GetAttribute(case, 'inputs', expectedtype=dict, default={});
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if command == 'algorithm-search-sequential':
|
if command == 'algorithm-sum-maxsub':
|
||||||
|
MaxSubSum(L=inputs['L']);
|
||||||
|
elif command == 'algorithm-sum-maxsub-dc':
|
||||||
|
MaxSubSumDC(L=inputs['L']);
|
||||||
|
elif command == 'algorithm-search-sequential':
|
||||||
SequentialSearch(L=inputs['L'], x=inputs['x']);
|
SequentialSearch(L=inputs['L'], x=inputs['x']);
|
||||||
elif command == 'algorithm-search-binary':
|
elif command == 'algorithm-search-binary':
|
||||||
BinarySearch(L=inputs['L'], x=inputs['x']);
|
BinarySearch(L=inputs['L'], x=inputs['x']);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user