Merge pull request 'uebung2' (#1) from uebung2 into master
Reviewed-on: #1 Tiptop. Kleine Anmerkung zu encode und decode. Da gibt es ja sehr viele Überschneidungen im Code, sie sind bis auf ein + resp. -Zeichen fast identisch. In einem solchen Fall kann man sich immer überlegen, ob man die gleichen Teile refaktorieren will. Das ermöglicht übersichtlicheren und besser wartbaren Code.
This commit is contained in:
commit
a833c0d75c
18
.pre-commit-config.yaml
Normal file
18
.pre-commit-config.yaml
Normal file
@ -0,0 +1,18 @@
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v4.5.0
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
- id: end-of-file-fixer
|
||||
- id: check-yaml
|
||||
- id: check-added-large-files
|
||||
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.3.0
|
||||
hooks:
|
||||
- id: ruff # Linting
|
||||
|
||||
- repo: https://github.com/psf/black
|
||||
rev: stable
|
||||
hooks:
|
||||
- id: black # formatting
|
||||
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
39
src/caesar_cipher_helper
Normal file
39
src/caesar_cipher_helper
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
Lösung des Katas: Caesar Cipher Helper
|
||||
Link: https://www.codewars.com/kata/526d42b6526963598d0004db
|
||||
"""
|
||||
|
||||
|
||||
beginning_alphabet = ord("A")
|
||||
ending_alphabet = ord("Z")
|
||||
|
||||
class CaesarCipher(object):
|
||||
def __init__(self, offset):
|
||||
self.offset = offset
|
||||
|
||||
def encode(self, st):
|
||||
upper = st.upper()
|
||||
encoded_st = ""
|
||||
for char in upper:
|
||||
if beginning_alphabet <= ord(char) <= ending_alphabet:
|
||||
encoded_char = (ord(char) + self.offset)
|
||||
if encoded_char > ending_alphabet:
|
||||
encoded_char -= (ending_alphabet - (beginning_alphabet - 1))
|
||||
encoded_st += chr(encoded_char)
|
||||
else:
|
||||
encoded_st += char
|
||||
|
||||
return encoded_st
|
||||
|
||||
def decode(self, st):
|
||||
decoded_st = ""
|
||||
for char in st:
|
||||
if beginning_alphabet <= ord(char) <= ending_alphabet:
|
||||
decoded_char = (ord(char) - self.offset)
|
||||
if decoded_char < beginning_alphabet:
|
||||
decoded_char += (ending_alphabet - (beginning_alphabet - 1))
|
||||
decoded_st += chr(decoded_char)
|
||||
else:
|
||||
decoded_st += char
|
||||
|
||||
return decoded_st
|
||||
13
src/kata_mean_square_error.py
Normal file
13
src/kata_mean_square_error.py
Normal file
@ -0,0 +1,13 @@
|
||||
"""
|
||||
Lösung des Katas: Mean Square Error von kylehill
|
||||
Link zum Kata: https://www.codewars.com/kata/51edd51599a189fe7f000015
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def solution(array_a, array_b):
|
||||
numbers_list = list(zip(array_a, array_b))
|
||||
differences = [x[0] - x[1] for x in numbers_list]
|
||||
mean_square_error = sum([x * x for x in differences]) / len(differences)
|
||||
|
||||
return mean_square_error
|
||||
10
src/moduleA.py
Normal file
10
src/moduleA.py
Normal file
@ -0,0 +1,10 @@
|
||||
def addition(a=1, b=2):
|
||||
return a + b
|
||||
|
||||
|
||||
def f(x: int, y: int) -> int:
|
||||
return x + y
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(addition(5, 5))
|
||||
6
tests/test_moduleA.py
Normal file
6
tests/test_moduleA.py
Normal file
@ -0,0 +1,6 @@
|
||||
from src.moduleA import addition
|
||||
|
||||
|
||||
def test_a():
|
||||
assert addition() == 3
|
||||
assert addition(5, 5) == 10
|
||||
Loading…
x
Reference in New Issue
Block a user