2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-19 11:38:05 +02:00

Adjust Python code to follow PEP8

This commit is contained in:
Martin Thoma 2015-11-20 23:12:22 +01:00
parent b36776fc27
commit de1ad26035
14 changed files with 349 additions and 311 deletions

View file

@ -1,12 +1,14 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def string(zahl):
if zahl <= 9:
return str(zahl)
else:
return chr(55+zahl)
def horner(b, Z):
ergebnis = ''
while Z > 0:

View file

@ -1,49 +1,53 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def ExtendedEuclideanAlgorithm(a, b):
"""
Calculates gcd(a,b) and a linear combination such that
gcd(a,b) = a*x + b*y
As a side effect:
If gcd(a,b) = 1 = a*x + b*y
Then x is multiplicative inverse of a modulo b.
"""
aO, bO = a, b
def extended_euclidean_algorithm(a, b):
"""
Calculates gcd(a,b) and a linear combination such that
gcd(a,b) = a*x + b*y
x=lasty=0
y=lastx=1
while (b!=0):
q= a/b
a, b = b, a%b
x, lastx = lastx-q*x, x
y, lasty = lasty-q*y, y
As a side effect:
If gcd(a,b) = 1 = a*x + b*y
Then x is multiplicative inverse of a modulo b.
"""
aO, bO = a, b
return {
"x": lastx,
"y": lasty,
"gcd": aO * lastx + bO * lasty
}
x = lasty = 0
y = lastx = 1
while (b != 0):
q = a/b
a, b = b, a % b
x, lastx = lastx-q*x, x
y, lasty = lasty-q*y, y
def solveLinearCongruenceEquations(rests, modulos):
"""
Solve a system of linear congruences.
return {
"x": lastx,
"y": lasty,
"gcd": aO * lastx + bO * lasty
}
>>> solveLinearCongruenceEquations([4, 12, 14], [19, 37, 43])
{'congruence class': 22804, 'modulo': 30229}
"""
assert len(rests) == len(modulos)
x = 0
M = reduce(lambda x, y: x*y, modulos)
for mi, resti in zip(modulos, rests):
Mi = M / mi
s = ExtendedEuclideanAlgorithm(Mi, mi)["x"]
e = s * Mi
x += resti * e
return {"congruence class": ((x % M) + M) % M, "modulo": M}
def solve_linear_congruence_equations(rests, modulos):
"""
Solve a system of linear congruences.
Examples
--------
>>> solve_linear_congruence_equations([4, 12, 14], [19, 37, 43])
{'congruence class': 22804, 'modulo': 30229}
"""
assert len(rests) == len(modulos)
x = 0
M = reduce(lambda x, y: x*y, modulos)
for mi, resti in zip(modulos, rests):
Mi = M / mi
s = extended_euclidean_algorithm(Mi, mi)["x"]
e = s * Mi
x += resti * e
return {"congruence class": ((x % M) + M) % M, "modulo": M}
if __name__ == "__main__":
import doctest
doctest.testmod()
import doctest
doctest.testmod()

View file

@ -1,17 +1,18 @@
#!/usr/bin/env python
def wer(r, h):
"""
Calculation of WER with Levenshtein distance.
Works only for iterables up to 254 elements (uint8).
O(nm) time ans space complexity.
Calculation of WER with Levenshtein distance.
Works only for iterables up to 254 elements (uint8).
O(nm) time ans space complexity.
>>> wer("who is there".split(), "is there".split())
1
>>> wer("who is there".split(), "".split())
3
>>> wer("".split(), "who is there".split())
3
>>> wer("who is there".split(), "is there".split())
1
>>> wer("who is there".split(), "".split())
3
>>> wer("".split(), "who is there".split())
3
"""
# initialisation
import numpy
@ -31,8 +32,8 @@ def wer(r, h):
d[i][j] = d[i-1][j-1]
else:
substitution = d[i-1][j-1] + 1
insertion = d[i][j-1] + 1
deletion = d[i-1][j] + 1
insertion = d[i][j-1] + 1
deletion = d[i-1][j] + 1
d[i][j] = min(substitution, insertion, deletion)
return d[len(r)][len(h)]