41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# IMPORTS
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
from local.maths import *;
|
|
from local.typing import *;
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# GLOBAL VARIABLES/CONSTANTS
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
#
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# ALGORITHM
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
def AlgoInterpol(L: List[int], u: int, v: int, x: int) -> int:
|
|
if not(L[u] <= x and x <= L[v]):
|
|
print('out of bounds!')
|
|
return -1;
|
|
p = getSuchposition(L, u, v, x);
|
|
print('Interpolatiert von u={u}, v={v} -> p = {p}.'.format(u=u, v=v, p=p));
|
|
if L[p] == x:
|
|
print('gefunden!');
|
|
return p;
|
|
elif x > L[p]:
|
|
print('x > L[p]');
|
|
return AlgoInterpol(L, p+1, v, x);
|
|
else:
|
|
print('x < L[p]');
|
|
return AlgoInterpol(L, u, p-1, x);
|
|
|
|
def getSuchposition(L: List[int], u: int, v: int, x: int) -> int:
|
|
r = (x - L[u])/(L[v]-L[u]);
|
|
p = math.floor(u + r*(v-u))
|
|
return p;
|