Hilfreiche Funktionen Flashcards

1
Q

Lineare Suche

A
def suche(element, liste):
   for i in range(0, len(liste)):
      if element == liste[i]:
         return i
   return -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Binäre Suche

A
def binaeresuche(element, liste):
   minIndex = 0
   maxIndex = len(liste)-1
   while maxIndex >= minIndex:
      midIndex = minIndex + (maxIndex-minIndex)//2
      if element > liste[midIndex]:
         minIndex = midIndex+1
      elif element < liste[midIndex]:
         maxIndex = midIndex-1
      else:
         return midIndex
   return -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Quersumme

A
def quersumme(zahl):
   summe = 0
   for i in str(zahl):
      summe += int(i)
   return summe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Primzahl

A
def istPrimzahl(n):
   if n < 2:
      return False
   i = 2
   while i*i <= n:
      if n % i == 0:
         return False
      i += 1
   return True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Exponentialfunktion

A
def exp(x,b):
   ergebnis = 1
   for i in range(x):
      ergebnis *= b
   return ergebnis
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Türme von Hanoi

A
def hanoi(quelle, ziel, zwischenlager, scheibe):
    if scheibe > 0:
        hanoi(quelle, zwischenlager, ziel, scheibe-1)
        print("Lege Scheibe",scheibe,"von",quelle,"nach",ziel)
        hanoi(zwischenlager, ziel, quelle, scheibe-1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Caesar Verschlüsselung

A
def caesarEncode(text, key):
    alpha = "abcdefghijklmnopqrstuvwxyz"
    cipher = ""
    for char in text:
        if char in alpha:
            cipher += alpha[(alpha.index(char) + key) % 26]
        else:
            cipher += char
    return cipher

def caesarDecode(cipher, key):
    return caesarEncode(cipher, -key)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly