2021-10-23 13:20:37 +02:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
# IMPORTS
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
from local.maths import *;
|
|
|
|
|
from local.typing import *;
|
|
|
|
|
|
|
|
|
|
from code.core.log import *;
|
2021-10-24 12:28:37 +02:00
|
|
|
|
from code.algorithms.methods import *;
|
2021-10-23 13:20:37 +02:00
|
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
# GLOBAL VARIABLES/CONSTANTS
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
2021-10-24 12:28:37 +02:00
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
# CHECKS
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
def preChecks(L: List[int], **_):
|
|
|
|
|
# Keine Checks!
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
def postChecks(L: List[int], x: int, index: int, **_):
|
2021-10-25 11:33:22 +02:00
|
|
|
|
if x in L:
|
|
|
|
|
assert index >= 0, 'Der Algorithmus sollte nicht -1 zurückgeben.';
|
|
|
|
|
assert L[index] == x, 'Der Algorithmus hat den falschen Index bestimmt.';
|
|
|
|
|
else:
|
|
|
|
|
assert index == -1, 'Der Algorithmus sollte -1 zurückgeben.';
|
2021-10-24 12:28:37 +02:00
|
|
|
|
return;
|
|
|
|
|
|
2021-10-23 13:20:37 +02:00
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
# ALGORITHM sequential search
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
2021-10-24 12:28:37 +02:00
|
|
|
|
@algorithmInfos(name='Sequenziellsuchalgorithmus', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
2021-10-23 13:20:37 +02:00
|
|
|
|
def SequentialSearch(L: List[int], x: int) -> int:
|
|
|
|
|
'''
|
|
|
|
|
Inputs: L = Liste von Zahlen, x = Zahl.
|
|
|
|
|
Outputs: Position von x in L, sonst −1 wenn x nicht in L.
|
|
|
|
|
'''
|
|
|
|
|
n = len(L);
|
|
|
|
|
for i in range(n):
|
|
|
|
|
AddToCounter();
|
|
|
|
|
if L[i] == x:
|
|
|
|
|
logDebug('Element in Position {} gefunden.'.format(i));
|
|
|
|
|
return i;
|
|
|
|
|
logDebug('Element nicht in Position {}.'.format(i));
|
|
|
|
|
return -1;
|